File: sockets.c

package info (click to toggle)
xt-toolbuslib 0.5.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 516 kB
  • ctags: 831
  • sloc: ansic: 5,255; sh: 361; makefile: 89
file content (425 lines) | stat: -rw-r--r-- 9,939 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

/*{{{  includes */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "toolbus.h" 
#include "terms.h"
#include "tools.h"
#include "utils.h"
#include "sockets.h"

/*}}}  */

int WellKnownLocalSocket = TB_ERROR;
int WellKnownGlobalSocket = TB_ERROR;
int WellKnownSocketPort;

static char * SOCKETFILE = NULL;

#define TB_SOCKET_CONNECT_DELAY 200

/*{{{  void tb_sleep(int sec, int usec)  */

void tb_sleep(int sec, int usec) 
{
  struct timeval tv;

  tv.tv_sec = sec;
  tv.tv_usec = usec;

  /* we use select to simulate a portable delay */
  select(0,NULL,NULL,NULL,&tv);

  return;
}

/*}}}  */

/*{{{  int getInt(int port, const char *tname) */

int getInt(int port, const char *tname)
{
  char buf[TB_MAX_HANDSHAKE], got_tool_name[256];
  int n, nread, nitems;

  if((nread = mread(port, buf, TB_MAX_HANDSHAKE)) < 0){
    err_sys_warn("getInt on port %d", port);
  }
  nitems = sscanf(buf,"%s %d",got_tool_name,&n);
  if(nitems != 2 || streq(tname, got_tool_name) == TBfalse)
    n = -1;
  if(TBverbose)
    TBmsg("getInt: got \"%s\", return: %d\n", buf, n);
  return n;
}

/*}}}  */
/*{{{  int putInt(int port, const char *tname, int n) */

int putInt(int port, const char *tname, int n)
{
  char buf[TB_MAX_HANDSHAKE];
  int res;
  sprintf(buf, "%s %d", tname, n);
  if(TBverbose)
    TBmsg("putInt(%d, %s, %d)\n", port, tname, n);
  if((res = write(port, buf, TB_MAX_HANDSHAKE)) < 0){
    err_sys_warn("putInt -- write error");
  }
  return res;
}

/*}}}  */

/*{{{  static void set_connect_options(int sock) */

static void set_connect_options(int sock)
{
  if((setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&sock, sizeof sock) < 0) && TBverbose)
    err_sys_warn("setsockopt TCP_NODELAY");

  if((setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, sizeof(int)) < 0) && TBverbose)
    err_sys_warn("setsockopt SO_SNDBUF");
}

/*}}}  */

/*{{{  void remove_socketfile() */

/* This gets called on termination of the ToolBus from bus_shutdown in main.c
*/

void remove_socketfile()
{
  unlink(SOCKETFILE);
}

/*}}}  */

/*{{{  static int connect_unix_socket(int port) */

static int connect_unix_socket(int port)
{
  int sock;
  char name[128];
  struct sockaddr_un usin;
  int attempts = 0;

  while(1) {
    int result;

    sprintf (name, "/var/tmp/%d", port);
    if((sock = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
      err_sys_fatal("cannot open socket");

    if(TBverbose)
      TBmsg("created AF_UNIX socket %d\n", sock);

    /* Initialize the socket address to the server's address. */
    memset((char *) &usin, 0, sizeof(usin));
    usin.sun_family = AF_UNIX;
    strcpy (usin.sun_path, name);

    /* Connect to the server. */
    if(TBverbose)
      TBmsg("connecting to address %s\n", name);
    result = connect(sock, (struct sockaddr *) &usin,sizeof(usin));
    if(result < 0) {
      close(sock);
      /* We want to delay a while, before we try again, there used
       * to be a warning message ('connect failed') that did this.
       */
      tb_sleep(0, TB_SOCKET_CONNECT_DELAY);
      if(attempts > 1000)
	err_sys_fatal("cannot connect, giving up");

      attempts++;
    } else { /* Connection established */
      chmod(name, 0777);
      set_connect_options(sock);
      return sock;
    }
  }
}

/*}}}  */
/*{{{  static int connect_inet_socket(const char *host, int port) */

static int connect_inet_socket(const char *host, int port)
{
  int sock;
  struct sockaddr_in isin;
  struct hostent *hp;
  int attempts = 0;

  while(1) {
    if((sock = socket(AF_INET,SOCK_STREAM,0)) < 0)
      err_sys_fatal("cannot open socket");

    if(TBverbose)
      TBmsg("created AF_INET socket %d\n", sock);

    /* Initialize the socket address to the server's address. */
    memset((char *) &isin, 0, sizeof(isin));
    isin.sin_family = AF_INET;

    /* to get host address */
    hp = gethostbyname(host);
    if(hp == NULL)
      err_sys_fatal("cannot get host name");

    memcpy (&(isin.sin_addr.s_addr), hp->h_addr, hp->h_length);
    isin.sin_port = htons(port);

    /* Connect to the server. */
    if(TBverbose)
      TBmsg("connecting to INET host %s, port %d\n", host, port);
    if(connect(sock, (struct sockaddr *)&isin, sizeof(isin)) < 0){
      close(sock);
      if(attempts > 1000)
	err_sys_fatal("cannot connect, giving up");
      attempts++;
    } else {
      set_connect_options(sock);
      return sock;
    }
  }
}

/*}}}  */
/*{{{  static int connect_socket (const char *host, int port) */

static int connect_socket (const char *host, int port)
{    
  if(host == NULL) {
    return connect_unix_socket(port);
  } else {
    return connect_inet_socket(host, port);
  }
}

/*}}}  */

/*{{{  static void set_create_options(int  socket) */

static void set_create_options(int  sock)
{
  int opt = 1;

#ifdef sgi
  /* Solaris doesn't know SO_REUSEPORT */
  if((setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char *)&opt, sizeof opt) < 0) && TBverbose)
    err_sys_warn("setsockopt SO_REUSEPORT");
#endif
  if((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof opt) < 0) && TBverbose)
    err_sys_warn("setsockopt SO_REUSEADDR");
  if((setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&buf_size, sizeof(int)) < 0) && TBverbose)
    err_sys_warn("setsockopt SO_RCVBUF");
}

/*}}}  */
/*{{{  static int create_unix_socket(int port) */

static int create_unix_socket(int port)
{
  struct sockaddr_un usin;
  char name[128] = "";
  int attempts = 0;
  int sock;

  sprintf (name, "/var/tmp/%d", port);
  unlink (name);
  SOCKETFILE = strdup(name);

  if((sock=socket(AF_UNIX,SOCK_STREAM,0)) < 0)
    err_sys_fatal("cannot open AF_UNIX stream socket");

  set_create_options(sock);

  /* Initialize socket's address structure */
  memset((char *) &usin, 0, sizeof(usin));
  usin.sun_family = AF_UNIX;

  strcpy (usin.sun_path, name);

  /* Assign an address to this socket */
  if(TBverbose)
    TBmsg("Binding %s\n", name);

  while(bind(sock,(struct sockaddr *)&usin,
	     strlen(usin.sun_path) +1 + sizeof(usin.sun_family)) < 0){
    if(attempts > 1000) {
      return TB_ERROR;
    }
    attempts++;
    chmod (name, 0777);
  }

  return sock; 
}

/*}}}  */
/*{{{  static int create_inet_socket(const char *host, int port) */

static int create_inet_socket(const char *host, int port)
{
  struct sockaddr_in isin;
  int attempts = 0;
  int sock;

  if((sock=socket(AF_INET,SOCK_STREAM,0)) < 0)
    err_sys_fatal("cannot open stream socket");

  set_create_options(sock);

  /* Initialize socket's address structure */
  memset((char *) &isin, 0, sizeof(isin));
  isin.sin_family = AF_INET;
  isin.sin_addr.s_addr = htonl(INADDR_ANY); /* htonl added */
  isin.sin_port = htons(port);

  while (bind (sock,(struct sockaddr *)&isin,sizeof(isin)) < 0){
    if (attempts > 1000) {
      return TB_ERROR;
    }
    attempts++;
  }

  return sock;
}

/*}}}  */
/*{{{  static int create_socket (const char *host, int port) */

static int create_socket (const char *host, int port)
{
  int sock;

  /* Create a socket */
  if (host == NULL)
    sock = create_unix_socket(port);
  else
    sock = create_inet_socket(host, port);

  /* Prepare socket queue for connect requests */
  if(sock < 0) {
    return TB_ERROR;
  }

  listen(sock,50);

  return sock;
}

/*}}}  */

/*{{{  void TBdestroyPort (int port) */

void TBdestroyPort (int port)
{
  close (port);
}

/*}}}  */
/*{{{  int createWellKnownSocket(char *host, int port) */

int createWellKnownSocket(char *host, int port)
{
  int sock = create_socket(host, port);
  if(TBverbose)
    TBmsg("createWellKnownSocket(%s,%d) returns %d\n", host ? host : "NULL", port, sock);
  return sock;
}

/*}}}  */
/*{{{  int connectWellKnownSocket(char *host, int port) */

int connectWellKnownSocket(char *host, int port)
{
  int sock = connect_socket(host, port);
  if(TBverbose)
    TBmsg("connectWellKnownSocket(%s,%d) returns %d\n", host ? host : "NULL", port, sock);
  return sock;
}

/*}}}  */

/*{{{  int accept_in_interval (int s, struct sockaddr *addr, int *addrlen) */

int accept_in_interval (int s, struct sockaddr *addr, int *addrlen)
{ int error, sock;
  fd_set read_template;
  struct timeval timeout = { 10, 0};

  FD_ZERO(&read_template);
  FD_SET(s,&read_template);
  if(( error = select(FD_SETSIZE, &read_template, NULL, NULL, &timeout)) > 0){
    if((sock = accept(s, addr, addrlen)) < 0){
      err_sys_warn("cannot accept_in_interval");
      return -1;
    } else
      return sock;
  }
  return -1;
}

/*}}}  */

/*{{{  int mk_server_ports(int local_port_only) */

int mk_server_ports(int local_port_only)
{
  int lsock = -1, gsock = -1;
  int attempts = 1000;
  int initialWellKnownSocketPort = WellKnownSocketPort;

  /* We try to create a socket, incrementing the port number if we fail. */
  do {

    if(!local_port_only) 
      gsock = createWellKnownSocket(this_host, WellKnownSocketPort);

    /* Only try to create the local port if the inet socket succeeded. 
     * The inet socket fails more frequently due some TCP bogy: some ports
     * stay unavailable for a long while after they are closed.
     */
    if(gsock >= 0 || local_port_only)
      lsock = createWellKnownSocket(NULL, WellKnownSocketPort);

    WellKnownSocketPort++;

  } while((lsock < 0 || (!local_port_only && gsock < 0)) && attempts--);

  if(!attempts)
    return TB_ERROR;

  /* Fix that the loop increments the port number in case of success */ 
  WellKnownSocketPort--;

  /* Commit the sockets */
  WellKnownLocalSocket = lsock;
  WellKnownGlobalSocket = gsock;

  if(initialWellKnownSocketPort != WellKnownSocketPort) {
    fprintf(stderr, "The toolbus server allocated port %d\n", WellKnownSocketPort);
  }

  return TB_OK;
}

/*}}}  */