3c46e1586d9188a414219eaa9aa5594c584b71bd
[condor-woldlab.git] / src / castor_load.c
1 #define _POSIX_SOURCE 1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netdb.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11
12 #define BUF_SIZE 512
13
14 int main(int argc, char **argv) {
15   int sock = 0;
16   int rv = 0;
17   int nread = 0;
18   struct addrinfo hints;
19   struct addrinfo *address;
20   struct addrinfo *addr_ptr;
21   char buf[BUF_SIZE];
22   
23   memset(buf, 0, BUF_SIZE);
24   memset(&hints, 0, sizeof(struct addrinfo));
25   hints.ai_family = AF_INET;
26   hints.ai_socktype = SOCK_STREAM;
27
28   
29   rv = getaddrinfo("castor-pvt", "1210", &hints, &address);
30   if (rv != 0) {
31     fprintf(stderr, "Error looking up hostname, %d\n", errno);
32     exit(-1);
33   }
34
35   
36   for (addr_ptr = address; addr_ptr != NULL; addr_ptr = addr_ptr->ai_next) {
37     sock = socket(addr_ptr->ai_family, 
38                   addr_ptr->ai_socktype,
39                   addr_ptr->ai_protocol);
40     if (sock == -1) {
41       continue;
42     }
43     
44     rv = connect(sock, addr_ptr->ai_addr, addr_ptr->ai_addrlen);
45     if (rv != -1) {
46       break;
47     }
48     close(sock);
49   }
50   if (addr_ptr == NULL) {
51     fprintf(stderr, "Could not connect\n");
52     exit(-1);
53   }
54
55   freeaddrinfo(address);
56
57   nread = read(sock, buf, BUF_SIZE-1);
58   if (nread == -1) {
59     fprintf(stderr, "read error");
60     exit(-1);
61   }
62   fprintf(stdout, "castor_load = %s", buf);
63   fprintf(stdout, "--\n");
64
65   exit(0);
66 }