Obsolete sys/time.h and HAVE_TIME_WITH_SYS_TIME
[citadel.git] / ctdlsalearn / src / sockets.c
1 /*
2  * Socket functions for ctdlsalearn
3  *
4  * This is open source software for the Linux operating system, licensed under GPL v3
5  */
6
7 #include <config.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <time.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <string.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <stdarg.h>
20
21 #ifndef INADDR_NONE
22 #define INADDR_NONE 0xffffffff
23 #endif
24
25 extern int verbose;
26
27 int uds_connectsock(char *sockpath)
28 {
29         struct sockaddr_un addr;
30         int s;
31
32         memset(&addr, 0, sizeof(addr));
33         addr.sun_family = AF_UNIX;
34         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
35
36         s = socket(AF_UNIX, SOCK_STREAM, 0);
37         if (s < 0) {
38                 if (verbose) fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
39                 return(-1);
40         }
41
42         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
43                 if (verbose) fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
44                 close(s);
45                 return(-1);
46         }
47
48         return s;
49 }
50
51
52 /*
53  * sock_read_to() - input binary data from socket, with a settable timeout.
54  * Returns the number of bytes read, or -1 for error.
55  * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
56  */
57 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
58 {
59         int len,rlen;
60         fd_set rfds;
61         struct timeval tv;
62         int retval;
63
64         len = 0;
65         while (len<bytes) {
66                 FD_ZERO(&rfds);
67                 FD_SET(sock, &rfds);
68                 tv.tv_sec = timeout;
69                 tv.tv_usec = 0;
70
71                 retval = select(sock+1, &rfds, NULL, NULL, &tv);
72
73                 if (FD_ISSET(sock, &rfds) == 0) {       /* timed out */
74                         if (verbose) fprintf(stderr, "sock_read() timed out.\n");
75                         return(-1);
76                 }
77
78                 rlen = read(sock, &buf[len], bytes-len);
79                 if (rlen<1) {
80                         if (verbose) fprintf(stderr, "sock_read() failed: %s\n", strerror(errno));
81                         return(-1);
82                 }
83                 len = len + rlen;
84                 if (!keep_reading_until_full) return(len);
85         }
86         return(bytes);
87 }
88
89
90 /*
91  * sock_read() - input binary data from socket.
92  * Returns the number of bytes read, or -1 for error.
93  */
94 int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
95 {
96         return sock_read_to(sock, buf, bytes, 30, keep_reading_until_full);
97 }
98
99
100 /*
101  * sock_write() - send binary to server.
102  * Returns the number of bytes written, or -1 for error.
103  */
104 int sock_write(int sock, char *buf, int nbytes)
105 {
106         int bytes_written = 0;
107         int retval;
108         while (bytes_written < nbytes) {
109                 retval = write(sock, &buf[bytes_written],
110                                nbytes - bytes_written);
111                 if (retval < 1) {
112                         return (-1);
113                 }
114                 bytes_written = bytes_written + retval;
115         }
116         return (bytes_written);
117 }
118
119
120
121 /*
122  * Input string from socket - implemented in terms of sock_read()
123  * 
124  */
125 int sock_getln(int sock, char *buf, int bufsize)
126 {
127         int i;
128
129         /* Read one character at a time.
130          */
131         for (i = 0;; i++) {
132                 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
133                 if (buf[i] == '\n' || i == (bufsize-1))
134                         break;
135         }
136
137         /* If we got a long line, discard characters until the newline.
138          */
139         if (i == (bufsize-1))
140                 while (buf[i] != '\n')
141                         if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
142
143         /* Strip any trailing CR and LF characters.
144          */
145         buf[i] = 0;
146         while ( (i > 0)
147                 && ( (buf[i - 1]==13)
148                      || ( buf[i - 1]==10)) ) {
149                 i--;
150                 buf[i] = 0;
151         }
152         return(i);
153 }
154
155
156
157 /*
158  * sock_puts() - send line to server - implemented in terms of serv_write()
159  * Returns the number of bytes written, or -1 for error.
160  */
161 int sock_puts(int sock, char *buf)
162 {
163         int i, j;
164
165         i = sock_write(sock, buf, strlen(buf));
166         if (i<0) return(i);
167         j = sock_write(sock, "\n", 1);
168         if (j<0) return(j);
169         return(i+j);
170 }
171
172
173 void sock_printf(int sock, const char *format,...)
174 {
175         va_list arg_ptr;
176         char buf[4096];
177         size_t len;
178
179         va_start(arg_ptr, format);
180         vsnprintf(buf, sizeof buf, format, arg_ptr);
181         va_end(arg_ptr);
182
183         sock_write(sock, buf, strlen(buf));
184 }