source: roaraudio/libroar/socket.c @ 69:5ef65a9359b3

Last change on this file since 69:5ef65a9359b3 was 69:5ef65a9359b3, checked in by phi, 16 years ago

added support to fork a roard instance in background

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