+
+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);
+}