5 * \defgroup TcpSockets TCP client socket module for WebCit
6 * \ingroup CitadelCommunitacion
11 * Uncomment this to log all communications with the Citadel server
17 #include "webserver.h"
20 * \brief register the timeout
21 * \param signum signalhandler number
24 RETSIGTYPE timeout(int signum)
26 lprintf(1, "Connection timed out.\n");
32 * \brief Connect a unix domain socket
33 * \param sockpath where to open a unix domain socket
35 int uds_connectsock(char *sockpath)
37 struct sockaddr_un addr;
40 memset(&addr, 0, sizeof(addr));
41 addr.sun_family = AF_UNIX;
42 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
44 s = socket(AF_UNIX, SOCK_STREAM, 0);
46 lprintf(1, "Can't create socket: %s\n",
51 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
52 lprintf(1, "Can't connect: %s\n",
63 * \brief Connect a TCP/IP socket
64 * \param host the host to connect to
65 * \param service the service on the host to call
67 int tcp_connectsock(char *host, char *service)
72 struct sockaddr_in sin;
75 memset(&sin, 0, sizeof(sin));
76 sin.sin_family = AF_INET;
78 pse = getservbyname(service, "tcp");
80 sin.sin_port = pse->s_port;
81 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
82 lprintf(1, "Can't get %s service entry\n", service);
85 phe = gethostbyname(host);
87 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
88 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
89 lprintf(1, "Can't get %s host entry: %s\n",
90 host, strerror(errno));
93 if ((ppe = getprotobyname("tcp")) == 0) {
94 lprintf(1, "Can't get TCP protocol entry: %s\n",
99 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
101 lprintf(1, "Can't create socket: %s\n", strerror(errno));
104 signal(SIGALRM, timeout);
107 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
108 lprintf(1, "Can't connect to %s.%s: %s\n",
109 host, service, strerror(errno));
114 signal(SIGALRM, SIG_IGN);
123 * \brief Input binary data from socket
124 * \param buf the buffer to get the input to
125 * \param bytes the maximal number of bytes to read
127 inline void _serv_read(char *buf, int bytes, struct wcsession *WCC)
132 while (len < bytes) {
133 rlen = read(WCC->serv_sock, &buf[len], bytes - len);
135 lprintf(1, "Server connection broken: %s\n",
138 close(WCC->serv_sock);
139 WCC->serv_sock = (-1);
142 memset(buf, 0, bytes);
149 void serv_read(char *buf, int bytes)
151 struct wcsession *WCC = WC;
152 _serv_read(buf, bytes, WCC);
156 * \brief input string from pipe
158 int serv_getln(char *strbuf, int bufsize)
160 struct wcsession *WCC = WC;
167 _serv_read(&buf[0], 1, WCC);
169 if ((ch != 13) && (ch != 10)) {
172 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
175 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
180 int StrBuf_ServGetln(StrBuf *buf)
185 rc = StrBufTCP_read_line(buf, &WC->serv_sock, 0, &ErrStr);
188 lprintf(1, "Server connection broken: %s\n",
191 WC->serv_sock = (-1);
198 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
203 rc = StrBufReadBLOB(buf, &WC->serv_sock, 1, BlobSize, &Err);
206 lprintf(1, "Server connection broken: %s\n",
209 WC->serv_sock = (-1);
217 * \brief send binary to server
218 * \param buf the buffer to write to citadel server
219 * \param nbytes how many bytes to send to citadel server
221 void serv_write(const char *buf, int nbytes)
223 int bytes_written = 0;
225 while (bytes_written < nbytes) {
226 retval = write(WC->serv_sock, &buf[bytes_written],
227 nbytes - bytes_written);
229 lprintf(1, "Server connection broken: %s\n",
231 close(WC->serv_sock);
232 WC->serv_sock = (-1);
237 bytes_written = bytes_written + retval;
243 * \brief send line to server
244 * \param string the line to send to the citadel server
246 void serv_puts(const char *string)
249 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
251 serv_write(string, strlen(string));
256 * \brief send line to server
257 * \param string the line to send to the citadel server
259 void serv_putbuf(const StrBuf *string)
262 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
264 serv_write(ChrPtr(string), StrLength(string));
270 * \brief convenience function to send stuff to the server
271 * \param format the formatstring
272 * \param ... the entities to insert into format
274 void serv_printf(const char *format,...)
280 va_start(arg_ptr, format);
281 vsnprintf(buf, sizeof buf, format, arg_ptr);
287 serv_write(buf, len);
289 lprintf(9, "<%s", buf);