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______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
59 struct sockaddr_in sin;
60 bzero(&sin, sizeof(sin));
61 sin.sin_family= AF_INET;
63 sin.sin_addr.s_addr=INADDR_ANY;
65 fd=socket(AF_INET, SOCK_STREAM, 0);
69 printf("createSocket error in nativeBind\n");
72 longjmp(error_handler,12);
82 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
85 sa_size = sizeof(sin);
86 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
95 printf("createSocket error #2 in nativeBind\n");
98 longjmp(error_handler,13);
108 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
109 int length=VAR(___hostname___)->___length___;
111 char * str=malloc(length+1);
113 struct ArrayObject * arraybytearray;
115 for(i=0;i<length;i++) {
116 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
119 h=gethostbyname(str);
122 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
125 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
127 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
130 struct ArrayObject *bytearray;
133 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
134 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
135 arraybytearray=(struct ArrayObject *) ptrarray[2];
138 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
140 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
142 int ha=ntohl(*(int *)h->h_addr_list[i]);
143 (&bytearray->___length___)[1]=ha;
147 return arraybytearray;
151 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
155 struct sockaddr_in sin;
157 bzero(&sin, sizeof(sin));
158 sin.sin_family = AF_INET;
159 sin.sin_port = htons (port);
160 sin.sin_addr.s_addr = htonl (INADDR_ANY);
161 fd=socket(AF_INET, SOCK_STREAM, 0);
165 printf("createSocket error #1\n");
168 longjmp(error_handler,5);
178 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
182 printf("createSocket error #2\n");
185 longjmp(error_handler,6);
196 fcntl(fd, F_SETFD, 1);
197 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
201 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
205 printf("createSocket error #3\n");
208 longjmp(error_handler,7);
219 if (listen(fd, 5)<0) {
223 printf("createSocket error #4\n");
226 longjmp(error_handler,8);
236 /* Store the fd/socket object mapping */
238 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
244 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
245 struct sockaddr_in sin;
246 unsigned int sinlen=sizeof(sin);
247 int fd=VAR(___this___)->___fd___;
251 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
254 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
263 printf("acceptSocket error #1\n");
266 longjmp(error_handler,9);
276 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
277 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
279 flagorand(VAR(___this___),0,0xFFFFFFFE);
284 void CALL02(___Socket______nativeWrite_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
285 int fd=VAR(___this___)->___fd___;
286 int length=VAR(___b___)->___length___;
287 char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
289 int bytewritten=write(fd, charstr, length);
290 if (bytewritten==-1&&errno==EAGAIN)
293 if (bytewritten!=length) {
294 perror("ERROR IN NATIVEWRITE");
295 printf("Supposed to write %d, wrote %d\n", length, bytewritten);
301 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
302 int fd=VAR(___this___)->___fd___;
303 int length=VAR(___b___)->___length___;
305 char * charstr=malloc(length);
309 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
314 // printf("Doing read on %d\n",fd);
316 byteread=read(fd, charstr, length);
328 for(i=0;i<byteread;i++) {
329 (((char *)& VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
336 printf("ERROR IN NATIVEREAD\n");
341 flagorand(VAR(___this___),0,0xFFFFFFFE);
346 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
347 int fd=VAR(___this___)->___fd___;
350 RuntimeHashget(fdtoobject, fd, &data);
351 RuntimeHashremove(fdtoobject, fd, data);
353 flagorand(VAR(___this___),0,0xFFFFFFFE);