WIP on send/recv
authorEugene Crosser <crosser@average.org>
Thu, 28 Mar 2019 17:19:05 +0000 (18:19 +0100)
committerEugene Crosser <evgenii.cherkashin@cloud.ionos.com>
Thu, 28 Mar 2019 17:19:05 +0000 (18:19 +0100)
src/psmb_socket.c

index 634be8e4b3bfa7358f7fbf05268ed0cc76875871..41564a0279abacebdeb870c8319035e40bf23ac0 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include "psmb_priv.h"
 #include "hash64.h"
 
+/* #include <linux/ipv6.h> // should give us this definition */
+struct in6_pktinfo {
+       struct in6_addr ipi6_addr;
+       int             ipi6_ifindex;
+};
+
 static void dummy_log(void *log_priv, int priority, const char *format, ...) {}
 
 psmb_ctx_t *psmb_new(void)
@@ -135,7 +142,7 @@ psmb_result_t psmb_open(psmb_ctx_t *ctx)
        if (setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
                        &on, sizeof(on)) < 0) {
                int sverr = errno;
-               LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_PKTINFO, ...): %m");
+               LOG(ctx, LOG_ERR, "setsockopt(..., IPV6_RECVPKTINFO, ...): %m");
                close(ctx->fd);
                ctx->fd = -1;
                errno = sverr;
@@ -205,40 +212,61 @@ psmb_result_t psmb_unsubscribe(psmb_ctx_t *ctx, char *channel)
 
 psmb_result_t psmb_ev_rd(psmb_ctx_t *ctx)
 {
-       char buf[MAXBUFLEN];
-       char cmsgbuf[MAXBUFLEN];
-       struct iovec iov[1];
-       iov[0].iov_base=buf;
-       iov[0].iov_len=sizeof(buf);
+       ssize_t readsize;
+       struct sockaddr_in6 peer_addr;
+       struct in6_addr self_addr = {{{0}}};
+       unsigned char msgbuf[BUFSIZ];
+       unsigned char cmsgbuf[BUFSIZ];
+       struct iovec iov[1] = {{
+               .iov_base=msgbuf,
+               .iov_len=sizeof(msgbuf),
+       }};
+       struct msghdr message = {
+               .msg_name=&peer_addr,
+               .msg_namelen=sizeof(peer_addr),
+               .msg_iov=iov,
+               .msg_iovlen=1,
+               .msg_control=cmsgbuf,
+               .msg_controllen=sizeof(cmsgbuf),
+       };
        struct cmsghdr *cmsg;
-       struct msghdr message;
-       message.msg_name=&their_addr;
-message.msg_namelen=sizeof(their_addr);
-message.msg_iov=iov;
-message.msg_iovlen=1;
-message.msg_control=cmsgbuf;
-message.msg_controllen=MAXBUFLEN;
+       char peer_str[INET6_ADDRSTRLEN+1];
+       char self_str[INET6_ADDRSTRLEN+1];
 
-       if ((numbytes = recvmsg(sockfd, &message, 0)) == -1) {
-               /* error */ ;
+       if ((readsize = recvmsg(ctx->fd, &message, 0)) == -1) {
+               if (errno == EWOULDBLOCK)
+                       return (psmb_result_t){PSMB_OK};
+               else {
+                       int sverr = errno;
+                       LOG(ctx, LOG_ERR, "recvmsg(..., 0): %m");
+                       errno = sverr;
+                       return (psmb_result_t){PSMB_ERROR};
+               }
        }
-       for (cmsg = CMSG_FIRSTHDR(&message); cmsg != NULL; cmsg = CMSG_NXTHDR(&message, cmsg)) {
-               if (cmsg->cmsg_level != IPPROTO_IP || cmsg->cmsg_type != IP_PKTINFO)
-                       continue;
-               struct in_pktinfo *pi = CMSG_DATA(cmsg);
-               addr = pi->ipi_spec_dst.s_addr;
+       for (cmsg = CMSG_FIRSTHDR(&message);
+            cmsg != NULL;
+            cmsg = CMSG_NXTHDR(&message, cmsg)) {
+               LOG(ctx, LOG_DEBUG, "CMSG: level %d, type %d - skip",
+                                       cmsg->cmsg_level, cmsg->cmsg_type);
+               if (cmsg->cmsg_level == IPPROTO_IPV6 &&
+                               cmsg->cmsg_type == IPV6_PKTINFO) {
+                       struct in6_pktinfo *pi =
+                               (struct in6_pktinfo *)CMSG_DATA(cmsg);
+                       self_addr = pi->ipi6_addr;
+               }
        }
-       
+       (void)inet_ntop(AF_INET6, &peer_addr.sin6_addr,
+                       peer_str, sizeof(peer_str));
+       (void)inet_ntop(AF_INET6, &self_addr,
+                       self_str, sizeof(self_str));
+       LOG(ctx, LOG_DEBUG, "CMSG: %d bytes from %s to %s",
+                       readsize, peer_str, self_str);
 
        return (psmb_result_t){PSMB_OK};
 }
 
 psmb_result_t psmb_ev_wr(psmb_ctx_t *ctx)
 {
-       sock_opt_addr.s_addr = addr;
-       setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &sock_opt_addr, sizeof(sock_opt_addr));
-       sendto(sockfd, buffer, len, 0, (struct sockaddr *)&destination, sizeof destination);
-
        return (psmb_result_t){PSMB_OK};
 }
 
@@ -250,6 +278,12 @@ psmb_result_t psmb_ev_ex(psmb_ctx_t *ctx)
 psmb_result_t psmb_publish(psmb_ctx_t *ctx, char *channel,
        void *data, size_t size)
 {
+       /*
+       unsigned long ifindex = 0;
+       setsockopt(ctx->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
+                       &ifindex, sizeof(ifindex));
+       */
+
        return (psmb_result_t){PSMB_OK};
 }