5 #include <netinet/in.h>
6 #include <sys/socket.h>
11 psmb_ctx_t *psmb_new(void)
13 return psmb_new_mm(malloc, free, realloc);
16 psmb_ctx_t *psmb_new_mm(void *(*malloc)(size_t size),
17 void (*free)(void *ptr),
18 void *(*realloc)(void *ptr, size_t size))
20 psmb_ctx_t *ctx = (*malloc)(sizeof(psmb_ctx_t));
25 .malloc = malloc, .free = free, .realloc = realloc,
26 .pmtu = PSMB_DEFAULT_PMTU, .port = PSMB_DEFAULT_PORT};
30 psmb_result_t psmb_set_pmtu(psmb_ctx_t *ctx, unsigned int pmtu)
34 return (psmb_result_t){PSMB_OK};
37 return (psmb_result_t){PSMB_ERROR};
41 psmb_result_t psmb_set_port(psmb_ctx_t *ctx, unsigned short port)
45 return (psmb_result_t){PSMB_OK};
48 return (psmb_result_t){PSMB_ERROR};
52 psmb_result_t psmb_open(psmb_ctx_t *ctx)
55 struct sockaddr_in6 addr = (struct sockaddr_in6){
56 .sin6_family = AF_INET6,
57 .sin6_addr = in6addr_any,
58 .sin6_port = htons(ctx->port)
63 return (psmb_result_t){PSMB_ERROR};
65 ctx->fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IPV6);
67 return (psmb_result_t){PSMB_ERROR};
69 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
70 &on, sizeof(on)) < 0) {
73 return (psmb_result_t){PSMB_ERROR};
75 if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_PKTINFO,
76 &on, sizeof(on)) < 0) {
79 return (psmb_result_t){PSMB_ERROR};
81 if (bind(ctx->fd, (struct sockaddr *)&addr,
82 sizeof(struct sockaddr)) == -1) {
85 return (psmb_result_t){PSMB_ERROR};
87 return (psmb_result_t){PSMB_OK};