1 /*
2 * SysDB - src/client/sock.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "client/sock.h"
29 #include "utils/strbuf.h"
30 #include "utils/proto.h"
32 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <limits.h>
37 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
42 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
47 /*
48 * private data types
49 */
51 struct sdb_client {
52 char *address;
53 int fd;
54 };
56 /*
57 * private helper functions
58 */
60 static int
61 connect_unixsock(sdb_client_t *client, const char *address)
62 {
63 struct sockaddr_un sa;
65 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
66 if (client->fd < 0)
67 return -1;
69 sa.sun_family = AF_UNIX;
70 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
71 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
73 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
74 sdb_client_close(client);
75 return -1;
76 }
77 return client->fd;
78 } /* connect_unixsock */
80 /*
81 * public API
82 */
84 sdb_client_t *
85 sdb_client_create(const char *address)
86 {
87 sdb_client_t *client;
89 if (! address)
90 return NULL;
92 client = malloc(sizeof(*client));
93 if (! client)
94 return NULL;
95 memset(client, 0, sizeof(*client));
96 client->fd = -1;
98 client->address = strdup(address);
99 if (! client->address) {
100 sdb_client_destroy(client);
101 return NULL;
102 }
104 return client;
105 } /* sdb_client_create */
107 void
108 sdb_client_destroy(sdb_client_t *client)
109 {
110 if (! client)
111 return;
113 sdb_client_close(client);
115 if (client->address)
116 free(client->address);
117 client->address = NULL;
119 free(client);
120 } /* sdb_client_destroy */
122 int
123 sdb_client_connect(sdb_client_t *client, const char *username)
124 {
125 sdb_strbuf_t *buf;
126 ssize_t status;
127 uint32_t rstatus;
129 if ((! client) || (! client->address))
130 return -1;
132 if (client->fd >= 0)
133 return -1;
135 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
136 connect_unixsock(client, client->address + strlen("unix:"));
137 else if (*client->address == '/')
138 connect_unixsock(client, client->address);
139 else
140 return -1;
142 if (client->fd < 0)
143 return -1;
145 /* XXX */
146 if (! username)
147 username = "";
149 status = sdb_client_send(client, CONNECTION_STARTUP,
150 (uint32_t)strlen(username), username);
151 if (status < 0) {
152 sdb_client_close(client);
153 return (int)status;
154 }
156 buf = sdb_strbuf_create(64);
157 rstatus = 0;
158 status = sdb_client_recv(client, &rstatus, buf);
159 if (status < 0) {
160 sdb_client_close(client);
161 sdb_strbuf_destroy(buf);
162 return (int)status;
163 }
165 if (rstatus != CONNECTION_OK) {
166 sdb_client_close(client);
167 sdb_strbuf_destroy(buf);
168 return -((int)rstatus);
169 }
170 return 0;
171 } /* sdb_client_connect */
173 void
174 sdb_client_close(sdb_client_t *client)
175 {
176 if (! client)
177 return;
179 close(client->fd);
180 client->fd = -1;
181 } /* sdb_client_close */
183 ssize_t
184 sdb_client_send(sdb_client_t *client,
185 uint32_t cmd, uint32_t msg_len, const char *msg)
186 {
187 if ((! client) || (! client->fd))
188 return -1;
190 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
191 } /* sdb_client_send */
193 ssize_t
194 sdb_client_recv(sdb_client_t *client,
195 uint32_t *code, sdb_strbuf_t *buf)
196 {
197 uint32_t rstatus = UINT32_MAX;
198 uint32_t rlen = UINT32_MAX;
200 size_t total = 0;
201 size_t req = 2 * sizeof(uint32_t);
203 size_t data_offset = sdb_strbuf_len(buf);
205 if ((! client) || (! client->fd) || (! buf)) {
206 errno = EBADF;
207 return -1;
208 }
210 if (code)
211 *code = UINT32_MAX;
213 while (42) {
214 ssize_t status;
216 /* XXX: use select */
218 errno = 0;
219 status = sdb_strbuf_read(buf, client->fd, req);
220 if (status < 0) {
221 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
222 continue;
223 return status;
224 }
225 else if (! status) /* EOF */
226 break;
228 total += (size_t)status;
230 if (total != req)
231 continue;
233 if (rstatus == UINT32_MAX) {
234 /* retrieve status and data len */
235 rstatus = sdb_proto_get_int(buf, data_offset);
236 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
238 if (! rlen)
239 break;
241 req = (size_t)rlen;
242 total = 0;
243 }
244 else /* finished reading data */
245 break;
246 }
248 if (total != req) {
249 /* unexpected EOF; clear partially read data */
250 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
251 return 0;
252 }
254 if (rstatus != UINT32_MAX)
255 /* remove status,len */
256 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
258 if (code)
259 *code = rstatus;
261 return (ssize_t)total;
262 } /* sdb_client_recv */
264 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */