2 #include "structdefs.h"
3 #include <sys/socket.h>
9 #include "SimpleHash.h"
10 #include "GenericHashtable.h"
12 extern struct RuntimeHash *fdtoobject;
14 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___ ,int ___port___) {
15 struct sockaddr_in sin;
18 bzero(&sin, sizeof(sin));
19 sin.sin_family= AF_INET;
20 sin.sin_port=htons(___port___);
21 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
24 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
28 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
29 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
41 fcntl(___fd___, F_SETFD, 1);
42 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
43 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
55 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
56 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
57 RuntimeHashremovekey(fdtoobject, ___fd___);
58 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
64 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
68 struct sockaddr_in sin;
69 bzero(&sin, sizeof(sin));
70 sin.sin_family= AF_INET;
72 sin.sin_addr.s_addr=INADDR_ANY;
74 fd=socket(AF_INET, SOCK_STREAM, 0);
78 printf("createSocket error in nativeBind\n");
81 longjmp(error_handler,12);
91 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
94 sa_size = sizeof(sin);
95 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
104 printf("createSocket error #2 in nativeBind\n");
107 longjmp(error_handler,13);
117 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
118 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
119 int length=VAR(___hostname___)->___length___;
121 char * str=malloc(length+1);
123 struct ArrayObject * arraybytearray;
125 for(i=0;i<length;i++) {
126 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
129 h=gethostbyname(str);
132 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
135 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
137 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
140 struct ArrayObject *bytearray;
143 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
144 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
145 arraybytearray=(struct ArrayObject *) ptrarray[2];
148 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
150 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
152 int ha=ntohl(*(int *)h->h_addr_list[i]);
153 (&bytearray->___length___)[1]=ha;
157 return arraybytearray;
161 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
165 struct sockaddr_in sin;
167 bzero(&sin, sizeof(sin));
168 sin.sin_family = AF_INET;
169 sin.sin_port = htons (port);
170 sin.sin_addr.s_addr = htonl (INADDR_ANY);
171 fd=socket(AF_INET, SOCK_STREAM, 0);
175 printf("createSocket error #1\n");
178 longjmp(error_handler,5);
188 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
192 printf("createSocket error #2\n");
195 longjmp(error_handler,6);
206 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
213 fcntl(fd, F_SETFD, 1);
214 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
218 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
222 printf("createSocket error #3\n");
225 longjmp(error_handler,7);
236 if (listen(fd, 5)<0) {
240 printf("createSocket error #4\n");
243 longjmp(error_handler,8);
253 /* Store the fd/socket object mapping */
255 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
261 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
262 struct sockaddr_in sin;
263 unsigned int sinlen=sizeof(sin);
264 int fd=VAR(___this___)->___fd___;
268 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
271 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
280 printf("acceptSocket error #1\n");
283 longjmp(error_handler,9);
293 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
294 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
297 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
298 enqueueObject(VAR(___this___), NULL, 0);
299 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
300 //enqueueObject(VAR(___this___), objq4socketobj[corenum], numqueues4socketobj[corenum]);
302 flagorand(VAR(___this___),0,0xFFFFFFFE);
303 enqueueObject(VAR(___this___));
309 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
310 int fd=VAR(___this___)->___fd___;
311 char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int)+offset;
316 bytewritten=write(fd, &charstr[offset], length);
317 if (bytewritten==-1&&errno!=EAGAIN)
324 perror("ERROR IN NATIVEWRITE");
325 printf("error=%d remaining bytes %d\n",errno, length);
331 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
332 int fd=VAR(___this___)->___fd___;
333 int length=VAR(___b___)->___length___;
335 char * charstr=malloc(length);
339 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
344 // printf("Doing read on %d\n",fd);
346 byteread=read(fd, charstr, length);
347 } while(byteread==-1&&errno==EINTR);
356 for(i=0;i<byteread;i++) {
357 (((char *)& VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
364 printf("ERROR IN NATIVEREAD\n");
369 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
370 enqueueObject(VAR(___this___), NULL, 0);
371 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
372 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
374 flagorand(VAR(___this___),0,0xFFFFFFFE);
375 enqueueObject(VAR(___this___));
381 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
382 int fd=VAR(___this___)->___fd___;
385 RuntimeHashget(fdtoobject, fd, &data);
386 RuntimeHashremove(fdtoobject, fd, data);
389 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
390 enqueueObject(VAR(___this___), NULL, 0);
391 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
392 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
394 flagorand(VAR(___this___),0,0xFFFFFFFE);
395 enqueueObject(VAR(___this___));