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