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