]> www.average.org Git - psmb.git/blobdiff - src/psmb_socket.c
add hash64 function
[psmb.git] / src / psmb_socket.c
index a3c718a9902334f9804cb6fa8338e3b1f9816961..06b90a83f66f28674c0d0a3285bb1f4a2228affb 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/types.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
@@ -30,6 +31,11 @@ psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
                .fd = -1,
                .malloc = malloc, .free = free, .realloc = realloc,
                .logf = dummy_log,
+               .prefix = (struct in6_addr){{{  0xff, 0xff, 0x01, 0x05,
+                                               0xb0, 0x55, 0xff, 0xe7,
+                                               0x00, 0x00, 0x00, 0x00,
+                                               0x00, 0x00, 0x00, 0x00 }}},
+               .prefixlen = 64,
                .pmtu = PSMB_DEFAULT_PMTU,
                .port = PSMB_DEFAULT_PORT};
        return ctx;
@@ -102,7 +108,7 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
                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");
@@ -118,7 +124,7 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
                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");
@@ -128,7 +134,7 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
                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);
@@ -138,3 +144,59 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
        }
        return (psmb_result_t){PSMB_OK};
 }
+
+static psmb_result_t psmb_sub_unsub(psmb_ctx_t *ctx, char *channel, int option)
+{
+       struct ipv6_mreq mreq = { 0 };
+
+       if (ctx->fd == -1) {
+               LOG(ctx, LOG_ERR, "subscribe: psmb is not open");
+               errno = EINVAL;
+               return (psmb_result_t){PSMB_ERROR};
+       }
+       mreq.ipv6mr_multiaddr = ctx->prefix; /* use hash of the 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);
+}