source: roaraudio/libroar/socket.c @ 374:e3f42f05cdfb

Last change on this file since 374:e3f42f05cdfb was 374:e3f42f05cdfb, checked in by phi, 16 years ago

added a function to open an UDP socket

File size: 7.1 KB
Line 
1//socket.c:
2
3#include "libroar.h"
4
5/* old...
6#define MODE_LISTEN  1
7#define MODE_CONNECT 2
8*/
9#define MODE_LISTEN  ROAR_SOCKET_MODE_LISTEN
10#define MODE_CONNECT ROAR_SOCKET_MODE_CONNECT
11
12int roar_socket_new_tcp (void) {
13 int fh;
14 int opt = IPTOS_LOWDELAY;
15// int extra_flags = TCP_NODELAY;
16
17 fh = socket(PF_INET, SOCK_STREAM, 0);
18
19/*
20phi@ph7:libroar $ IP_TOS
21phi@ph7:libroar $ IPTOS_LOWDELAY
22*/
23
24 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
25
26 return fh;
27}
28
29int roar_socket_new_udp (void) {
30 int fh;
31 int opt = IPTOS_LOWDELAY;
32
33 fh = socket(PF_INET, SOCK_DGRAM, 0);
34
35 setsockopt(fh, IPPROTO_IP, IP_TOS, &opt, sizeof(int));
36
37 return fh;
38}
39
40int roar_socket_new_unix (void) {
41 int fh;
42
43 fh = socket(AF_UNIX, SOCK_STREAM, 0);
44
45 return fh;
46}
47
48int roar_socket_nonblock(int fh, int state) {
49 int flags;
50
51 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
52  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno));
53  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
54  return -1;
55 }
56
57 flags |= O_NONBLOCK;
58
59 if ( state == ROAR_SOCKET_BLOCK )
60  flags -= O_NONBLOCK;
61
62 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
63  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno));
64  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
65  return -1;
66 }
67
68 ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state);
69 return 0;
70}
71
72int roar_socket_listen  (int type, char * host, int port) {
73 return roar_socket_open(MODE_LISTEN, type, host, port);
74}
75
76int roar_socket_connect (char * host, int port) {
77 char * proxy_type = getenv("ROAR_PROXY");
78
79 if ( proxy_type == NULL || strcmp(proxy_type, "") == 0 ) {
80  return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
81 } else {
82  return roar_socket_open_proxy(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port, proxy_type);
83 }
84}
85
86int roar_socket_open (int mode, int type, char * host, int port) {
87// int type = ROAR_SOCKET_TYPE_INET;
88 int fh;
89 struct sockaddr_in   socket_addr;
90 struct sockaddr_un   socket_addr_un;
91 struct hostent     * he;
92 //unsigned int host_div = 0;
93 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
94
95 if ( mode == MODE_LISTEN )
96  mode_func = bind;
97
98 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
99  type = ROAR_SOCKET_TYPE_INET;
100  if ( *host == '/' ) {
101   type = ROAR_SOCKET_TYPE_UNIX;
102  } else if ( strcmp(host, "+fork") == 0 ) {
103   type = ROAR_SOCKET_TYPE_FORK;
104  }
105 }
106
107
108 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
109             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port);
110
111 memset(&socket_addr   , 0, sizeof(socket_addr));
112 memset(&socket_addr_un, 0, sizeof(socket_addr_un));
113 memset(&he,             0, sizeof(he));               // FIXME: we have a valid pointer in here????
114
115
116 if ( type == ROAR_SOCKET_TYPE_INET ) {
117
118  if ( (he = gethostbyname(host)) == NULL ) {
119   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
120                     host);
121   return -1;
122  }
123
124  memcpy((struct in_addr *)&socket_addr.sin_addr, he->h_addr, sizeof(struct in_addr));
125
126  /* set the connect information */
127  socket_addr.sin_family = AF_INET;
128  socket_addr.sin_port = htons( port );
129
130  fh = roar_socket_new_tcp();
131
132  if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) {
133   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
134   close(fh);
135   return -1;
136  }
137  // hey! we have a socket...
138 } else if ( type == ROAR_SOCKET_TYPE_UNIX ) {
139  socket_addr_un.sun_family = AF_UNIX;
140  strncpy(socket_addr_un.sun_path, host, sizeof(socket_addr_un.sun_path) - 1);
141
142  fh = roar_socket_new_unix();
143
144  if ( mode_func(fh, (struct sockaddr *)&socket_addr_un, sizeof(struct sockaddr_un)) == -1 ) {
145   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
146   close(fh);
147   return -1;
148  }
149 } else if ( type == ROAR_SOCKET_TYPE_FORK ) {
150  return roar_socket_open_fork(mode, host, port);
151 } else if ( type == ROAR_SOCKET_TYPE_FILE ) {
152  return roar_socket_open_file(mode, host, port);
153 } else {
154  return -1;
155 }
156
157 if ( mode == MODE_LISTEN )
158  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
159   close(fh);
160   return -1;
161  }
162
163 return fh;
164}
165
166int roar_socket_open_fork  (int mode, char * host, int port) {
167 int socks[2];
168 int r;
169 char fhstr[8];
170
171 if ( mode == MODE_LISTEN )
172  return -1;
173
174 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
175  return -1;
176 }
177
178 r = fork();
179
180 if ( r == -1 ) { // error!
181  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno));
182  close(socks[0]);
183  close(socks[1]);
184  return -1;
185 } else if ( r == 0 ) { // we are the child
186  close(socks[0]);
187
188  close(ROAR_STDIN ); // we do not want roard to have any standard input
189  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
190                      // but STDERR we keep open for error messages.
191
192  snprintf(fhstr, 7, "%i", socks[1]);
193
194  execlp("roard", "roard", "--terminate", "--no-listen", "--client-fh", fhstr, NULL);
195
196  // we are still alive?
197  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
198  _exit(1);
199 } else { // we are the parent
200  close(socks[1]);
201  return socks[0];
202 }
203
204 return -1;
205}
206
207int roar_socket_open_file  (int mode, char * host, int port) {
208 int fh;
209
210 if ( mode == MODE_LISTEN )
211  return -1;
212
213 if ( (fh = open(host, O_RDONLY, 0644)) == -1 ) {
214  ROAR_ERR("roar_socket_open_file(*): Can not open file %s: %s", host, strerror(errno));
215 }
216
217 return fh;
218}
219
220// --- [ PROXY CODE ] ---
221
222// generic proxy code:
223
224int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
225 int    proxy_port;
226 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
227 char * proxy_addr;
228 int    i;
229 int    fh;
230
231 // TODO: change this so we support listen() proxys (ssh -R)
232 if ( mode != MODE_CONNECT )
233  return -1;
234
235 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
236  proxy_addr = getenv("socks_proxy");
237
238  proxy_port = 9050; // TOR's default port
239
240  if ( proxy_addr == NULL )
241   return -1;
242
243  for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++)
244   proxy_host[i] = proxy_addr[i];
245  proxy_host[i] = 0;
246
247  if ( i == 0 ) // no hostname found
248   return -1;
249
250  if ( proxy_addr[i] == ':' )
251   proxy_port = atoi(&proxy_addr[i+1]);
252
253  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
254   return -1;
255  }
256
257  if ( roar_socket_open_socks4a(mode, fh, host, port) == -1 ) {
258   close(fh);
259   return -1;
260  }
261
262  return fh;
263 } else {
264  return -1; // unknown type
265 }
266}
267
268// protocoll dependet proxy code:
269
270int roar_socket_open_socks4a(int mode, int fh, char * host, int port) {
271 char buf[9];
272 int  len;
273
274 buf[0] = 0x04;
275 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
276 *((uint16_t*)&buf[2]) = htons(port);
277 buf[4] = 0x00;
278 buf[5] = 0x00;
279 buf[6] = 0x00;
280 buf[7] = 0x01;
281 buf[8] = 0x00;
282
283 if ( write(fh, buf, 9) != 9 )
284  return -1;
285
286 len = strlen(host);
287
288 if ( write(fh, host, len) != len )
289  return -1;
290
291 if ( write(fh, "\0", 1) != 1 )
292  return -1;
293
294 if ( read(fh, buf, 8) != 8 )
295  return -1;
296
297 if ( buf[1] != 0x5a )
298  return -1;
299
300 return 0;
301}
302
303//ll
Note: See TracBrowser for help on using the repository browser.