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