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)));
23 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
24 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
30 fcntl(___fd___, F_SETFD, 1);
31 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
32 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
44 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
48 struct sockaddr_in sin;
49 bzero(&sin, sizeof(sin));
50 sin.sin_family= AF_INET;
52 sin.sin_addr.s_addr=INADDR_ANY;
54 fd=socket(AF_INET, SOCK_STREAM, 0);
58 printf("createSocket error in nativeBind\n");
61 longjmp(error_handler,12);
71 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
74 sa_size = sizeof(sin);
75 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
84 printf("createSocket error #2 in nativeBind\n");
87 longjmp(error_handler,13);
97 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
98 int length=VAR(___hostname___)->___length___;
100 char * str=malloc(length+1);
102 struct ArrayObject * arraybytearray;
104 for(i=0;i<length;i++) {
105 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
108 h=gethostbyname(str);
111 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
114 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
116 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
119 struct ArrayObject *bytearray;
122 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
123 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
124 arraybytearray=(struct ArrayObject *) ptrarray[2];
127 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
129 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
131 int ha=ntohl(*(int *)h->h_addr_list[i]);
132 (&bytearray->___length___)[1]=ha;
136 return arraybytearray;
140 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
144 struct sockaddr_in sin;
146 bzero(&sin, sizeof(sin));
147 sin.sin_family = AF_INET;
148 sin.sin_port = htons (port);
149 sin.sin_addr.s_addr = htonl (INADDR_ANY);
150 fd=socket(AF_INET, SOCK_STREAM, 0);
154 printf("createSocket error #1\n");
157 longjmp(error_handler,5);
167 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
171 printf("createSocket error #2\n");
174 longjmp(error_handler,6);
185 fcntl(fd, F_SETFD, 1);
186 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
190 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
194 printf("createSocket error #3\n");
197 longjmp(error_handler,7);
208 if (listen(fd, 5)<0) {
212 printf("createSocket error #4\n");
215 longjmp(error_handler,8);
225 /* Store the fd/socket object mapping */
227 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
233 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
234 struct sockaddr_in sin;
235 unsigned int sinlen=sizeof(sin);
236 int fd=VAR(___this___)->___fd___;
240 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
243 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
252 printf("acceptSocket error #1\n");
255 longjmp(error_handler,9);
265 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
266 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
268 flagorand(VAR(___this___),0,0xFFFFFFFE);
273 void CALL02(___Socket______nativeWrite_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
274 int fd=VAR(___this___)->___fd___;
275 int length=VAR(___b___)->___length___;
276 char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
278 int bytewritten=write(fd, charstr, length);
279 if (bytewritten==-1&&errno==EAGAIN)
282 if (bytewritten!=length) {
283 perror("ERROR IN NATIVEWRITE");
289 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
290 int fd=VAR(___this___)->___fd___;
291 int length=VAR(___b___)->___length___;
293 char * charstr=malloc(length);
297 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
300 int byteread=read(fd, charstr, length);
309 for(i=0;i<byteread;i++) {
310 (((char *)& VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
317 printf("ERROR IN NATIVEREAD\n");
321 flagorand(VAR(___this___),0,0xFFFFFFFE);
326 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
327 int fd=VAR(___this___)->___fd___;
330 RuntimeHashget(fdtoobject, fd, &data);
331 RuntimeHashremove(fdtoobject, fd, data);
333 flagorand(VAR(___this___),0,0xFFFFFFFE);