swrap: Add swrap_sendmsg_copy_cmsg().
authorAndreas Schneider <asn@samba.org>
Tue, 3 Jun 2014 13:03:41 +0000 (15:03 +0200)
committerMichael Adam <obnox@samba.org>
Thu, 5 Jun 2014 21:57:10 +0000 (23:57 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
lib/socket_wrapper/socket_wrapper.c

index 26750b0c0828daf3c2c2fd00b00b8197b73c90a5..5212cadd3568a1b075b2533c0543e1a201363b7e 100644 (file)
@@ -2995,6 +2995,15 @@ int ioctl(int s, unsigned long int r, ...)
  *****************/
 
 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
+
+#ifndef CMSG_ALIGN
+# ifdef _ALIGN /* BSD */
+#define CMSG_ALIGN _ALIGN
+# else
+#error NO_CMSG_ALIGN
+# endif /* _ALIGN */
+#endif /* CMSG_ALIGN */
+
 /**
  * @brief Add a cmsghdr to a msghdr.
  *
@@ -3145,6 +3154,10 @@ static int swrap_msghdr_add_socket_info(struct socket_info *si,
        return rc;
 }
 
+static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
+                                  uint8_t *cm_data,
+                                  size_t *cm_data_space);
+
 static int swrap_sendmsg_filter_cmsghdr(struct msghdr *msg,
                                        uint8_t *cm_data,
                                        size_t *cm_data_space) {
@@ -3164,13 +3177,40 @@ static int swrap_sendmsg_filter_cmsghdr(struct msghdr *msg,
                        /* TODO swrap_sendmsg_filter_cmsg_socket */
                        break;
                default:
-                       /* TODO swrap_sendmsg_copy_cmsg */
+                       rc = swrap_sendmsg_copy_cmsg(cmsg,
+                                                    cm_data,
+                                                    cm_data_space);
                        break;
                }
        }
 
        return rc;
 }
+
+static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
+                                  uint8_t *cm_data,
+                                  size_t *cm_data_space)
+{
+       size_t cmspace;
+       uint8_t *p;
+
+       cmspace =
+               (*cm_data_space) +
+               CMSG_SPACE(cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)));
+
+       p = realloc(cm_data, cmspace);
+       if (p == NULL) {
+               return -1;
+       }
+       cm_data = p;
+
+       p = cm_data + (*cm_data_space);
+       *cm_data_space = cmspace;
+
+       memcpy(p, cmsg, cmsg->cmsg_len);
+
+       return 0;
+}
 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
 
 static ssize_t swrap_sendmsg_before(int fd,