Debian Codegraph @

src/utils/cst_socket.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: October 2000 */
35 /*************************************************************************/
36 /* */
37 /* Some generic socket client/server code */
38 /* */
39 /*************************************************************************/
40 #ifdef CST_NO_SOCKETS
41
42 int cst_socket_open(const char *host, int port)
43 {
44 (void)host;
45 (void)port;
46 return -1;
47 }
48
49 int cst_socket_server(const char *name, int port,
50 int (process_client)(int name,int fd))
51 {
52 (void)name;
53 (void)port;
54 (void)process_client;
55 return -1;
56 }
57
58 int cst_socket_close(int socket)
59 {
60 return -1;
61 }
62 #else
63 #include <stdio.h>
64 #include <stdlib.h>
65 #ifndef _MSC_VER
66 #include <unistd.h>
67 #endif
68 #include <string.h>
69 #include <sys/types.h>
70 #ifndef _MSC_VER
71 #include <sys/socket.h>
72 #include <netdb.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #else
76 #include <io.h>
77 #include <WinSock2.h>
78 #pragma comment(lib, "Ws2_32.lib")
79 #endif
80 #include "cst_socket.h"
81 #include "cst_error.h"
82
83 int cst_socket_open(const char *host, int port)
84 {
85 /* Return an FD to a remote server */
86 struct sockaddr_in serv_addr;
87 struct hostent *serverhost;
88 int fd;
89
90 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
91
92 if (fd < 0)
93 {
94 cst_errmsg("cst_socket: can't get socket\n");
95 return -1;
96 }
97 memset(&serv_addr, 0, sizeof(serv_addr));
98 if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
99 {
100 /* its a name rather than an ipnum */
101 serverhost = gethostbyname(host);
102 if (serverhost == (struct hostent *)0)
103 {
104 cst_errmsg("cst_socket: gethostbyname failed\n");
105 return -1;
106 }
107 memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
108 }
109 serv_addr.sin_family = AF_INET;
110 serv_addr.sin_port = htons(port);
111
112 if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0)
113 {
114 cst_errmsg("cst_socket: connect to server failed\n");
115 return -1;
116 }
117
118 return fd;
119 }
120
121 int cst_socket_close(int socket)
122 {
123 return close(socket);
124 }
125
126 int cst_socket_server(const char *name, int port,
127 int (process_client)(int name,int fd))
128 {
129 struct sockaddr_in serv_addr;
130 int fd, fd1;
131 int client_name = 0;
132 int one = 1;
133
134 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
135
136 if (fd < 0)
137 {
138 cst_errmsg("can't open socket %d\n",port);
139 return -1;
140 }
141
142 if (setsockopt(fd, SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(int)) < 0)
143 {
144 cst_errmsg("socket SO_REUSERADDR failed\n");
145 return -1;
146 }
147
148 memset(&serv_addr, 0, sizeof(serv_addr));
149 serv_addr.sin_family = AF_INET;
150 serv_addr.sin_port = htons(port);
151 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
152
153 if (bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0)
154 {
155 cst_errmsg("socket: bind failed\n");
156 return -1;
157 }
158
159 if (listen(fd, 5) != 0)
160 {
161 cst_errmsg("socket: listen failed\n");
162 return -1;
163 }
164
165 if (name)
166 printf("server (%s) started on port %d\n",name, port);
167
168 while(1) /* never exits except by signals */
169 {
170 if((fd1 = accept(fd, 0, 0)) < 0)
171 {
172 cst_errmsg("socket: accept failed\n");
173 return -1;
174 }
175
176 client_name++;
177
178 (process_client)(client_name,fd1);
179
180 close(fd1);
181 }
182
183 return 0;
184 }
185
186 #endif