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