]> www.average.org Git - psmb.git/blob - src/psmb_socket.c
28344a7a65c04138712f1dc5922a37e167a3b2e7
[psmb.git] / src / psmb_socket.c
1 #include <stdlib.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7
8 #include <psmb.h>
9 #include "psmb_priv.h"
10
11 static void dummy_log(void *log_priv, int priority, const char *format, ...) {}
12
13 psmb_ctx_t *psmb_new(void)
14 {
15         return psmb_new_mm(malloc, free, realloc);
16 }
17
18 psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
19                         void (*free)(void *ptr),
20                         void *(*realloc)(void *ptr, size_t size))
21 {
22         psmb_ctx_t *ctx = (*malloc)(sizeof(psmb_ctx_t));
23         if (!ctx) {
24                 int sverr = errno;
25                 LOG(ctx, LOG_ERR, "failed to allocate psmb_ctx: %m");
26                 errno = sverr;
27                 return NULL;
28         }
29         *ctx = (psmb_ctx_t){
30                 .fd = -1,
31                 .malloc = malloc, .free = free, .realloc = realloc,
32                 .logf = dummy_log,
33                 .pmtu = PSMB_DEFAULT_PMTU,
34                 .port = PSMB_DEFAULT_PORT};
35         return ctx;
36 }
37
38 psmb_result_t psmb_set_logf(psmb_ctx_t *ctx,
39         void (*logf)(void *log_priv, int priority, const char *format, ...),
40         void *log_priv)
41 {
42         if (ctx->fd == -1) {
43                 ctx->logf = logf;
44                 ctx->log_priv = log_priv;
45                 return (psmb_result_t){PSMB_OK};
46         } else {
47                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
48                 errno = EBUSY;
49                 return (psmb_result_t){PSMB_ERROR};
50         }
51 }
52
53 psmb_result_t psmb_set_pmtu(psmb_ctx_t *ctx, unsigned int pmtu)
54 {
55         if (ctx->fd == -1) {
56                 ctx->pmtu = pmtu;
57                 return (psmb_result_t){PSMB_OK};
58         } else {
59                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
60                 errno = EBUSY;
61                 return (psmb_result_t){PSMB_ERROR};
62         }
63 }
64
65 psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port)
66 {
67         if (ctx->fd == -1) {
68                 ctx->port = port;
69                 return (psmb_result_t){PSMB_OK};
70         } else {
71                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
72                 errno = EBUSY;
73                 return (psmb_result_t){PSMB_ERROR};
74         }
75 }
76
77 psmb_result_t psmb_set_mgrp(psmb_ctx_t *ctx, struct in6_addr prefix,
78         unsigned char prefixlen)
79 {
80         if (ctx->fd == -1) {
81                 ctx->prefix = prefix;
82                 ctx->prefixlen = prefixlen;
83                 return (psmb_result_t){PSMB_OK};
84         } else {
85                 LOG(ctx, LOG_ERR, "psmb_set_...() used after psmb_open()");
86                 errno = EBUSY;
87                 return (psmb_result_t){PSMB_ERROR};
88         }
89 }
90
91 psmb_result_t psmb_open(psmb_ctx_t *ctx)
92 {
93         unsigned long on = 1;
94         struct sockaddr_in6 addr = (struct sockaddr_in6){
95                 .sin6_family = AF_INET6,
96                 .sin6_addr = in6addr_any,
97                 .sin6_port = htons(ctx->port)
98         };
99
100         if (ctx->fd != -1) {
101                 LOG(ctx, LOG_ERR, "redundant call to psmb_open()");
102                 errno = EBUSY;
103                 return (psmb_result_t){PSMB_ERROR};
104         }
105         ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IPV6);
106         if (ctx->fd == -1) {
107                 int sverr = errno;
108                 LOG(ctx, LOG_ERR, "socket: %m");
109                 errno = sverr;
110                 return (psmb_result_t){PSMB_ERROR};
111         }
112         if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
113                         &on, sizeof(on)) < 0) {
114                 int sverr = errno;
115                 LOG(ctx, LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...): %m");
116                 close(ctx->fd);
117                 ctx->fd = -1;
118                 errno = sverr;
119                 return (psmb_result_t){PSMB_ERROR};
120         }
121         if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_PKTINFO,
122                         &on, sizeof(on)) < 0) {
123                 int sverr = errno;
124                 LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_PKTINFO, ...): %m");
125                 close(ctx->fd);
126                 ctx->fd = -1;
127                 errno = sverr;
128                 return (psmb_result_t){PSMB_ERROR};
129         }
130         if (bind(ctx->fd, (struct sockaddr *)&addr,
131                         sizeof(struct sockaddr)) == -1) {
132                 int sverr = errno;
133                 LOG(ctx, LOG_ERR, "bind(): %m");
134                 close(ctx->fd);
135                 ctx->fd = -1;
136                 errno = sverr;
137                 return (psmb_result_t){PSMB_ERROR};
138         }
139         return (psmb_result_t){PSMB_OK};
140 }
141
142 void psmb_destroy(psmb_ctx_t *ctx)
143 {
144         if (ctx->fd == -1) {
145                 LOG(ctx, LOG_ERR, "psmb_ctx is not open");
146         } else {
147                 if (close(ctx->fd) == -1)
148                         LOG(ctx, LOG_ERR, "close(): %m");
149         }
150         /* clean up the rest */
151         (*ctx->free)(ctx);
152 }