Code

Moved core/error to utils/error.
[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 /*
40  * public API
41  */
43 ssize_t
44 sdb_proto_send(int fd, size_t msg_len, const char *msg)
45 {
46         const char *buf;
47         size_t len;
49         if ((fd < 0) || (msg_len && (! msg)))
50                 return -1;
51         if (! msg_len)
52                 return 0;
54         buf = msg;
55         len = msg_len;
56         while (len > 0) {
57                 ssize_t status;
59                 /* XXX: use select() */
61                 errno = 0;
62                 status = write(fd, buf, len);
63                 if (status < 0) {
64                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
65                                 continue;
66                         if (errno == EINTR)
67                                 continue;
69                         return status;
70                 }
72                 len -= (size_t)status;
73                 buf += status;
74         }
76         return (ssize_t)msg_len;
77 } /* sdb_proto_send */
79 ssize_t
80 sdb_proto_send_msg(int fd, uint32_t code,
81                 uint32_t msg_len, const char *msg)
82 {
83         size_t len = 2 * sizeof(uint32_t) + msg_len;
84         char buffer[len];
86         uint32_t tmp;
88         tmp = htonl(code);
89         memcpy(buffer, &tmp, sizeof(tmp));
90         tmp = htonl(msg_len);
91         memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
93         if (msg_len)
94                 memcpy(buffer + 2 * sizeof(tmp), msg, msg_len);
96         return sdb_proto_send(fd, len, buffer);
97 } /* sdb_proto_send_msg */
99 uint32_t
100 sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset)
102         const char *data;
103         uint32_t n;
105         if (! buf)
106                 return UINT32_MAX;
108         /* not enough data to read */
109         if (offset + sizeof(uint32_t) > sdb_strbuf_len(buf))
110                 return UINT32_MAX;
112         data = sdb_strbuf_string(buf);
113         data += offset;
114         memcpy(&n, data, sizeof(n));
115         return ntohl(n);
116 } /* sdb_proto_get_int */
118 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */