]> www.average.org Git - psmb.git/blob - src/psmb_socket.c
d18ad57c506327fbfb03078fdc6fd1b1d44ae07c
[psmb.git] / src / psmb_socket.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <arpa/inet.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9
10 #include <psmb.h>
11 #include "psmb_priv.h"
12 #include "hash64.h"
13
14 static void dummy_log(void *log_priv, int priority, const char *format, ...) {}
15
16 psmb_ctx_t *psmb_new(void)
17 {
18         return psmb_new_mm(malloc, free, realloc);
19 }
20
21 psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
22                         void (*free)(void *ptr),
23                         void *(*realloc)(void *ptr, size_t size))
24 {
25         psmb_ctx_t *ctx = (*malloc)(sizeof(psmb_ctx_t));
26         if (!ctx) {
27                 int sverr = errno;
28                 LOG(ctx, LOG_ERR, "failed to allocate psmb_ctx: %m");
29                 errno = sverr;
30                 return NULL;
31         }
32         *ctx = (psmb_ctx_t){
33                 .fd = -1,
34                 .malloc = malloc, .free = free, .realloc = realloc,
35                 .logf = dummy_log,
36                 .prefix = (struct in6_addr){{{  0xff, 0x15,  'P',  'S',
37                                                  'M',  'B',  '0',  '1',
38                                                 0x00, 0x00, 0x00, 0x00,
39                                                 0x00, 0x00, 0x00, 0x00 }}},
40                 .prefixlen = 64,
41                 .pmtu = PSMB_DEFAULT_PMTU,
42                 .port = PSMB_DEFAULT_PORT};
43         return ctx;
44 }
45
46 psmb_result_t psmb_set_logf(psmb_ctx_t *ctx,
47         void (*logf)(void *log_priv, int priority, const char *format, ...),
48         void *log_priv)
49 {
50         if (ctx->fd == -1) {
51                 ctx->logf = logf;
52                 ctx->log_priv = log_priv;
53                 return (psmb_result_t){PSMB_OK};
54         } else {
55                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
56                 errno = EBUSY;
57                 return (psmb_result_t){PSMB_ERROR};
58         }
59 }
60
61 psmb_result_t psmb_set_pmtu(psmb_ctx_t *ctx, unsigned int pmtu)
62 {
63         if (ctx->fd == -1) {
64                 ctx->pmtu = pmtu;
65                 return (psmb_result_t){PSMB_OK};
66         } else {
67                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
68                 errno = EBUSY;
69                 return (psmb_result_t){PSMB_ERROR};
70         }
71 }
72
73 psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port)
74 {
75         if (ctx->fd == -1) {
76                 ctx->port = port;
77                 return (psmb_result_t){PSMB_OK};
78         } else {
79                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
80                 errno = EBUSY;
81                 return (psmb_result_t){PSMB_ERROR};
82         }
83 }
84
85 psmb_result_t psmb_set_mgrp(psmb_ctx_t *ctx, struct in6_addr prefix,
86         unsigned char prefixlen)
87 {
88         if (prefixlen > 128) {
89                 LOG(ctx, LOG_ERR, "psmb_set_mgrp() prefixlen %d is too big",
90                                 prefixlen);
91                 errno = EINVAL;
92                 return (psmb_result_t){PSMB_ERROR};
93         }
94         if (ctx->fd == -1) {
95                 ctx->prefix = prefix;
96                 ctx->prefixlen = prefixlen;
97                 return (psmb_result_t){PSMB_OK};
98         } else {
99                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
100                 errno = EBUSY;
101                 return (psmb_result_t){PSMB_ERROR};
102         }
103 }
104
105 psmb_result_t psmb_open(psmb_ctx_t *ctx)
106 {
107         unsigned long on = 1;
108         struct sockaddr_in6 addr = (struct sockaddr_in6){
109                 .sin6_family = AF_INET6,
110                 .sin6_addr = in6addr_any,
111                 .sin6_port = htons(ctx->port)
112         };
113
114         if (ctx->fd != -1) {
115                 LOG(ctx, LOG_ERR, "redundant call to psmb_open()");
116                 errno = EBUSY;
117                 return (psmb_result_t){PSMB_ERROR};
118         }
119         ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
120         if (ctx->fd == -1) {
121                 int sverr = errno;
122                 LOG(ctx, LOG_ERR, "socket: %m");
123                 errno = sverr;
124                 return (psmb_result_t){PSMB_ERROR};
125         }
126         if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
127                         &on, sizeof(on)) < 0) {
128                 int sverr = errno;
129                 LOG(ctx, LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...): %m");
130                 close(ctx->fd);
131                 ctx->fd = -1;
132                 errno = sverr;
133                 return (psmb_result_t){PSMB_ERROR};
134         }
135         if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
136                         &on, sizeof(on)) < 0) {
137                 int sverr = errno;
138                 LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_PKTINFO, ...): %m");
139                 close(ctx->fd);
140                 ctx->fd = -1;
141                 errno = sverr;
142                 return (psmb_result_t){PSMB_ERROR};
143         }
144         if (bind(ctx->fd, (struct sockaddr *)&addr,
145                         sizeof(struct sockaddr_in6)) == -1) {
146                 int sverr = errno;
147                 LOG(ctx, LOG_ERR, "bind(): %m");
148                 close(ctx->fd);
149                 ctx->fd = -1;
150                 errno = sverr;
151                 return (psmb_result_t){PSMB_ERROR};
152         }
153         return (psmb_result_t){PSMB_OK};
154 }
155
156 static struct in6_addr multiaddr(struct in6_addr prefix,
157                 unsigned char prefixlen, uint64_t suffix)
158 {
159         struct in6_addr result = prefix;
160         unsigned char len = prefixlen > 64 ? prefixlen : 64;
161         uint64_t mask = len == 64 ? ~(uint64_t)0 : ((uint64_t)1 << len) - 1;
162
163         *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) &= ~mask;
164         *(uint64_t *)(&result.__in6_u.__u6_addr32[2]) |= (suffix & mask);
165         return result;
166 }
167
168 static psmb_result_t psmb_sub_unsub(psmb_ctx_t *ctx, char *channel, int option)
169 {
170         struct ipv6_mreq mreq = { 0 };
171         char mgrp_str[INET6_ADDRSTRLEN+1];
172
173         if (ctx->fd == -1) {
174                 LOG(ctx, LOG_ERR, "subscribe: psmb is not open");
175                 errno = EINVAL;
176                 return (psmb_result_t){PSMB_ERROR};
177         }
178         mreq.ipv6mr_multiaddr = multiaddr(ctx->prefix, ctx->prefixlen,
179                         hash64(channel, strlen(channel)));
180         (void)inet_ntop(AF_INET6, &mreq.ipv6mr_multiaddr,
181                         mgrp_str, sizeof(mgrp_str));
182         LOG(ctx, LOG_DEBUG, "using multiaddr %s for channel \"%s\"",
183                         mgrp_str, channel);
184         mreq.ipv6mr_interface = 0; /* how to use this??? */
185         if (setsockopt(ctx->fd, IPPROTO_IPV6, option,
186                         (void *)&mreq, sizeof(mreq)) == -1) {
187                 int sverr = errno;
188                 LOG(ctx, LOG_ERR, "add_membership(): %m");
189                 errno = sverr;
190                 return (psmb_result_t){PSMB_ERROR};
191         }
192         return (psmb_result_t){PSMB_OK};
193 }
194
195 psmb_result_t psmb_subscribe(psmb_ctx_t *ctx, char *channel) {
196         return psmb_sub_unsub(ctx, channel, IPV6_ADD_MEMBERSHIP);
197 }
198
199 psmb_result_t psmb_unsubscribe(psmb_ctx_t *ctx, char *channel) {
200         return psmb_sub_unsub(ctx, channel, IPV6_DROP_MEMBERSHIP);
201 }
202
203 bool psmb_success(psmb_result_t result)
204 {
205         return !(result.code & PSMB_ERROR);
206 }
207
208 bool psmb_message_waiting(psmb_result_t result)
209 {
210         return !!(result.code & PSMB_MESSAGE);
211 }
212
213 bool psmb_need_write_wait(psmb_result_t result)
214 {
215         return !!(result.code & PSMB_NEED_WRITE);
216 }
217
218 void psmb_destroy(psmb_ctx_t *ctx)
219 {
220         if (ctx->fd == -1) {
221                 LOG(ctx, LOG_ERR, "psmb_ctx is not open");
222         } else {
223                 if (close(ctx->fd) == -1)
224                         LOG(ctx, LOG_ERR, "close(): %m");
225         }
226         /* clean up the rest */
227         (*ctx->free)(ctx);
228 }