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