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