Initial revision

This commit is contained in:
christos 2015-01-10 23:17:03 +00:00
parent 5c10ef7e83
commit 7c5e701462
6 changed files with 290 additions and 0 deletions

8
external/bsd/blacklist/bin/Makefile vendored Normal file
View File

@ -0,0 +1,8 @@
.include <bsd.own.mk>
MKMAN=no
PROGS=srvtest cltest
SRCS.srvtest = client.c srvtest.c
SRCS.cltest = cltest.c
.include <bsd.prog.mk>

16
external/bsd/blacklist/bin/internal.h vendored Normal file
View File

@ -0,0 +1,16 @@
typedef struct {
uint32_t bl_len;
uint32_t bl_version;
uint32_t bl_type;
char bl_data[];
} bl_message_t;
struct blacklist {
int b_fd;
int b_connected;
};
#define BL_VERSION 1

13
external/bsd/blacklist/include/bl.h vendored Normal file
View File

@ -0,0 +1,13 @@
typedef enum {
BL_ADD,
BL_PING,
} bl_type_t;
typedef struct blacklist *bl_t;
bl_t bl_create(void);
void bl_destroy(bl_t);
int bl_add(bl_t, bl_type_t, int, int, const char *ctx);
#define _PATH_BLACKLIST "/tmp/blacklist"

136
external/bsd/blacklist/lib/bl.c vendored Normal file
View File

@ -0,0 +1,136 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "bl.h"
#include "internal.h"
static int
bl_init(bl_t b)
{
/* AF_UNIX address of local logger */
static const struct sockaddr_un sun = {
.sun_family = AF_LOCAL,
.sun_len = sizeof(sun),
.sun_path = _PATH_BLACKLIST,
};
if (b->b_fd == -1) {
b->b_fd = socket(PF_LOCAL,
SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK|SOCK_NOSIGPIPE, 0);
if (b->b_fd == -1) {
syslog(LOG_ERR, "%s: socket failed (%m)", __func__);
return 0;
}
}
if (b->b_connected)
return 0;
if (connect(b->b_fd, (const void *)&sun,
(socklen_t)sizeof(sun)) == -1) {
syslog(LOG_ERR, "%s: connect failed (%m)", __func__);
return -1;
}
b->b_connected = 1;
return 0;
}
bl_t
bl_create(void)
{
bl_t b = malloc(sizeof(*b));
if (b == NULL) {
syslog(LOG_ERR, "%s: malloc failed (%m)", __func__);
return NULL;
}
bl_init(b);
return b;
}
void
bl_destroy(bl_t b)
{
close(b->b_fd);
free(b);
}
#if 0
static int
bl_post(bl_t b, const struct sockaddr_storage *lss,
const struct sockaddr_storage *pss, bl_event_t e, const char *ctx)
{
struct sockaddr_storage lss, pss;
socklen_t lsl, psl;
lsl = sizeof(lss);
psl = sizeof(pss);
if (getsockname(lfd, &lss, &lsl) == -1) {
syslog(LOG_ERR, "%s: getsockname failed (%m)", __func__);
return -1;
}
if (getpeername(pfd, &pss, &psl) == -1) {
syslog(LOG_ERR, "%s: getpeername failed (%m)", __func__);
return -1;
}
return bl_post(&lss, &pss, e, ctx);
}
#endif
int
bl_add(bl_t b, bl_type_t e, int lfd, int pfd, const char *ctx)
{
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;
size_t ctxlen;
ctxlen = strlen(ctx);
if (ctxlen > 256)
ctxlen = 256;
iov.iov_base = ub.buf;
iov.iov_len = sizeof(bl_message_t) + ctxlen;
ub.bl.bl_len = iov.iov_len;
ub.bl.bl_version = BL_VERSION;
ub.bl.bl_type = (uint32_t)e;
memcpy(ub.bl.bl_data, ctx, ctxlen);
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);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(2 * sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
fd = (void *)CMSG_DATA(cmsg);
fd[0] = lfd;
fd[1] = pfd;
if (bl_init(b) == -1)
return -1;
return sendmsg(b->b_fd, &msg, 0);
}

42
external/bsd/blacklist/test/cltest.c vendored Normal file
View File

@ -0,0 +1,42 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
static __dead void
usage(const char *msg)
{
fprintf(stderr, "Usage: %s %s\n", getprogname(), msg);
exit(1);
}
int
main(int argc, char *argv[])
{
int sfd;
struct sockaddr_in ssin;
if (argc == 1)
usage("<message>");
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
err(1, "socket");
memset(&ssin, 0, sizeof(ssin));
ssin.sin_family = AF_INET;
ssin.sin_len = sizeof(ssin);
ssin.sin_addr.s_addr = htonl(INADDR_ANY);
ssin.sin_port = htons(6161);
if (connect(sfd, (const void *)&ssin, sizeof(ssin)) == -1)
err(1, "connect");
size_t len = strlen(argv[1]) + 1;
if (write(sfd, argv[1], len) != (ssize_t)len)
err(1, "write");
return 0;
}

75
external/bsd/blacklist/test/srvtest.c vendored Normal file
View File

@ -0,0 +1,75 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>
#include "bl.h"
static void
process(bl_t bl, int sfd, int afd)
{
ssize_t n;
char buffer[256];
memset(buffer, 0, sizeof(buffer));
if ((n = read(afd, buffer, sizeof(buffer))) == -1)
err(1, "read");
buffer[sizeof(buffer) - 1] = '\0';
bl_add(bl, BL_ADD, sfd, afd, buffer);
printf("received %s\n", buffer);
exit(0);
}
int
main(int argc, char *argv[])
{
int sfd;
bl_t bl;
struct sockaddr_in ssin;
if ((sfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
err(1, "socket");
signal(SIGCHLD, SIG_IGN);
memset(&ssin, 0, sizeof(ssin));
ssin.sin_family = AF_INET;
ssin.sin_len = sizeof(ssin);
ssin.sin_addr.s_addr = htonl(INADDR_ANY);
ssin.sin_port = htons(6161);
if (bind(sfd, (const void *)&ssin, sizeof(ssin)) == -1)
err(1, "bind");
if (listen(sfd, 5) == -1)
err(1, "listen");
bl = bl_create();
for (;;) {
struct sockaddr_in asin;
socklen_t alen = sizeof(asin);
int afd;
if ((afd = accept(sfd, (void *)&asin, &alen)) == -1)
err(1, "accept");
/* Create child process */
switch (fork()) {
case -1:
err(1, "fork");
case 0:
process(bl, sfd, afd);
break;
default:
close(afd);
break;
}
}
}