aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.c
blob: 9ad122bc1fa58b2df29d4cb72800799678672ece (plain)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*@@
   @file      Utils.c
   @date      1991
   @author    John Shalf
   @desc 
   Socket stuff provided by John Shalf
   @enddesc
   @history
   @hdate Thu May 25 13:45:29 2000 @hauthor Tom Goodale
   @hdesc Moved this file into Cactus
   @endhistory 
   @version $Header$
 @@*/

#include "cctk.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/errno.h>
#include <arpa/inet.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "SocketUtils.h"

static char *rcsid = "$Header$";
CCTK_FILEVERSION(Development_Socket)


/* #if defined(ANSI) || defined(__STDC__) */
#ifdef __STDC__
/*
u_long htonl(u_long hostlong);
u_short htons(u_short hostshort);
u_long ntohl(u_long netlong);
u_short ntohs(u_short netshort);
 
int inet_aton(const char *cp, struct in_addr *pin);
unsigned long inet_addr(const char *cp);
unsigned long inet_network(const char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int lna);
unsigned long inet_lnaof(struct in_addr in);
unsigned long inet_netof(struct in_addr in);
*/
#else
u_short htons(); 
u_short ntohs();
u_long inet_addr();
#endif

extern int errno;

 /*@@
   @routine    Socket_TCPOpenClientSock
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_TCPOpenClientSock(const char *hostname,int port)
{
  struct hostent *phe;
  /* struct servent *pse; */
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s /*,type*/;
        
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr=INADDR_ANY;
  sin.sin_port = htons((u_short)port);

  if((phe=gethostbyname(hostname)) != NULL)
  {
    memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
  }
  else if((sin.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE)
  {
    fprintf(stderr,"can\'t find host %s \n",hostname);
  }
  if((ppe=getprotobyname("tcp")) == NULL)
  {
    perror("can\'t find tcp protocol\n");
    return -1;
  }
  s=socket(PF_INET,SOCK_STREAM,ppe->p_proto);
  if(s<0)
  {
    perror("");
    fprintf(stderr,"couldn\'t allocate socket on host %s: port %u\n",hostname,port);
    return s;
  }
  if(connect(s,(struct sockaddr *)&sin, sizeof(sin)) < 0)
  {
    perror("");
    fprintf(stderr,"couldn\'t connect to host %s: port %u\n",hostname,port);
    close (s);
    return -1;
  }
  return s;
}

/*@@
   @routine    Socket_UDPOpenClientSock
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_UDPOpenClientSock(const char *hostname,int port)
{
  struct hostent *phe;
  /*        struct servent *pse; */
  struct protoent *ppe;
  struct sockaddr_in sin;
  int sock/*,type*/;
 
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr=INADDR_ANY;
  sin.sin_port = htons((u_short)port);

  if((phe=gethostbyname(hostname)) != NULL)
  {
    memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
  }
  else if((sin.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE)
  {
    fprintf(stderr,"can\'t find host %s \n",hostname);
  }
  if((ppe=getprotobyname("udp")) == 0)
  {
    perror("can\'t find udp protocol\n");
    return -1;
  }
  sock=socket(PF_INET,SOCK_DGRAM,ppe->p_proto);
  if(sock<0)
  {
    perror("");
    fprintf(stderr,"couldn\'t allocate socket on host %s: port %u\n",hostname,port);
    return sock;
  }
  if(bind(sock,(struct sockaddr *)&sin,sizeof(sin)) <0)
  {
    close(sock);
    perror("client: bind failed");
    return -1;
  }


  /*        if(connect(s,(struct sockaddr *)&sin, sizeof(sin)) < 0)
            {`
            perror("");
            fprintf(stderr,"couldn\'t connect to host %s: port %u\n",hostname,port);
            return -1;
            }
  */
  return sock;
}

/*@@
   @routine    Socket_TCPOpenServerSock
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_TCPOpenServerSock(int port)
{
  /*    struct servent *pse; */
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s/*,type*/;
  const int on = 1;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons((u_short)port);

  if((ppe=getprotobyname("tcp")) == NULL)
  {
    fprintf (stderr, "can\'t find tcp protocol: %s\n", strerror (errno));
    return -1;
  }
  s=socket(PF_INET,SOCK_STREAM,ppe->p_proto);
  if(s<0)
  {
    fprintf (stderr, "couldn\'t create socket on port %u: %s\n",
             port, strerror (errno));
    return s;
  }
  if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0)
  {
    fprintf (stderr, "couldn\'t set SO_REUSEADDR to port %u: %s\n",
             port, strerror (errno));
    close(s);
    return -1;
  }
  if(bind(s,(struct sockaddr *)&sin, sizeof(sin)) < 0)
  {
    fprintf (stderr, "couldn\'t bind to port %u: %s\n", port, strerror (errno));
    close(s);
    return -1;
  }
  if(listen(s,5) < 0) /* note, server connection qlen fixed to 5 */
  {
    fprintf (stderr, "couldn\'t listen on port %u: %s\n", port, strerror (errno));
  }
  return s;
}

/*@@
   @routine    Socket_UDPOpenServerSock
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_UDPOpenServerSock(int port)
{
  /* struct servent *pse; */
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s/*,type*/;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons((u_short)port);

  if((ppe=getprotobyname("udp")) == 0)
  {
    perror("can\'t find udp protocol\n");
    return -1;
  }
  s=socket(PF_INET,SOCK_DGRAM,ppe->p_proto);
  if(s<0)
  {
    fprintf(stderr,"couldn\'t create socket on port %u\n",port);
    return s;
  }
  if(bind(s,(struct sockaddr *)&sin, sizeof(sin)) < 0)
  {
    fprintf(stderr,"couldn\'t bind to port %u\n",port);
    close(s);
    return -1;
  }
  return s;
}

/*@@
   @routine    Socket_TCPBlockingWrite
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_TCPBlockingWrite(int fd,char *buffer, int buflen)
{
  register int n;
  int nstore;
  n=write(fd,buffer,buflen);

  if(n>=0)
  {
    return n;
  }
  else
  { 
    switch(errno) /* note use of global variable errno */
    {
      case EBADF:
#ifdef PERRORS
        perror("invalid file descriptor");
#endif
        break;
      case EPIPE:
#ifdef PERRORS
        perror("attemped to write to an unconnected socket");
#endif
        break;
      case EFBIG:
#ifdef PERRORS
        perror("datasize too large to write.");
        perror("will attempt recovery by sending in smaller pieces");
#endif
        /* subdivide buffer and call TCPBlockingWrite() recursively */
        nstore=n; /* preserve register variable */
        Socket_TCPBlockingWrite(fd,buffer,buflen>>1);
        Socket_TCPBlockingWrite(fd,buffer+(buflen>>1),buflen-(buflen>>1));
        n=nstore; /* restore register variable */
        break;
      case EFAULT:
#ifdef PERRORS
        perror("invalid buffer address");
#endif
        break;
      case EINVAL:
#ifdef PERRORS
        perror("file descriptor points to unusable device.");
#endif
        break;
      case EIO:
#ifdef PERRORS
        perror("an io error occured");
#endif
        break;
      case EWOULDBLOCK:
#ifdef PERRORS
        perror("Non-blocking I/O is specified by ioctl");
        perror("but socket has no data (would have blocked).");
#endif
        break;
    }
  }
  return n; /* default, don't know what happened */
}

/*@@
   @routine    Socket_TCPBlockingRead
   @date       1991
   @author     John Shalf
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int Socket_TCPBlockingRead(int fd,char *buffer, int buflen)
{
  /* do errno only if types.h has been included */
  /* how can I tell if this has been included ? */
  register int n,accum=0;
  while((n=read(fd,buffer,buflen)) > 0)
  {
    if(n>=0)
    {
      buffer = buffer + n;
      buflen -= n;
      accum+=n;
    }
  }
#ifdef PERRORS
  if(n<0) 
  {
    switch(errno) /* note use of global variable errno */
    {
      case EBADF:
#ifdef PERRORS
        perror("invalid file descriptor");
#endif
        break;
      case EFAULT:
#ifdef PERRORS
        perror("invalid buffer address");
#endif
        break;
      case EINTR:
#ifdef PERRORS
        perror("operation interrupted by a signal");
        perror("disable signals and try again");
#endif
        break;
      case EWOULDBLOCK:
#ifdef PERRORS
        perror("Non-blocking I/O is specified by ioctl");
        perror("but socket has no data (would have blocked).");
#endif
        break;
    }
  }
#endif

  if(n<0)
  { 
    return n;
  }
  else
  {
    return accum;
  }
}

int Socket_SetNonBlocking(int sock)
{
  int retval,ierr;
  ierr = fcntl (sock, F_SETFL, O_NONBLOCK);
  
  if (ierr<0) 
    retval = -1;
  else
    retval =  0;

  return(retval);
}