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 | #if 0
23 | sprintf(log_str, "would perform query >%s< to %s:%d \n"
24 | "limits: line %d tmout %d and print on socket %d\n",
25 | query,hostname,port, maxlines,timeout,sock );
26 | log_inst_print(log_str);
27 | #endif
28 |
29 | if ( (hp = gethostbyname(hostname)) == NULL) {
30 | return WH_BADHOST;
31 | }
32 |
33 | s = socket(AF_INET, SOCK_STREAM, 0);
34 | if (s < 0) {
35 | return WH_SOCKET;
36 | }
37 |
38 | bzero((caddr_t)&sin, sizeof (sin));
39 | sin.sin_family = hp->h_addrtype;
40 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
41 | close(s);
42 | return WH_BIND;
43 | }
44 | bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
45 | sin.sin_port=port;
46 |
47 | SK_puts(sock, "% connecting to a remote referral site...\n");
48 |
49 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
50 | close(s);
51 | return WH_CONNECT;
52 | }
53 |
54 | SK_puts(sock, "% Connection established...\n");
55 |
56 | #if 1
57 | sfi = fdopen(s, "r");
58 | sfo = fdopen(s, "w");
59 | if (sfi == NULL || sfo == NULL) {
60 | (void)close(s);
61 | return WH_OPEN;
62 | }
63 |
64 | fprintf(sfo, "%s\r\n", query);
65 | fflush(sfo);
66 |
67 | while ((ch = getc(sfi)) != EOF) {
68 | int ret = SK_putc(sock,ch);
69 | }
70 |
71 | fclose(sfo);
72 | fclose(sfi);
73 | #else
74 | SK_puts(s, query);
75 | SK_puts(s, "\r\n");
76 |
77 | while( (ch = SK_getc(s)) != EOF ) {
78 | SK_putc(sock,ch);
79 | }
80 | #endif
81 | close(s);
82 | }
83 |