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

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