8 #define INADDR_NONE 0xffffffff
11 int uds_connectsock(char *sockpath)
13 struct sockaddr_un addr;
16 memset(&addr, 0, sizeof(addr));
17 addr.sun_family = AF_UNIX;
18 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
20 s = socket(AF_UNIX, SOCK_STREAM, 0);
22 fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
26 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
27 fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
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
41 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
55 retval = select(sock+1, &rfds, NULL, NULL, &tv);
57 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
58 fprintf(stderr, "sock_read() timed out.\n");
62 rlen = read(sock, &buf[len], bytes-len);
64 fprintf(stderr, "sock_read() failed: %s\n",
69 if (!keep_reading_until_full) return(len);
76 * sock_read() - input binary data from socket.
77 * Returns the number of bytes read, or -1 for error.
79 int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
81 return sock_read_to(sock, buf, bytes, 30, keep_reading_until_full);
86 * sock_write() - send binary to server.
87 * Returns the number of bytes written, or -1 for error.
89 int sock_write(int sock, char *buf, int nbytes)
91 int bytes_written = 0;
93 while (bytes_written < nbytes) {
94 retval = write(sock, &buf[bytes_written],
95 nbytes - bytes_written);
99 bytes_written = bytes_written + retval;
101 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) return(-1);
118 if (buf[i] == '\n' || i == (bufsize-1))
122 /* If we got a long line, discard characters until the newline.
124 if (i == (bufsize-1))
125 while (buf[i] != '\n')
126 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
128 /* Strip any trailing CR and LF characters.
132 && ( (buf[i - 1]==13)
133 || ( buf[i - 1]==10)) ) {
143 * sock_puts() - send line to server - implemented in terms of serv_write()
144 * Returns the number of bytes written, or -1 for error.
146 int sock_puts(int sock, char *buf)
150 i = sock_write(sock, buf, strlen(buf));
152 j = sock_write(sock, "\n", 1);
158 void sock_printf(int sock, const char *format,...)
164 va_start(arg_ptr, format);
165 vsnprintf(buf, sizeof buf, format, arg_ptr);
168 sock_write(sock, buf, strlen(buf));