summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2df0d2d)
raw | patch | inline | side by side (parent: 2df0d2d)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 4 Dec 2013 18:32:50 +0000 (19:32 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 4 Dec 2013 18:32:50 +0000 (19:32 +0100) |
This function “recieves” a 32bit integer from a string buffer and converts it
from network to host byte order.
from network to host byte order.
src/include/utils/proto.h | patch | blob | history | |
src/utils/proto.c | patch | blob | history |
index 8ab2e1ee7b3a22adb1481fb7cfecb09daaef77c7..372e34d474b7be20dd5fc13ce6c04025748bc0a8 100644 (file)
#ifndef SDB_UTILS_PROTO_H
#define SDB_UTILS_PROTO_H 1
+#include "utils/strbuf.h"
+
#include <stdint.h>
#include <unistd.h>
sdb_proto_send_msg(int fd, uint32_t code,
uint32_t msg_len, const char *msg);
+uint32_t
+sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/src/utils/proto.c b/src/utils/proto.c
index d049144ca2716c7a493cab1be92f9fb698bde274..e9e3e6c7ca9984396a2c55bb4b299cd3be34ef4a 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
#include <arpa/inet.h>
#include <errno.h>
+#include <limits.h>
+
#include <string.h>
#include <unistd.h>
return sdb_proto_send(fd, len, buffer);
} /* sdb_proto_send_msg */
+uint32_t
+sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset)
+{
+ const char *data;
+ uint32_t n;
+
+ if (! buf)
+ return UINT32_MAX;
+
+ /* not enough data to read */
+ if (offset + sizeof(uint32_t) < sdb_strbuf_len(buf))
+ return UINT32_MAX;
+
+ data = sdb_strbuf_string(buf);
+ data += offset;
+ memcpy(&n, data, sizeof(n));
+ return ntohl(n);
+} /* sdb_proto_get_int */
+
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */