Code

client: Let client_recv() read a full response.
[sysdb.git] / src / client / sock.c
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)
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)
125         if ((! client) || (! client->address))
126                 return -1;
128         if (client->fd >= 0)
129                 return -1;
131         if (!strncasecmp(client->address, "unix:", strlen("unix:")))
132                 connect_unixsock(client, client->address + strlen("unix:"));
133         else if (*client->address == '/')
134                 connect_unixsock(client, client->address);
135         else
136                 return -1;
138         if (client->fd < 0)
139                 return -1;
140         return 0;
141 } /* sdb_client_connect */
143 void
144 sdb_client_close(sdb_client_t *client)
146         if (! client)
147                 return;
149         close(client->fd);
150         client->fd = -1;
151 } /* sdb_client_close */
153 ssize_t
154 sdb_client_send(sdb_client_t *client,
155                 uint32_t cmd, uint32_t msg_len, const char *msg)
157         if ((! client) || (! client->fd))
158                 return -1;
160         return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
161 } /* sdb_client_send */
163 ssize_t
164 sdb_client_recv(sdb_client_t *client,
165                 uint32_t *code, sdb_strbuf_t *buf)
167         uint32_t rstatus = UINT32_MAX;
168         uint32_t rlen = UINT32_MAX;
170         size_t total = 0;
171         size_t req = 2 * sizeof(uint32_t);
173         size_t data_offset = sdb_strbuf_len(buf);
175         if ((! client) || (! client->fd) || (! buf)) {
176                 errno = EBADF;
177                 return -1;
178         }
180         if (code)
181                 *code = UINT32_MAX;
183         while (42) {
184                 ssize_t status;
186                 /* XXX: use select */
188                 errno = 0;
189                 status = sdb_strbuf_read(buf, client->fd, req);
190                 if (status < 0) {
191                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
192                                 continue;
193                         return status;
194                 }
195                 else if (! status) /* EOF */
196                         break;
198                 total += (size_t)status;
200                 if (total != req)
201                         continue;
203                 if (rstatus == UINT32_MAX) {
204                         /* retrieve status and data len */
205                         rstatus = sdb_proto_get_int(buf, data_offset);
206                         rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
208                         if (! rlen)
209                                 break;
211                         req = (size_t)rlen;
212                         total = 0;
213                 }
214                 else /* finished reading data */
215                         break;
216         }
218         if (total != req) {
219                 /* unexpected EOF; clear partially read data */
220                 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
221                 return 0;
222         }
224         if (rstatus != UINT32_MAX)
225                 /* remove status,len */
226                 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
228         if (code)
229                 *code = rstatus;
231         return (ssize_t)total;
232 } /* sdb_client_recv */
234 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */