source: trunk/athena/bin/xcluster/net.c @ 22709

Revision 22709, 2.2 KB checked in by ghudson, 17 years ago (diff)
* athena/xcluster/net.c: Fall back to hardcoded port if getservbyname fails. Patch by broder.
Line 
1#include <stdio.h>
2#include <string.h>
3#include <sys/types.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <netdb.h>
7#include <hesiod.h>
8
9#define CVIEW_FALLBACK_PORT 3704
10
11int net(progname, num, names)
12     char *progname;
13     int num;
14     char **names;
15{
16  static char *host;            /* name of host running cview daemon. */
17  static char **serv;           /* cview service hesiod info. */
18  static struct hostent *hp;    /* hostent struct for cview server. */
19  static struct servent *sp;    /* servent struct for cview service. */
20  static struct sockaddr_in sin; /* Socket address. */
21  static int init = 0;          /* Have we been here before? */
22  int s;                        /* Socket to connect to cview daemon. */
23  int j;                        /* Counter. */
24  char buf[BUFSIZ];             /* Temporary buffer. */
25
26  if (!init)
27    {
28#ifdef TEST
29      host = "fries.mit.edu";
30#else
31      serv = hes_resolve("cview","sloc");
32
33      if (serv == NULL)
34        host = "doghouse.mit.edu";      /* fall back if hesiod is broken... */
35      else
36        host = *serv;
37#endif
38
39      hp = gethostbyname(host);
40
41      if (hp == NULL)
42        {
43          fprintf(stderr, "%s: Unable to resolve hostname '%s'.\n",
44                  progname, host);
45          return(-1);
46        }
47 
48      if (hp->h_length != sizeof(sin.sin_addr))
49        {
50          fprintf(stderr, "%s: Unexpected h_length value for '%s'.\n",
51                  progname, host);
52          return(-1);
53        }
54
55      sp = getservbyname("cview", "tcp");
56
57      memset(&sin, 0, sizeof (sin));
58      memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
59      sin.sin_family = hp->h_addrtype;
60      sin.sin_port = (sp) ? sp->s_port : htons(CVIEW_FALLBACK_PORT);
61
62      init = 1;
63    }
64 
65  s = socket(hp->h_addrtype, SOCK_STREAM, 0);
66  if (s < 0)
67    {
68      perror("socket");
69      return(-1);
70    }
71
72  if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
73    {
74      perror("connect");
75      close(s);
76      return(-1);
77    }
78
79  for (j=0; j<num; j++)
80    {
81      strncpy(buf, *names, sizeof(buf) - 2);
82      buf[sizeof(buf) - 2] = '\0';
83      strcat(buf, " ");
84      if (write(s, buf, strlen(buf)) == -1)
85        {
86          perror("write");
87          close(s);
88          return(-1);
89        }
90      names++;
91    }
92
93  if (write(s, "\n", 1) == -1)
94    {
95      perror("write");
96      close(s);
97      return(-1);
98    }
99
100  return(s);
101}
Note: See TracBrowser for help on using the repository browser.