X-Git-Url: http://www.average.org/gitweb/?p=psmb.git;a=blobdiff_plain;f=src%2Fpsmb_socket.c;h=d18ad57c506327fbfb03078fdc6fd1b1d44ae07c;hp=c962651bc8ac21f19e7b61c63e9e815c590e1d2e;hb=ff0485ea8683816c1374bfc21f77a3623b10ed48;hpb=20b696a4f530bab420be36972f6a19d36b0f7080 diff --git a/src/psmb_socket.c b/src/psmb_socket.c index c962651..d18ad57 100644 --- a/src/psmb_socket.c +++ b/src/psmb_socket.c @@ -1,12 +1,15 @@ #include +#include #include #include +#include #include #include #include #include #include "psmb_priv.h" +#include "hash64.h" static void dummy_log(void *log_priv, int priority, const char *format, ...) {} @@ -20,12 +23,21 @@ psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size), void *(*realloc)(void *ptr, size_t size)) { psmb_ctx_t *ctx = (*malloc)(sizeof(psmb_ctx_t)); - if (!ctx) + if (!ctx) { + int sverr = errno; + LOG(ctx, LOG_ERR, "failed to allocate psmb_ctx: %m"); + errno = sverr; return NULL; + } *ctx = (psmb_ctx_t){ .fd = -1, .malloc = malloc, .free = free, .realloc = realloc, .logf = dummy_log, + .prefix = (struct in6_addr){{{ 0xff, 0x15, 'P', 'S', + 'M', 'B', '0', '1', + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 }}}, + .prefixlen = 64, .pmtu = PSMB_DEFAULT_PMTU, .port = PSMB_DEFAULT_PORT}; return ctx; @@ -40,6 +52,7 @@ psmb_result_t psmb_set_logf(psmb_ctx_t *ctx, ctx->log_priv = log_priv; return (psmb_result_t){PSMB_OK}; } else { + LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()"); errno = EBUSY; return (psmb_result_t){PSMB_ERROR}; } @@ -51,6 +64,7 @@ psmb_result_t psmb_set_pmtu(psmb_ctx_t *ctx, unsigned int pmtu) ctx->pmtu = pmtu; return (psmb_result_t){PSMB_OK}; } else { + LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()"); errno = EBUSY; return (psmb_result_t){PSMB_ERROR}; } @@ -62,6 +76,7 @@ psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port) ctx->port = port; return (psmb_result_t){PSMB_OK}; } else { + LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()"); errno = EBUSY; return (psmb_result_t){PSMB_ERROR}; } @@ -70,11 +85,18 @@ psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port) psmb_result_t psmb_set_mgrp(psmb_ctx_t *ctx, struct in6_addr prefix, unsigned char prefixlen) { + if (prefixlen > 128) { + LOG(ctx, LOG_ERR, "psmb_set_mgrp() prefixlen %d is too big", + prefixlen); + errno = EINVAL; + return (psmb_result_t){PSMB_ERROR}; + } if (ctx->fd == -1) { ctx->prefix = prefix; ctx->prefixlen = prefixlen; return (psmb_result_t){PSMB_OK}; } else { + LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()"); errno = EBUSY; return (psmb_result_t){PSMB_ERROR}; } @@ -90,32 +112,39 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx) }; if (ctx->fd != -1) { + LOG(ctx, LOG_ERR, "redundant call to psmb_open()"); errno = EBUSY; return (psmb_result_t){PSMB_ERROR}; } - ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IPV6); + ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (ctx->fd == -1) { + int sverr = errno; + LOG(ctx, LOG_ERR, "socket: %m"); + errno = sverr; return (psmb_result_t){PSMB_ERROR}; } if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) { int sverr = errno; + LOG(ctx, LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...): %m"); close(ctx->fd); ctx->fd = -1; errno = sverr; return (psmb_result_t){PSMB_ERROR}; } - if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_PKTINFO, + if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0) { int sverr = errno; + LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_PKTINFO, ...): %m"); close(ctx->fd); ctx->fd = -1; errno = sverr; return (psmb_result_t){PSMB_ERROR}; } if (bind(ctx->fd, (struct sockaddr *)&addr, - sizeof(struct sockaddr)) == -1) { + sizeof(struct sockaddr_in6)) == -1) { int sverr = errno; + LOG(ctx, LOG_ERR, "bind(): %m"); close(ctx->fd); ctx->fd = -1; errno = sverr; @@ -123,3 +152,77 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx) } return (psmb_result_t){PSMB_OK}; } + +static struct in6_addr multiaddr(struct in6_addr prefix, + unsigned char prefixlen, uint64_t suffix) +{ + struct in6_addr result = prefix; + unsigned char len = prefixlen > 64 ? prefixlen : 64; + uint64_t mask = len == 64 ? ~(uint64_t)0 : ((uint64_t)1 << len) - 1; + + *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) &= ~mask; + *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) |= (suffix & mask); + return result; +} + +static psmb_result_t psmb_sub_unsub(psmb_ctx_t *ctx, char *channel, int option) +{ + struct ipv6_mreq mreq = { 0 }; + char mgrp_str[INET6_ADDRSTRLEN+1]; + + if (ctx->fd == -1) { + LOG(ctx, LOG_ERR, "subscribe: psmb is not open"); + errno = EINVAL; + return (psmb_result_t){PSMB_ERROR}; + } + mreq.ipv6mr_multiaddr = multiaddr(ctx->prefix, ctx->prefixlen, + hash64(channel, strlen(channel))); + (void)inet_ntop(AF_INET6, &mreq.ipv6mr_multiaddr, + mgrp_str, sizeof(mgrp_str)); + LOG(ctx, LOG_DEBUG, "using multiaddr %s for channel \"%s\"", + mgrp_str, channel); + mreq.ipv6mr_interface = 0; /* how to use this??? */ + if (setsockopt(ctx->fd, IPPROTO_IPV6, option, + (void *)&mreq, sizeof(mreq)) == -1) { + int sverr = errno; + LOG(ctx, LOG_ERR, "add_membership(): %m"); + errno = sverr; + return (psmb_result_t){PSMB_ERROR}; + } + return (psmb_result_t){PSMB_OK}; +} + +psmb_result_t psmb_subscribe(psmb_ctx_t *ctx, char *channel) { + return psmb_sub_unsub(ctx, channel, IPV6_ADD_MEMBERSHIP); +} + +psmb_result_t psmb_unsubscribe(psmb_ctx_t *ctx, char *channel) { + return psmb_sub_unsub(ctx, channel, IPV6_DROP_MEMBERSHIP); +} + +bool psmb_success(psmb_result_t result) +{ + return !(result.code & PSMB_ERROR); +} + +bool psmb_message_waiting(psmb_result_t result) +{ + return !!(result.code & PSMB_MESSAGE); +} + +bool psmb_need_write_wait(psmb_result_t result) +{ + return !!(result.code & PSMB_NEED_WRITE); +} + +void psmb_destroy(psmb_ctx_t *ctx) +{ + if (ctx->fd == -1) { + LOG(ctx, LOG_ERR, "psmb_ctx is not open"); + } else { + if (close(ctx->fd) == -1) + LOG(ctx, LOG_ERR, "close(): %m"); + } + /* clean up the rest */ + (*ctx->free)(ctx); +}