No need to declare INADDR_NONE here, it's a unix domain socket.
[citadel.git] / ctdlsh / src / 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",
67                                 strerror(errno));
68                         return(-1);
69                 }
70                 len = len + rlen;
71                 if (!keep_reading_until_full) 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],
97                                nbytes - bytes_written);
98                 if (retval < 1) {
99                         return (-1);
100                 }
101                 bytes_written = bytes_written + retval;
102         }
103         return (bytes_written);
104 }
105
106
107 /*
108  * Input string from socket - implemented in terms of sock_read()
109  * 
110  */
111 int sock_getln(int sock, char *buf, int bufsize)
112 {
113         int i;
114
115         /* Read one character at a time.
116          */
117         for (i = 0;; i++) {
118                 if (sock_read(sock, &buf[i], 1, 1) < 0) 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) return(-1);
128
129         /* Strip any trailing CR and LF characters.
130          */
131         buf[i] = 0;
132         while ( (i > 0)
133                 && ( (buf[i - 1]==13)
134                      || ( buf[i - 1]==10)) ) {
135                 i--;
136                 buf[i] = 0;
137         }
138         return(i);
139 }
140
141
142 /*
143  * sock_puts() - send line to server - implemented in terms of sock_write()
144  * Returns the number of bytes written, or -1 for error.
145  */
146 int sock_puts(int sock, char *buf)
147 {
148         int i, j;
149
150         i = sock_write(sock, buf, strlen(buf));
151         if (i<0) return(i);
152         j = sock_write(sock, "\n", 1);
153         if (j<0) return(j);
154         return(i+j);
155 }
156
157
158 /*
159  * Write a formatted string to the server - implemented in terms of sock_write()
160  */
161 void sock_printf(int sock, const char *format,...)
162 {
163         va_list arg_ptr;
164         char buf[4096];
165         size_t len;
166
167         va_start(arg_ptr, format);
168         vsnprintf(buf, sizeof buf, format, arg_ptr);
169         va_end(arg_ptr);
170
171         sock_write(sock, buf, strlen(buf));
172 }