2 #include "structdefs.h"
5 #include <sys/socket.h>
11 #include "SimpleHash.h"
12 #include "GenericHashtable.h"
14 struct RuntimeHash *fdtoobject;
16 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___,int ___port___) {
18 // not supported in RAW version
21 struct sockaddr_in sin;
24 bzero(&sin, sizeof(sin));
25 sin.sin_family= AF_INET;
26 sin.sin_port=htons(___port___);
27 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
28 #if defined(THREADS)||defined(DSTM)
30 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
34 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
35 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
36 #if defined(THREADS)||defined(DSTM)
47 fcntl(___fd___, F_SETFD, 1);
48 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
49 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
62 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
63 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
64 RuntimeHashremovekey(fdtoobject, ___fd___);
65 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
71 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
73 // not supported in RAW version
79 struct sockaddr_in sin;
80 bzero(&sin, sizeof(sin));
81 sin.sin_family= AF_INET;
83 sin.sin_addr.s_addr=INADDR_ANY;
85 fd=socket(AF_INET, SOCK_STREAM, 0);
89 printf("createSocket error in nativeBind\n");
92 longjmp(error_handler,12);
102 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
103 if (rc<0) goto error;
105 sa_size = sizeof(sin);
106 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
107 if (rc<0) goto error;
115 printf("createSocket error #2 in nativeBind\n");
118 longjmp(error_handler,13);
129 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
131 // not supported in RAW version
134 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
135 int length=VAR(___hostname___)->___length___;
137 char * str=malloc(length+1);
139 struct ArrayObject * arraybytearray;
141 for(i=0; i<length; i++) {
142 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
145 h=gethostbyname(str);
148 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
151 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
153 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
156 struct ArrayObject *bytearray;
159 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
160 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
161 arraybytearray=(struct ArrayObject *) ptrarray[2];
164 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
166 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
168 int ha=ntohl(*(int *)h->h_addr_list[i]);
169 (&bytearray->___length___)[1]=ha;
173 return arraybytearray;
178 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
180 // not supported in RAW version
186 struct sockaddr_in sin;
188 bzero(&sin, sizeof(sin));
189 sin.sin_family = AF_INET;
190 sin.sin_port = htons(port);
191 sin.sin_addr.s_addr = htonl(INADDR_ANY);
192 fd=socket(AF_INET, SOCK_STREAM, 0);
196 printf("createSocket error #1\n");
199 longjmp(error_handler,5);
209 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
213 printf("createSocket error #2\n");
216 longjmp(error_handler,6);
227 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
234 fcntl(fd, F_SETFD, 1);
235 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
239 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
243 printf("createSocket error #3\n");
246 longjmp(error_handler,7);
257 if (listen(fd, 5)<0) {
261 printf("createSocket error #4\n");
264 longjmp(error_handler,8);
274 /* Store the fd/socket object mapping */
276 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
283 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
285 // not supported in RAW version
288 struct sockaddr_in sin;
289 unsigned int sinlen=sizeof(sin);
290 int fd=VAR(___this___)->___fd___;
292 #if defined(THREADS)||defined(DSTM)
294 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
297 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
298 #if defined(THREADS)||defined(DSTM)
306 printf("acceptSocket error #1\n");
309 longjmp(error_handler,9);
319 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
320 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
323 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
324 enqueueObject(VAR(___this___), NULL, 0);
325 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
326 //enqueueObject(VAR(___this___), objq4socketobj[corenum], numqueues4socketobj[corenum]);
328 flagorand(VAR(___this___),0,0xFFFFFFFE);
329 enqueueObject(VAR(___this___));
336 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
337 int fd=VAR(___this___)->___fd___;
338 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
343 bytewritten=write(fd, &charstr[offset], length);
344 if (bytewritten==-1&&errno!=EAGAIN)
352 perror("ERROR IN NATIVEWRITE");
353 printf("error=%d remaining bytes %d\n",errno, length);
360 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
361 int fd=VAR(___this___)->___fd___;
362 int length=VAR(___b___)->___length___;
364 char * charstr=malloc(length);
366 #if defined(THREADS)||defined(DSTM)
368 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
373 // printf("Doing read on %d\n",fd);
375 byteread=read(fd, charstr, length);
376 } while(byteread==-1&&errno==EINTR);
377 #if defined(THREADS)||defined(DSTM)
385 for(i=0; i<byteread; i++) {
386 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
394 printf("ERROR IN NATIVEREAD\n");
400 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
401 enqueueObject(VAR(___this___), NULL, 0);
402 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
403 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
405 flagorand(VAR(___this___),0,0xFFFFFFFE);
406 enqueueObject(VAR(___this___));
412 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
413 int fd=VAR(___this___)->___fd___;
416 RuntimeHashget(fdtoobject, fd, &data);
417 RuntimeHashremove(fdtoobject, fd, data);
420 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
421 enqueueObject(VAR(___this___), NULL, 0);
422 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
423 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
425 flagorand(VAR(___this___),0,0xFFFFFFFE);
426 enqueueObject(VAR(___this___));