more fiddling around with ctdlsh
[citadel.git] / ctdlsh / src / sockets.c
1 /*
2  *
3  */
4
5 #include "ctdlsh.h"
6
7 #ifndef INADDR_NONE
8 #define INADDR_NONE 0xffffffff
9 #endif
10
11 int uds_connectsock(char *sockpath)
12 {
13         struct sockaddr_un addr;
14         int s;
15
16         memset(&addr, 0, sizeof(addr));
17         addr.sun_family = AF_UNIX;
18         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
19
20         s = socket(AF_UNIX, SOCK_STREAM, 0);
21         if (s < 0) {
22                 fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
23                 return(-1);
24         }
25
26         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
27                 fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
28                 close(s);
29                 return(-1);
30         }
31
32         return s;
33 }
34
35
36 /*
37  * sock_read_to() - input binary data from socket, with a settable timeout.
38  * Returns the number of bytes read, or -1 for error.
39  * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
40  */
41 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
42 {
43         int len,rlen;
44         fd_set rfds;
45         struct timeval tv;
46         int retval;
47
48         len = 0;
49         while (len<bytes) {
50                 FD_ZERO(&rfds);
51                 FD_SET(sock, &rfds);
52                 tv.tv_sec = timeout;
53                 tv.tv_usec = 0;
54
55                 retval = select(sock+1, &rfds, NULL, NULL, &tv);
56
57                 if (FD_ISSET(sock, &rfds) == 0) {       /* timed out */
58                         fprintf(stderr, "sock_read() timed out.\n");
59                         return(-1);
60                 }
61
62                 rlen = read(sock, &buf[len], bytes-len);
63                 if (rlen<1) {
64                         fprintf(stderr, "sock_read() failed: %s\n",
65                                 strerror(errno));
66                         return(-1);
67                 }
68                 len = len + rlen;
69                 if (!keep_reading_until_full) return(len);
70         }
71         return(bytes);
72 }
73
74
75 /*
76  * sock_read() - input binary data from socket.
77  * Returns the number of bytes read, or -1 for error.
78  */
79 int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
80 {
81         return sock_read_to(sock, buf, bytes, 30, keep_reading_until_full);
82 }
83
84
85 /*
86  * sock_write() - send binary to server.
87  * Returns the number of bytes written, or -1 for error.
88  */
89 int sock_write(int sock, char *buf, int nbytes)
90 {
91         int bytes_written = 0;
92         int retval;
93         while (bytes_written < nbytes) {
94                 retval = write(sock, &buf[bytes_written],
95                                nbytes - bytes_written);
96                 if (retval < 1) {
97                         return (-1);
98                 }
99                 bytes_written = bytes_written + retval;
100         }
101         return (bytes_written);
102 }
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) return(-1);
118                 if (buf[i] == '\n' || i == (bufsize-1))
119                         break;
120         }
121
122         /* If we got a long line, discard characters until the newline.
123          */
124         if (i == (bufsize-1))
125                 while (buf[i] != '\n')
126                         if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
127
128         /* Strip any trailing CR and LF characters.
129          */
130         buf[i] = 0;
131         while ( (i > 0)
132                 && ( (buf[i - 1]==13)
133                      || ( buf[i - 1]==10)) ) {
134                 i--;
135                 buf[i] = 0;
136         }
137         return(i);
138 }
139
140
141
142 /*
143  * sock_puts() - send line to server - implemented in terms of serv_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 void sock_printf(int sock, const char *format,...)
159 {
160         va_list arg_ptr;
161         char buf[4096];
162         size_t len;
163
164         va_start(arg_ptr, format);
165         vsnprintf(buf, sizeof buf, format, arg_ptr);
166         va_end(arg_ptr);
167
168         sock_write(sock, buf, strlen(buf));
169 }