* Started moving all of the global variables into a struct, to facilitate
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * tcp_sockets.c
3  * 
4  * TCP socket module for WebCit
5  *
6  * $Id$
7  */
8
9 #include <stdlib.h>
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #include <stdio.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <stdarg.h>
22 #include "webcit.h"
23
24 char server_is_local = 0;
25
26 #ifndef INADDR_NONE
27 #define INADDR_NONE 0xffffffff
28 #endif
29
30 extern int errno;
31
32 RETSIGTYPE timeout(int signum)
33 {
34         fprintf(stderr, "Connection timed out.\n");
35         exit(3);
36 }
37
38 int connectsock(char *host, char *service, char *protocol)
39 {
40         struct hostent *phe;
41         struct servent *pse;
42         struct protoent *ppe;
43         struct sockaddr_in sin;
44         int s, type;
45
46         bzero((char *) &sin, sizeof(sin));
47         sin.sin_family = AF_INET;
48
49         pse = getservbyname(service, protocol);
50         if (pse) {
51                 sin.sin_port = pse->s_port;
52         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
53                 fprintf(stderr, "Can't get %s service entry\n", service);
54                 return (-1);
55         }
56         phe = gethostbyname(host);
57         if (phe) {
58                 bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
59         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
60                 fprintf(stderr, "Can't get %s host entry: %s\n",
61                         host, strerror(errno));
62                 return (-1);
63         }
64         if ((ppe = getprotobyname(protocol)) == 0) {
65                 fprintf(stderr, "Can't get %s protocol entry: %s\n",
66                         protocol, strerror(errno));
67                 return (-1);
68         }
69         if (!strcmp(protocol, "udp"))
70                 type = SOCK_DGRAM;
71         else
72                 type = SOCK_STREAM;
73
74         s = socket(PF_INET, type, ppe->p_proto);
75         if (s < 0) {
76                 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
77                 return (-1);
78         }
79         signal(SIGALRM, timeout);
80         alarm(30);
81
82         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
83                 fprintf(stderr, "can't connect to %s.%s: %s\n",
84                         host, service, strerror(errno));
85                 return (-1);
86         }
87         alarm(0);
88         signal(SIGALRM, SIG_IGN);
89
90         return (s);
91 }
92
93
94
95
96 /*
97  * Input binary data from socket
98  */
99 void serv_read(char *buf, int bytes)
100 {
101         int len, rlen;
102
103         len = 0;
104         while (len < bytes) {
105                 rlen = read(WC->serv_sock, &buf[len], bytes - len);
106                 if (rlen < 1) {
107                         fprintf(stderr, "Server connection broken: %s\n",
108                                 strerror(errno));
109                         WC->connected = 0;
110                         WC->logged_in = 0;
111                         return;
112                 }
113                 len = len + rlen;
114         }
115 }
116
117
118 /*
119  * input string from pipe
120  */
121 void serv_gets(char *strbuf)
122 {
123         int ch, len;
124         char buf[2];
125
126         len = 0;
127         strcpy(strbuf, "");
128         do {
129                 serv_read(&buf[0], 1);
130                 ch = buf[0];
131                 strbuf[len++] = ch;
132         } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
133         strbuf[len - 1] = 0;
134         /* fprintf(stderr, ">%s\n", strbuf); */
135 }
136
137
138
139 /*
140  * send binary to server
141  */
142 void serv_write(char *buf, int nbytes)
143 {
144         int bytes_written = 0;
145         int retval;
146         while (bytes_written < nbytes) {
147                 retval = write(WC->serv_sock, &buf[bytes_written],
148                                nbytes - bytes_written);
149                 if (retval < 1) {
150                         fprintf(stderr, "Server connection broken: %s\n",
151                                 strerror(errno));
152                         WC->connected = 0;
153                         WC->logged_in = 0;
154                         return;
155                 }
156                 bytes_written = bytes_written + retval;
157         }
158 }
159
160
161 /*
162  * send line to server
163  */
164 void serv_puts(char *string)
165 {
166         char buf[256];
167
168         sprintf(buf, "%s\n", string);
169         serv_write(buf, strlen(buf));
170 }
171
172
173 /*
174  * convenience function to send stuff to the server
175  */
176 void serv_printf(const char *format,...)
177 {
178         va_list arg_ptr;
179         char buf[256];
180
181         va_start(arg_ptr, format);
182         vsprintf(buf, format, arg_ptr);
183         va_end(arg_ptr);
184
185         strcat(buf, "\n");
186         serv_write(buf, strlen(buf));
187         /* fprintf(stderr, "<%s", buf); */
188 }