source: roaraudio/libroar/socket.c @ 70:58a327d2e216

Last change on this file since 70:58a327d2e216 was 70:58a327d2e216, checked in by phi, 16 years ago

closing STDIN/STDOUT on fork

File size: 6.3 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  close(ROAR_STDIN ); // we do not want roard to have any standard input
164  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
165                      // but STDERR we keep open for error messages.
166
167  snprintf(fhstr, 7, "%i", socks[1]);
168
169  execlp("roard", "roard", "--no-listen", "--client-fh", fhstr, NULL);
170
171  // we are still alive?
172  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!");
173  _exit(1);
174 } else { // we are the parent
175  close(socks[1]);
176  return socks[0];
177 }
178
179 return -1;
180}
181
182// --- [ PROXY CODE ] ---
183
184// generic proxy code:
185
186int roar_socket_open_proxy (int mode, int type, char * host, int port, char * proxy_type) {
187 int    proxy_port;
188 char   proxy_host[ROAR_SOCKET_MAX_HOSTNAMELEN];
189 char * proxy_addr;
190 int    i;
191 int    fh;
192
193 // TODO: change this so we support listen() proxys (ssh -R)
194 if ( mode != MODE_CONNECT )
195  return -1;
196
197 if ( !strcmp(proxy_type, "socks4a") ) { // for TOR, the only supported type at the moment
198  proxy_addr = getenv("socks_proxy");
199
200  proxy_port = 9050; // TOR's default port
201
202  if ( proxy_addr == NULL )
203   return -1;
204
205  for (i = 0; proxy_addr[i] != 0 && proxy_addr[i] != ':' && i < ROAR_SOCKET_MAX_HOSTNAMELEN; i++)
206   proxy_host[i] = proxy_addr[i];
207  proxy_host[i] = 0;
208
209  if ( i == 0 ) // no hostname found
210   return -1;
211
212  if ( proxy_addr[i] == ':' )
213   proxy_port = atoi(&proxy_addr[i+1]);
214
215  if ( (fh = roar_socket_open(mode, type, proxy_host, proxy_port)) == -1) {
216   return -1;
217  }
218
219  if ( roar_socket_open_socks4a(mode, fh, host, port) == -1 ) {
220   close(fh);
221   return -1;
222  }
223
224  return fh;
225 } else {
226  return -1; // unknown type
227 }
228}
229
230// protocoll dependet proxy code:
231
232int roar_socket_open_socks4a(int mode, int fh, char * host, int port) {
233 char buf[9];
234 int  len;
235
236 buf[0] = 0x04;
237 buf[1] = mode == MODE_CONNECT ? 0x01 : 0x02;
238 *((uint16_t*)&buf[2]) = htons(port);
239 buf[4] = 0x00;
240 buf[5] = 0x00;
241 buf[6] = 0x00;
242 buf[7] = 0x01;
243 buf[8] = 0x00;
244
245 if ( write(fh, buf, 9) != 9 )
246  return -1;
247
248 len = strlen(host);
249
250 if ( write(fh, host, len) != len )
251  return -1;
252
253 if ( write(fh, "\0", 1) != 1 )
254  return -1;
255
256 if ( read(fh, buf, 8) != 8 )
257  return -1;
258
259 if ( buf[1] != 0x5a )
260  return -1;
261
262 return 0;
263}
264
265//ll
Note: See TracBrowser for help on using the repository browser.