webcit_before_automake is now the trunk
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * $Id$
3  */
4 /** 
5  * \defgroup TcpSockets TCP client socket module for WebCit
6  * \ingroup CitadelCommunitacion
7  */
8 /*@{*/
9
10 /*
11  * Uncomment this to log all communications with the Citadel server
12 #define SERV_TRACE 1
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17
18 /**
19  * \brief register the timeout
20  * \param signum signalhandler number
21  * \return signals
22  */
23 RETSIGTYPE timeout(int signum)
24 {
25         lprintf(1, "Connection timed out.\n");
26         exit(3);
27 }
28
29
30 /**
31  * \brief Connect a unix domain socket
32  * \param sockpath where to open a unix domain socket
33  */
34 int uds_connectsock(char *sockpath)
35 {
36         struct sockaddr_un addr;
37         int s;
38
39         memset(&addr, 0, sizeof(addr));
40         addr.sun_family = AF_UNIX;
41         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
42
43         s = socket(AF_UNIX, SOCK_STREAM, 0);
44         if (s < 0) {
45                 lprintf(1, "Can't create socket: %s\n",
46                         strerror(errno));
47                 return(-1);
48         }
49
50         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
51                 lprintf(1, "Can't connect: %s\n",
52                         strerror(errno));
53                 close(s);
54                 return(-1);
55         }
56
57         return s;
58 }
59
60
61 /**
62  * \brief Connect a TCP/IP socket
63  * \param host the host to connect to
64  * \param service the service on the host to call
65  */
66 int tcp_connectsock(char *host, char *service)
67 {
68         struct hostent *phe;
69         struct servent *pse;
70         struct protoent *ppe;
71         struct sockaddr_in sin;
72         int s;
73
74         memset(&sin, 0, sizeof(sin));
75         sin.sin_family = AF_INET;
76
77         pse = getservbyname(service, "tcp");
78         if (pse) {
79                 sin.sin_port = pse->s_port;
80         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
81                 lprintf(1, "Can't get %s service entry\n", service);
82                 return (-1);
83         }
84         phe = gethostbyname(host);
85         if (phe) {
86                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
87         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
88                 lprintf(1, "Can't get %s host entry: %s\n",
89                         host, strerror(errno));
90                 return (-1);
91         }
92         if ((ppe = getprotobyname("tcp")) == 0) {
93                 lprintf(1, "Can't get TCP protocol entry: %s\n",
94                         strerror(errno));
95                 return (-1);
96         }
97
98         s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
99         if (s < 0) {
100                 lprintf(1, "Can't create socket: %s\n", strerror(errno));
101                 return (-1);
102         }
103         signal(SIGALRM, timeout);
104         alarm(30);
105
106         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
107                 lprintf(1, "Can't connect to %s.%s: %s\n",
108                         host, service, strerror(errno));
109                 close(s);
110                 return (-1);
111         }
112         alarm(0);
113         signal(SIGALRM, SIG_IGN);
114
115         return (s);
116 }
117
118
119
120
121 /**
122  * \brief Input binary data from socket
123  * \param buf the buffer to get the input to
124  * \param bytes the maximal number of bytes to read
125  */
126 void serv_read(char *buf, int bytes)
127 {
128         int len, rlen;
129
130         len = 0;
131         while (len < bytes) {
132                 rlen = read(WC->serv_sock, &buf[len], bytes - len);
133                 if (rlen < 1) {
134                         lprintf(1, "Server connection broken: %s\n",
135                                 strerror(errno));
136                         close(WC->serv_sock);
137                         WC->serv_sock = (-1);
138                         WC->connected = 0;
139                         WC->logged_in = 0;
140                         memset(buf, 0, bytes);
141                         return;
142                 }
143                 len = len + rlen;
144         }
145 }
146
147
148 /**
149  * \brief input string from pipe
150  */
151 void serv_getln(char *strbuf, int bufsize)
152 {
153         int ch, len;
154         char buf[2];
155
156         len = 0;
157         strbuf[0] = 0;
158         do {
159                 serv_read(&buf[0], 1);
160                 ch = buf[0];
161                 if ((ch != 13) && (ch != 10)) {
162                         strbuf[len++] = ch;
163                 }
164         } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
165         strbuf[len] = 0;
166 #ifdef SERV_TRACE
167         lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
168 #endif
169 }
170
171
172
173 /**
174  * \brief send binary to server
175  * \param buf the buffer to write to citadel server
176  * \param nbytes how many bytes to send to citadel server
177  */
178 void serv_write(char *buf, int nbytes)
179 {
180         int bytes_written = 0;
181         int retval;
182         while (bytes_written < nbytes) {
183                 retval = write(WC->serv_sock, &buf[bytes_written],
184                                nbytes - bytes_written);
185                 if (retval < 1) {
186                         lprintf(1, "Server connection broken: %s\n",
187                                 strerror(errno));
188                         close(WC->serv_sock);
189                         WC->serv_sock = (-1);
190                         WC->connected = 0;
191                         WC->logged_in = 0;
192                         return;
193                 }
194                 bytes_written = bytes_written + retval;
195         }
196 }
197
198
199 /**
200  * \brief send line to server
201  * \param string the line to send to the citadel server
202  */
203 void serv_puts(char *string)
204 {
205         char buf[SIZ];
206
207 #ifdef SERV_TRACE
208         lprintf(9, "%3d<%s\n", WC->serv_sock, string);
209 #endif
210         sprintf(buf, "%s\n", string);
211         serv_write(buf, strlen(buf));
212 }
213
214
215 /**
216  * \brief convenience function to send stuff to the server
217  * \param format the formatstring
218  * \param ... the entities to insert into format 
219  */
220 void serv_printf(const char *format,...)
221 {
222         va_list arg_ptr;
223         char buf[SIZ];
224
225         va_start(arg_ptr, format);
226         vsnprintf(buf, sizeof buf, format, arg_ptr);
227         va_end(arg_ptr);
228
229         strcat(buf, "\n");
230         serv_write(buf, strlen(buf));
231 #ifdef SERV_TRACE
232         lprintf(9, "<%s", buf);
233 #endif
234 }
235
236
237 /*@}*/