8 #include <netinet/in.h>
9 #include <sys/socket.h>
12 #include "psmb_priv.h"
15 #include <linux/ipv6.h> /* contains definition of `struct in6_pktinfo`, */
16 /* but only if _GNU_SOURCE is defined. Arrgh! */
17 /* The structure itself *should* be like this:
19 struct in6_addr ipi6_addr;
24 static void dummy_log(void *log_priv, int priority, const char *format, ...) {}
26 psmb_ctx_t *psmb_new(void)
28 return psmb_new_mm(malloc, free, realloc);
31 psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
32 void (*free)(void *ptr),
33 void *(*realloc)(void *ptr, size_t size))
35 psmb_ctx_t *ctx = (*malloc)(sizeof(psmb_ctx_t));
38 LOG(ctx, LOG_ERR, "failed to allocate psmb_ctx: %m");
44 .malloc = malloc, .free = free, .realloc = realloc,
46 .prefix = (struct in6_addr){{{ 0xff, 0x15, 'P', 'S',
48 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00 }}},
51 .pmtu = PSMB_DEFAULT_PMTU,
52 .port = PSMB_DEFAULT_PORT};
56 psmb_result_t psmb_set_logf(psmb_ctx_t *ctx,
57 void (*logf)(void *log_priv, int priority, const char *format, ...),
62 ctx->log_priv = log_priv;
63 return (psmb_result_t){PSMB_OK};
65 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
67 return (psmb_result_t){PSMB_ERROR};
71 psmb_result_t psmb_set_pmtu(psmb_ctx_t *ctx, unsigned int pmtu)
75 return (psmb_result_t){PSMB_OK};
77 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
79 return (psmb_result_t){PSMB_ERROR};
83 psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port)
87 return (psmb_result_t){PSMB_OK};
89 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
91 return (psmb_result_t){PSMB_ERROR};
95 psmb_result_t psmb_set_mgrp(psmb_ctx_t *ctx, struct in6_addr prefix,
96 unsigned char prefixlen)
98 if (prefixlen > 128) {
99 LOG(ctx, LOG_ERR, "psmb_set_mgrp() prefixlen %d is too big",
102 return (psmb_result_t){PSMB_ERROR};
105 ctx->prefix = prefix;
106 ctx->prefixlen = prefixlen;
107 return (psmb_result_t){PSMB_OK};
109 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
111 return (psmb_result_t){PSMB_ERROR};
115 psmb_result_t psmb_open(psmb_ctx_t *ctx)
117 unsigned long on = 1;
118 struct sockaddr_in6 addr = (struct sockaddr_in6){
119 .sin6_family = AF_INET6,
120 .sin6_addr = in6addr_any,
121 .sin6_port = htons(ctx->port)
125 LOG(ctx, LOG_ERR, "redundant call to psmb_open()");
127 return (psmb_result_t){PSMB_ERROR};
129 ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
132 LOG(ctx, LOG_ERR, "socket: %m");
134 return (psmb_result_t){PSMB_ERROR};
136 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
137 &on, sizeof(on)) < 0) {
139 LOG(ctx, LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...): %m");
143 return (psmb_result_t){PSMB_ERROR};
145 if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
146 &on, sizeof(on)) < 0) {
148 LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_RECVPKTINFO, ...): %m");
152 return (psmb_result_t){PSMB_ERROR};
154 if (bind(ctx->fd, (struct sockaddr *)&addr,
155 sizeof(struct sockaddr_in6)) == -1) {
157 LOG(ctx, LOG_ERR, "bind(): %m");
161 return (psmb_result_t){PSMB_ERROR};
163 /* TODO: set non-blocking */
164 return (psmb_result_t){PSMB_OK};
167 static struct in6_addr multiaddr(struct in6_addr prefix,
168 unsigned char prefixlen, uint64_t suffix)
170 struct in6_addr result = prefix;
171 unsigned char len = prefixlen > 64 ? prefixlen : 64;
172 uint64_t mask = len == 64 ? ~(uint64_t)0 : ((uint64_t)1 << len) - 1;
174 *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) &= ~mask;
175 *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) |= (suffix & mask);
179 static psmb_result_t psmb_sub_unsub(psmb_ctx_t *ctx, char *channel, int option)
181 struct ipv6_mreq mreq = { 0 };
182 char mgrp_str[INET6_ADDRSTRLEN+1];
185 LOG(ctx, LOG_ERR, "subscribe: psmb is not open");
187 return (psmb_result_t){PSMB_ERROR};
189 mreq.ipv6mr_multiaddr = multiaddr(ctx->prefix, ctx->prefixlen,
190 hash64(channel, strlen(channel)));
191 (void)inet_ntop(AF_INET6, &mreq.ipv6mr_multiaddr,
192 mgrp_str, sizeof(mgrp_str));
193 LOG(ctx, LOG_DEBUG, "using multiaddr %s for channel \"%s\"",
195 mreq.ipv6mr_interface = 0; /* how to use this??? */
196 if (setsockopt(ctx->fd, IPPROTO_IPV6, option,
197 (void *)&mreq, sizeof(mreq)) == -1) {
199 LOG(ctx, LOG_ERR, "add_membership(): %m");
201 return (psmb_result_t){PSMB_ERROR};
203 return (psmb_result_t){PSMB_OK};
206 psmb_result_t psmb_subscribe(psmb_ctx_t *ctx, char *channel)
208 return psmb_sub_unsub(ctx, channel, IPV6_ADD_MEMBERSHIP);
211 psmb_result_t psmb_unsubscribe(psmb_ctx_t *ctx, char *channel)
213 return psmb_sub_unsub(ctx, channel, IPV6_DROP_MEMBERSHIP);
216 psmb_result_t psmb_ev_rd(psmb_ctx_t *ctx)
219 struct sockaddr_in6 peer_addr;
220 struct in6_addr self_addr = {{{0}}};
221 unsigned char msgbuf[BUFSIZ];
222 unsigned char cmsgbuf[BUFSIZ];
223 struct iovec iov[1] = {{
225 .iov_len=sizeof(msgbuf),
227 struct msghdr message = {
228 .msg_name=&peer_addr,
229 .msg_namelen=sizeof(peer_addr),
232 .msg_control=cmsgbuf,
233 .msg_controllen=sizeof(cmsgbuf),
235 struct cmsghdr *cmsg;
236 char peer_str[INET6_ADDRSTRLEN+1];
237 char self_str[INET6_ADDRSTRLEN+1];
239 if ((readsize = recvmsg(ctx->fd, &message, 0)) == -1) {
240 if (errno == EWOULDBLOCK)
241 return (psmb_result_t){PSMB_OK};
244 LOG(ctx, LOG_ERR, "recvmsg(..., 0): %m");
246 return (psmb_result_t){PSMB_ERROR};
249 for (cmsg = CMSG_FIRSTHDR(&message);
251 cmsg = CMSG_NXTHDR(&message, cmsg)) {
252 LOG(ctx, LOG_DEBUG, "CMSG: level %d, type %d - skip",
253 cmsg->cmsg_level, cmsg->cmsg_type);
254 if (cmsg->cmsg_level == IPPROTO_IPV6 &&
255 cmsg->cmsg_type == IPV6_PKTINFO) {
256 struct in6_pktinfo *pi =
257 (struct in6_pktinfo *)CMSG_DATA(cmsg);
258 self_addr = pi->ipi6_addr;
261 (void)inet_ntop(AF_INET6, &peer_addr.sin6_addr,
262 peer_str, sizeof(peer_str));
263 (void)inet_ntop(AF_INET6, &self_addr,
264 self_str, sizeof(self_str));
265 LOG(ctx, LOG_DEBUG, "CMSG: %d bytes from %s to %s",
266 readsize, peer_str, self_str);
268 return (psmb_result_t){PSMB_OK};
271 psmb_result_t psmb_ev_wr(psmb_ctx_t *ctx)
273 return (psmb_result_t){PSMB_OK};
276 psmb_result_t psmb_ev_ex(psmb_ctx_t *ctx)
278 return (psmb_result_t){PSMB_OK};
281 psmb_result_t psmb_publish(psmb_ctx_t *ctx, char *channel,
282 void *data, size_t size)
285 unsigned long ifindex = 0;
286 setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
287 &ifindex, sizeof(ifindex));
290 return (psmb_result_t){PSMB_OK};
293 psmb_result_t psmb_get_message(psmb_ctx_t *ctx, char **channel,
294 void **data, size_t *size)
296 return (psmb_result_t){PSMB_OK};
299 bool psmb_success(psmb_result_t result)
301 return !(result.code & PSMB_ERROR);
304 bool psmb_message_waiting(psmb_result_t result)
306 return !!(result.code & PSMB_MESSAGE);
309 bool psmb_need_write_wait(psmb_result_t result)
311 return !!(result.code & PSMB_NEED_WRITE);
314 void psmb_destroy(psmb_ctx_t *ctx)
317 LOG(ctx, LOG_ERR, "psmb_ctx is not open");
319 if (close(ctx->fd) == -1)
320 LOG(ctx, LOG_ERR, "close(): %m");
322 /* clean up the rest */