diff options
Diffstat (limited to 'src/socket.c')
-rw-r--r-- | src/socket.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c new file mode 100644 index 00000000..16e90f2f --- /dev/null +++ b/src/socket.c | |||
@@ -0,0 +1,74 @@ | |||
1 | /* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved. | ||
2 | * | ||
3 | * See the LICENSE file | ||
4 | * | ||
5 | * The origin of this software must not be misrepresented, either by | ||
6 | * explicit claim or by omission. Since few users ever read sources, | ||
7 | * credits must appear in the documentation. | ||
8 | * | ||
9 | * date: Sat Mar 30 14:44:42 GMT 2002 | ||
10 | * | ||
11 | * -- Basic socket operations | ||
12 | * | ||
13 | */ | ||
14 | |||
15 | |||
16 | #include "socket.h" | ||
17 | |||
18 | |||
19 | /*********************************************************************************************************/ | ||
20 | /* | ||
21 | * socket operations | ||
22 | */ | ||
23 | int __ILWS_newdata(int sock) { | ||
24 | int ret; | ||
25 | struct timeval tv; | ||
26 | fd_set rfds; | ||
27 | FD_ZERO(&rfds); | ||
28 | FD_SET((unsigned)sock,&rfds); | ||
29 | tv.tv_usec=2; | ||
30 | tv.tv_sec=0; | ||
31 | ret=select(sock+1,&rfds,NULL,NULL,&tv); | ||
32 | FD_CLR((unsigned)sock,&rfds); | ||
33 | return ret; | ||
34 | } | ||
35 | |||
36 | /*********************************************************************************************************/ | ||
37 | /* | ||
38 | * to add a listen socket | ||
39 | */ | ||
40 | int __ILWS_listensocket(short port, int saddr) { | ||
41 | struct sockaddr_in sa; | ||
42 | int ret; | ||
43 | int sockopt=1; /* Rocco Was Here */ | ||
44 | sa.sin_addr.s_addr=saddr; | ||
45 | sa.sin_port=htons((short)port); | ||
46 | sa.sin_family=AF_INET; | ||
47 | ret=socket(AF_INET,SOCK_STREAM,6); // tcp | ||
48 | if(ret==-1) { | ||
49 | return -1; | ||
50 | }; | ||
51 | /* Rocco Was Here */ | ||
52 | setsockopt(ret,SOL_SOCKET,SO_REUSEADDR,(char *)&sockopt,sizeof(sockopt)); // why? Rocco | ||
53 | |||
54 | if(bind(ret,(struct sockaddr *)&sa,sizeof(sa))==-1) { | ||
55 | close(ret); /* Rocco Was Here */ | ||
56 | return -1; | ||
57 | }; | ||
58 | |||
59 | if(listen(ret,512)==-1) { // 512 backlog | ||
60 | close(ret); /* Rocco Was Here */ | ||
61 | return -1; | ||
62 | }; | ||
63 | IFDEBUG(fprintf(stderr,"socket.c: Listen on port %d\n",port)); | ||
64 | return ret; | ||
65 | } | ||
66 | |||
67 | /*********************************************************************************************************/ | ||
68 | /* | ||
69 | * as the read function | ||
70 | */ | ||
71 | int __ILWS_read(int sock,void *buf,size_t s) { | ||
72 | return recv(sock,buf,s,0); | ||
73 | } | ||
74 | |||