]> code.citadel.org Git - citadel.git/blob - webcit/tcp_sockets.c
* Trace file using lprintf() similarly to citserver
[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 <arpa/inet.h>
23 #include <sys/un.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include "webcit.h"
32 #include "webserver.h"
33
34
35 #ifndef INADDR_NONE
36 #define INADDR_NONE 0xffffffff
37 #endif
38
39 RETSIGTYPE timeout(int signum)
40 {
41         lprintf(1, "Connection timed out.\n");
42         exit(3);
43 }
44
45
46
47 /*
48  * Connect a unix domain socket
49  */
50 int uds_connectsock(char *sockpath)
51 {
52         struct sockaddr_un addr;
53         int s;
54
55         memset(&addr, 0, sizeof(addr));
56         addr.sun_family = AF_UNIX;
57         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
58
59         s = socket(AF_UNIX, SOCK_STREAM, 0);
60         if (s < 0) {
61                 lprintf(1, "Can't create socket: %s\n",
62                         strerror(errno));
63                 return(-1);
64         }
65
66         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
67                 lprintf(1, "Can't connect: %s\n",
68                         strerror(errno));
69                 return(-1);
70         }
71
72         return s;
73 }
74
75
76 /*
77  * Connect a TCP/IP socket
78  */
79 int tcp_connectsock(char *host, char *service)
80 {
81         struct hostent *phe;
82         struct servent *pse;
83         struct protoent *ppe;
84         struct sockaddr_in sin;
85         int s;
86
87         memset(&sin, 0, sizeof(sin));
88         sin.sin_family = AF_INET;
89
90         pse = getservbyname(service, "tcp");
91         if (pse) {
92                 sin.sin_port = pse->s_port;
93         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
94                 lprintf(1, "Can't get %s service entry\n", service);
95                 return (-1);
96         }
97         phe = gethostbyname(host);
98         if (phe) {
99                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
100         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
101                 lprintf(1, "Can't get %s host entry: %s\n",
102                         host, strerror(errno));
103                 return (-1);
104         }
105         if ((ppe = getprotobyname("tcp")) == 0) {
106                 lprintf(1, "Can't get TCP protocol entry: %s\n",
107                         strerror(errno));
108                 return (-1);
109         }
110
111         s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
112         if (s < 0) {
113                 lprintf(1, "Can't create socket: %s\n", strerror(errno));
114                 return (-1);
115         }
116         signal(SIGALRM, timeout);
117         alarm(30);
118
119         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
120                 lprintf(1, "Can't connect to %s.%s: %s\n",
121                         host, service, strerror(errno));
122                 return (-1);
123         }
124         alarm(0);
125         signal(SIGALRM, SIG_IGN);
126
127         return (s);
128 }
129
130
131
132
133 /*
134  * Input binary data from socket
135  */
136 void serv_read(char *buf, int bytes)
137 {
138         int len, rlen;
139
140         len = 0;
141         while (len < bytes) {
142                 rlen = read(WC->serv_sock, &buf[len], bytes - len);
143                 if (rlen < 1) {
144                         lprintf(1, "Server connection broken: %s\n",
145                                 strerror(errno));
146                         WC->connected = 0;
147                         WC->logged_in = 0;
148                         return;
149                 }
150                 len = len + rlen;
151         }
152 }
153
154
155 /*
156  * input string from pipe
157  */
158 void serv_gets(char *strbuf)
159 {
160         int ch, len;
161         char buf[2];
162
163         len = 0;
164         strcpy(strbuf, "");
165         do {
166                 serv_read(&buf[0], 1);
167                 ch = buf[0];
168                 strbuf[len++] = ch;
169         } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
170         strbuf[len - 1] = 0;
171         /* lprintf(9, ">%s\n", strbuf); */
172 }
173
174
175
176 /*
177  * send binary to server
178  */
179 void serv_write(char *buf, int nbytes)
180 {
181         int bytes_written = 0;
182         int retval;
183         while (bytes_written < nbytes) {
184                 retval = write(WC->serv_sock, &buf[bytes_written],
185                                nbytes - bytes_written);
186                 if (retval < 1) {
187                         lprintf(1, "Server connection broken: %s\n",
188                                 strerror(errno));
189                         WC->connected = 0;
190                         WC->logged_in = 0;
191                         return;
192                 }
193                 bytes_written = bytes_written + retval;
194         }
195 }
196
197
198 /*
199  * send line to server
200  */
201 void serv_puts(char *string)
202 {
203         char buf[SIZ];
204
205         sprintf(buf, "%s\n", string);
206         serv_write(buf, strlen(buf));
207 }
208
209
210 /*
211  * convenience function to send stuff to the server
212  */
213 void serv_printf(const char *format,...)
214 {
215         va_list arg_ptr;
216         char buf[SIZ];
217
218         va_start(arg_ptr, format);
219         vsprintf(buf, format, arg_ptr);
220         va_end(arg_ptr);
221
222         strcat(buf, "\n");
223         serv_write(buf, strlen(buf));
224         /* lprintf(9, "<%s", buf); */
225 }