Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / utils / proto.c
1 /*
2  * SysDB - src/utils/proto.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 "utils/error.h"
29 #include "utils/proto.h"
31 #include <arpa/inet.h>
32 #include <errno.h>
34 #include <limits.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <sys/select.h>
41 /*
42  * public API
43  */
45 int
46 sdb_proto_select(int fd, int type)
47 {
48         fd_set fds;
49         fd_set *readfds = NULL;
50         fd_set *writefds = NULL;
51         fd_set *exceptfds = NULL;
53         if (fd < 0) {
54                 errno = EBADF;
55                 return -1;
56         }
58         FD_ZERO(&fds);
60         switch (type) {
61                 case SDB_PROTO_SELECTIN:
62                         readfds = &fds;
63                         break;
64                 case SDB_PROTO_SELECTOUT:
65                         writefds = &fds;
66                         break;
67                 case SDB_PROTO_SELECTERR:
68                         exceptfds = &fds;
69                         break;
70                 default:
71                         errno = EINVAL;
72                         return -1;
73         }
75         FD_SET(fd, &fds);
77         while (42) {
78                 int n;
79                 errno = 0;
80                 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
82                 if ((n < 0) && (errno != EINTR))
83                         return (ssize_t)n;
84                 if (n > 0)
85                         break;
86         }
87         return 0;
88 } /* sdb_proto_select */
90 ssize_t
91 sdb_proto_send(int fd, size_t msg_len, const char *msg)
92 {
93         const char *buf;
94         size_t len;
96         if ((fd < 0) || (msg_len && (! msg)))
97                 return -1;
98         if (! msg_len)
99                 return 0;
101         buf = msg;
102         len = msg_len;
103         while (len > 0) {
104                 ssize_t status;
106                 if (sdb_proto_select(fd, SDB_PROTO_SELECTOUT))
107                         return -1;
109                 errno = 0;
110                 status = write(fd, buf, len);
111                 if (status < 0) {
112                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
113                                 continue;
114                         if (errno == EINTR)
115                                 continue;
117                         return status;
118                 }
120                 len -= (size_t)status;
121                 buf += status;
122         }
124         return (ssize_t)msg_len;
125 } /* sdb_proto_send */
127 ssize_t
128 sdb_proto_send_msg(int fd, uint32_t code,
129                 uint32_t msg_len, const char *msg)
131         size_t len = 2 * sizeof(uint32_t) + msg_len;
132         char buffer[len];
134         uint32_t tmp;
136         tmp = htonl(code);
137         memcpy(buffer, &tmp, sizeof(tmp));
138         tmp = htonl(msg_len);
139         memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
141         if (msg_len)
142                 memcpy(buffer + 2 * sizeof(tmp), msg, msg_len);
144         return sdb_proto_send(fd, len, buffer);
145 } /* sdb_proto_send_msg */
147 uint32_t
148 sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset)
150         const char *data;
151         uint32_t n;
153         if (! buf)
154                 return UINT32_MAX;
156         /* not enough data to read */
157         if (offset + sizeof(uint32_t) > sdb_strbuf_len(buf))
158                 return UINT32_MAX;
160         data = sdb_strbuf_string(buf);
161         data += offset;
162         memcpy(&n, data, sizeof(n));
163         return ntohl(n);
164 } /* sdb_proto_get_int */
166 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */