1    | #include <sys/types.h>
2    | #include <sys/socket.h>
3    | #include <netinet/in.h>
4    | #include <netdb.h>
5    | #include <stdio.h>
6    | 
7    | #include <erroutines.h>
8    | 
9    | 
10   | int
11   | WH_sock(int sock, char *hostname, int port, 
12   | 	char *query, int maxlines, int timeout)
13   | {
14   |   char log_str[256];
15   |   FILE *sfi;
16   |   FILE *sfo;
17   |   struct sockaddr_in sin;
18   |   struct hostent *hp;
19   |   int ch;
20   |   int s;
21   |   
22   |   sprintf(log_str, "would perform query >%s< to %s:%d  \n"
23   | 	  "limits: line %d tmout %d and print on socket %d\n",
24   | 	  query,hostname,port, maxlines,timeout,sock );
25   |   log_inst_print(log_str);
26   |   
27   |   if ( (hp = gethostbyname(hostname)) == NULL) {
28   |     return WH_BADHOST;
29   |   }
30   | 
31   |   s = socket(AF_INET, SOCK_STREAM, 0);
32   |   if (s < 0) {
33   |     return WH_SOCKET;
34   |   }
35   |   
36   |   bzero((caddr_t)&sin, sizeof (sin));
37   |   sin.sin_family = hp->h_addrtype;
38   |   if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
39   |     close(s);
40   |     return WH_BIND;
41   |   }
42   |   bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
43   |   sin.sin_port=port;
44   | 
45   |   if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
46   |     close(s);
47   |     return WH_CONNECT;
48   |   }
49   | 
50   | #if 1
51   |   sfi = fdopen(s, "r");
52   |   sfo = fdopen(s, "w");
53   |   if (sfi == NULL || sfo == NULL) {
54   |     (void)close(s);
55   |     return WH_OPEN;
56   |   }
57   | 
58   |   fprintf(sfo, "%s\r\n", query);
59   |   fflush(sfo);
60   | 
61   |   while ((ch = getc(sfi)) != EOF) {
62   |     int ret = SK_putc(sock,ch);
63   |   }
64   | 
65   |   fclose(sfo);
66   |   fclose(sfi);
67   | #else
68   |   SK_puts(s, query);
69   |   SK_puts(s, "\r\n");
70   | 
71   |   while( (ch = SK_getc(s)) != EOF ) {
72   |     SK_putc(sock,ch);
73   |   }
74   | #endif
75   |   close(s);
76   | }
77   |