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