Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/external/bsd/blacklist add receiver.
details: https://anonhg.NetBSD.org/src/rev/2bbd93f32f5c
branches: trunk
changeset: 335525:2bbd93f32f5c
user: christos <christos%NetBSD.org@localhost>
date: Sun Jan 11 17:56:21 2015 +0000
description:
add receiver.
diffstat:
external/bsd/blacklist/include/bl.h | 9 +-
external/bsd/blacklist/lib/bl.c | 103 +++++++++++++++++++++++++++++++--
external/bsd/blacklist/test/srvtest.c | 4 +-
3 files changed, 103 insertions(+), 13 deletions(-)
diffs (201 lines):
diff -r 8f5a5f928f67 -r 2bbd93f32f5c external/bsd/blacklist/include/bl.h
--- a/external/bsd/blacklist/include/bl.h Sun Jan 11 17:29:57 2015 +0000
+++ b/external/bsd/blacklist/include/bl.h Sun Jan 11 17:56:21 2015 +0000
@@ -1,13 +1,16 @@
+
+#include <stdbool.h>
typedef enum {
+ BL_INVALID,
BL_ADD,
- BL_PING,
} bl_type_t;
typedef struct blacklist *bl_t;
-bl_t bl_create(void);
+bl_t bl_create(bool);
void bl_destroy(bl_t);
-int bl_add(bl_t, bl_type_t, int, int, const char *ctx);
+int bl_send(bl_t, bl_type_t, int, int, const char *);
+int bl_recv(bl_t, bl_type_t *, int *, int *, char *, size_t);
#define _PATH_BLACKLIST "/tmp/blacklist"
diff -r 8f5a5f928f67 -r 2bbd93f32f5c external/bsd/blacklist/lib/bl.c
--- a/external/bsd/blacklist/lib/bl.c Sun Jan 11 17:29:57 2015 +0000
+++ b/external/bsd/blacklist/lib/bl.c Sun Jan 11 17:56:21 2015 +0000
@@ -1,3 +1,4 @@
+#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -7,12 +8,13 @@
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
+#include <stdbool.h>
#include "bl.h"
#include "internal.h"
static int
-bl_init(bl_t b)
+bl_init(bl_t b, bool srv)
{
/* AF_UNIX address of local logger */
static const struct sockaddr_un sun = {
@@ -33,24 +35,32 @@
if (b->b_connected)
return 0;
- if (connect(b->b_fd, (const void *)&sun,
+ if ((srv ? bind : connect)(b->b_fd, (const void *)&sun,
(socklen_t)sizeof(sun)) == -1) {
- syslog(LOG_ERR, "%s: connect failed (%m)", __func__);
+ syslog(LOG_ERR, "%s: %s failed (%m)", __func__,
+ srv ? "bind" : "connect");
return -1;
}
- b->b_connected = 1;
+ b->b_connected = true;
+ if (srv)
+ if (listen(b->b_fd, 5) == -1) {
+ syslog(LOG_ERR, "%s: listen failed (%m)", __func__);
+ close(b->b_fd);
+ b->b_fd = -1;
+ b->b_connected = false;
+ }
return 0;
}
bl_t
-bl_create(void)
+bl_create(bool srv)
{
bl_t b = malloc(sizeof(*b));
if (b == NULL) {
syslog(LOG_ERR, "%s: malloc failed (%m)", __func__);
return NULL;
}
- bl_init(b);
+ bl_init(b, srv);
return b;
}
@@ -85,7 +95,7 @@
#endif
int
-bl_add(bl_t b, bl_type_t e, int lfd, int pfd, const char *ctx)
+bl_send(bl_t b, bl_type_t e, int lfd, int pfd, const char *ctx)
{
struct msghdr msg;
struct iovec iov;
@@ -129,8 +139,85 @@
fd[0] = lfd;
fd[1] = pfd;
- if (bl_init(b) == -1)
+ if (bl_init(b, false) == -1)
return -1;
return sendmsg(b->b_fd, &msg, 0);
}
+
+int
+bl_recv(bl_t b, bl_type_t *e, int *lfd, int *pfd, char *ctx, size_t clen)
+{
+ struct msghdr msg;
+ struct iovec iov;
+ union {
+ char ctrl[CMSG_SPACE(2 * sizeof(int))];
+ uint32_t fd[2];
+ } uc;
+ struct cmsghdr *cmsg;
+ union {
+ bl_message_t bl;
+ char buf[512];
+ } ub;
+ int *fd;
+ ssize_t rlen;
+
+ *e = BL_INVALID;
+ *lfd = *pfd = -1;
+ if (clen > 0)
+ *ctx = '\0';
+
+ iov.iov_base = ub.buf;
+ iov.iov_len = sizeof(ub);
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+
+ msg.msg_control = uc.ctrl;
+ msg.msg_controllen = sizeof(uc.ctrl);
+
+ rlen = recvmsg(b->b_fd, &msg, 0);
+ if (rlen == -1) {
+ syslog(LOG_ERR, "%s: recvmsg failed (%m)", __func__);
+ return -1;
+ }
+
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level != SOL_SOCKET) {
+ syslog(LOG_ERR, "%s: unexpected cmsg_level %d",
+ __func__, cmsg->cmsg_level);
+ continue;
+ }
+ if (cmsg->cmsg_type != SCM_RIGHTS) {
+ syslog(LOG_ERR, "%s: unexpected cmsg_type %d",
+ __func__, cmsg->cmsg_type);
+ continue;
+ }
+ if (cmsg->cmsg_len == CMSG_LEN(2 * sizeof(int))) {
+ syslog(LOG_ERR, "%s: unexpected cmsg_len %d != %zu",
+ __func__, cmsg->cmsg_len,
+ CMSG_LEN(2 * sizeof(int)));
+ continue;
+ }
+ fd = (void *)CMSG_DATA(cmsg);
+ *lfd = fd[0];
+ *pfd = fd[1];
+
+ }
+
+ if (rlen <= sizeof(ub.bl)) {
+ syslog(LOG_ERR, "message too short %zd", rlen);
+ return rlen;
+ }
+
+ if (ub.bl.bl_version != BL_VERSION) {
+ syslog(LOG_ERR, "bad version %d", ub.bl.bl_version);
+ return rlen;
+ }
+
+ *e = ub.bl.bl_type;
+ strlcpy(ctx, ub.bl.bl_data, MIN(clen, rlen - sizeof(ub.bl)));
+ return rlen;
+}
diff -r 8f5a5f928f67 -r 2bbd93f32f5c external/bsd/blacklist/test/srvtest.c
--- a/external/bsd/blacklist/test/srvtest.c Sun Jan 11 17:29:57 2015 +0000
+++ b/external/bsd/blacklist/test/srvtest.c Sun Jan 11 17:56:21 2015 +0000
@@ -22,7 +22,7 @@
if ((n = read(afd, buffer, sizeof(buffer))) == -1)
err(1, "read");
buffer[sizeof(buffer) - 1] = '\0';
- bl_add(bl, BL_ADD, sfd, afd, buffer);
+ bl_send(bl, BL_ADD, sfd, afd, buffer);
printf("received %s\n", buffer);
exit(0);
}
@@ -51,7 +51,7 @@
if (listen(sfd, 5) == -1)
err(1, "listen");
- bl = bl_create();
+ bl = bl_create(false);
for (;;) {
struct sockaddr_in asin;
Home |
Main Index |
Thread Index |
Old Index