2 * This file contains functions which handle ctdlsh's connection
3 * to the Citadel server's admin socket.
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.
13 int uds_connectsock(char *sockpath)
15 struct sockaddr_un addr;
18 memset(&addr, 0, sizeof(addr));
19 addr.sun_family = AF_UNIX;
20 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
22 s = socket(AF_UNIX, SOCK_STREAM, 0);
24 fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
28 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
29 fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
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
43 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
57 retval = select(sock + 1, &rfds, NULL, NULL, &tv);
59 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
60 fprintf(stderr, "sock_read() timed out.\n");
64 rlen = read(sock, &buf[len], bytes - len);
66 fprintf(stderr, "sock_read() failed: %s\n", strerror(errno));
70 if (!keep_reading_until_full)
78 * sock_read() - input binary data from socket.
79 * Returns the number of bytes read, or -1 for error.
81 int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
83 return sock_read_to(sock, buf, bytes, 30, keep_reading_until_full);
88 * sock_write() - send binary to server.
89 * Returns the number of bytes written, or -1 for error.
91 int sock_write(int sock, char *buf, int nbytes)
93 int bytes_written = 0;
95 while (bytes_written < nbytes) {
96 retval = write(sock, &buf[bytes_written], nbytes - bytes_written);
100 bytes_written = bytes_written + retval;
102 return (bytes_written);
107 * Input string from socket - implemented in terms of sock_read()
110 int sock_getln(int sock, char *buf, int bufsize)
114 /* Read one character at a time.
117 if (sock_read(sock, &buf[i], 1, 1) < 0)
119 if (buf[i] == '\n' || i == (bufsize - 1))
123 /* If we got a long line, discard characters until the newline.
125 if (i == (bufsize - 1))
126 while (buf[i] != '\n')
127 if (sock_read(sock, &buf[i], 1, 1) < 0)
130 /* Strip any trailing CR and LF characters.
134 && ((buf[i - 1] == 13)
135 || (buf[i - 1] == 10))) {
144 * sock_puts() - send line to server - implemented in terms of sock_write()
145 * Returns the number of bytes written, or -1 for error.
147 int sock_puts(int sock, char *buf)
151 i = sock_write(sock, buf, strlen(buf));
154 j = sock_write(sock, "\n", 1);
162 * Write a formatted string to the server - implemented in terms of sock_write()
164 void sock_printf(int sock, const char *format, ...)
170 va_start(arg_ptr, format);
171 vsnprintf(buf, sizeof buf, format, arg_ptr);
174 sock_write(sock, buf, strlen(buf));