2 #include "structdefs.h"
5 #include <sys/socket.h>
9 #include <netinet/tcp.h>
12 #include "SimpleHash.h"
13 #include "GenericHashtable.h"
15 struct RuntimeHash *fdtoobject;
17 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___,int ___port___) {
19 // not supported in RAW version
22 struct sockaddr_in sin;
25 bzero(&sin, sizeof(sin));
26 sin.sin_family= AF_INET;
27 sin.sin_port=htons(___port___);
28 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
29 #if defined(THREADS)||defined(DSTM)||defined(STM)
31 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
35 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
36 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
37 #if defined(THREADS)||defined(DSTM)||defined(STM)
45 setsockopt(___fd___, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
51 fcntl(___fd___, F_SETFD, 1);
52 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
53 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
66 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
67 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
68 RuntimeHashremovekey(fdtoobject, ___fd___);
69 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
75 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
77 // not supported in RAW version
83 struct sockaddr_in sin;
84 bzero(&sin, sizeof(sin));
85 sin.sin_family= AF_INET;
87 sin.sin_addr.s_addr=INADDR_ANY;
89 fd=socket(AF_INET, SOCK_STREAM, 0);
93 printf("createSocket error in nativeBind\n");
96 longjmp(error_handler,12);
106 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
107 if (rc<0) goto error;
109 sa_size = sizeof(sin);
110 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
111 if (rc<0) goto error;
119 printf("createSocket error #2 in nativeBind\n");
122 longjmp(error_handler,13);
133 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
135 // not supported in RAW version
138 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
139 int length=VAR(___hostname___)->___length___;
141 char * str=malloc(length+1);
143 struct ArrayObject * arraybytearray;
145 for(i=0; i<length; i++) {
146 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
149 h=gethostbyname(str);
152 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
155 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
157 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
160 struct ArrayObject *bytearray;
163 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
164 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
165 arraybytearray=(struct ArrayObject *) ptrarray[2];
168 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
170 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
172 int ha=ntohl(*(int *)h->h_addr_list[i]);
173 (&bytearray->___length___)[1]=ha;
177 return arraybytearray;
182 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
184 // not supported in RAW version
190 struct sockaddr_in sin;
192 bzero(&sin, sizeof(sin));
193 sin.sin_family = AF_INET;
194 sin.sin_port = htons(port);
195 sin.sin_addr.s_addr = htonl(INADDR_ANY);
196 fd=socket(AF_INET, SOCK_STREAM, 0);
200 printf("createSocket error #1\n");
203 longjmp(error_handler,5);
213 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
217 printf("createSocket error #2\n");
220 longjmp(error_handler,6);
231 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
238 fcntl(fd, F_SETFD, 1);
239 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
243 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
247 printf("createSocket error #3\n");
250 longjmp(error_handler,7);
261 if (listen(fd, 5)<0) {
265 printf("createSocket error #4\n");
268 longjmp(error_handler,8);
278 /* Store the fd/socket object mapping */
280 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
287 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
289 // not supported in RAW version
292 struct sockaddr_in sin;
293 unsigned int sinlen=sizeof(sin);
294 int fd=VAR(___this___)->___fd___;
296 #if defined(THREADS)||defined(DSTM)||defined(STM)
298 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
301 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
303 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
304 #if defined(THREADS)||defined(DSTM)||defined(STM)
312 printf("acceptSocket error #1\n");
315 longjmp(error_handler,9);
325 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
326 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
329 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
330 enqueueObject(VAR(___this___), NULL, 0);
331 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
332 //enqueueObject(VAR(___this___), objq4socketobj[corenum], numqueues4socketobj[corenum]);
334 flagorand(VAR(___this___),0,0xFFFFFFFE);
335 enqueueObject(VAR(___this___));
342 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
343 int fd=VAR(___this___)->___fd___;
344 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
349 bytewritten=write(fd, &charstr[offset], length);
350 if (bytewritten==-1&&errno!=EAGAIN)
358 perror("ERROR IN NATIVEWRITE");
359 printf("error=%d remaining bytes %d\n",errno, length);
366 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
367 int fd=VAR(___this___)->___fd___;
368 int length=VAR(___b___)->___length___;
370 char * charstr=malloc(length);
372 #if defined(THREADS)||defined(DSTM)||defined(STM)
374 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
379 // printf("Doing read on %d\n",fd);
381 byteread=read(fd, charstr, length);
382 } while(byteread==-1&&errno==EINTR);
383 #if defined(THREADS)||defined(DSTM)||defined(STM)
391 for(i=0; i<byteread; i++) {
392 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
400 printf("ERROR IN NATIVEREAD\n");
406 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
407 enqueueObject(VAR(___this___), NULL, 0);
408 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
409 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
411 flagorand(VAR(___this___),0,0xFFFFFFFE);
412 enqueueObject(VAR(___this___));
418 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
419 int fd=VAR(___this___)->___fd___;
422 RuntimeHashget(fdtoobject, fd, &data);
423 RuntimeHashremove(fdtoobject, fd, data);
426 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
427 enqueueObject(VAR(___this___), NULL, 0);
428 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
429 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
431 flagorand(VAR(___this___),0,0xFFFFFFFE);
432 enqueueObject(VAR(___this___));