Browse code

Modified --port-share code to remove the assumption that CMSG_SPACE always evaluates to a constant, to enable compilation on NetBSD and possibly other BSDs as well.

git-svn-id: http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn@1041 e7ae566f-a301-0410-adde-c780ea21d3b5

james authored on 2006/06/24 17:10:10
Showing 2 changed files
... ...
@@ -3019,11 +3019,7 @@ Currently only designed to work with HTTP/HTTPS,
3019 3019
 though it would be theoretically possible to extend to
3020 3020
 other protocols such as ssh.
3021 3021
 
3022
-Currently only implemented
3023
-on Linux, though porting to BSDs should be straightforward.
3024
-The reason for the non-portability is that the current implementation
3025
-uses sendmsg and recvmsg for passing file descriptors between
3026
-processes.
3022
+Not implemented on Windows.
3027 3023
 .\"*********************************************************
3028 3024
 .SS Client Mode
3029 3025
 Use client mode when connecting to an OpenVPN server
... ...
@@ -76,12 +76,6 @@ struct proxy_connection {
76 76
   int sd;
77 77
 };
78 78
 
79
-/* used for passing fds between processes */
80
-union fdmsg {
81
-  struct cmsghdr h;
82
-  char buf[CMSG_SPACE(sizeof(socket_descriptor_t))];
83
-};
84
-
85 79
 #if 0
86 80
 static const char *
87 81
 headc (const struct buffer *buf)
... ...
@@ -166,6 +160,12 @@ send_control (const socket_descriptor_t fd, int code)
166 166
     return -1;
167 167
 }
168 168
 
169
+static int
170
+cmsg_size ()
171
+{
172
+  return CMSG_SPACE(sizeof(socket_descriptor_t));
173
+}
174
+
169 175
 /*
170 176
  * Send a command (char), data (head), and a file descriptor (sd_send) to a local process
171 177
  * over unix socket sd.  Unfortunately, there's no portable way to send file descriptors
... ...
@@ -182,7 +182,6 @@ port_share_sendmsg (const socket_descriptor_t sd,
182 182
   if (socket_defined (sd))
183 183
     {
184 184
       struct msghdr mesg;
185
-      union fdmsg cmsg;
186 185
       struct cmsghdr* h;
187 186
       struct iovec iov[2];
188 187
       socket_descriptor_t sd_null[2] = { SOCKET_UNDEFINED, SOCKET_UNDEFINED };
... ...
@@ -210,8 +209,9 @@ port_share_sendmsg (const socket_descriptor_t sd,
210 210
 
211 211
       mesg.msg_iov = iov;
212 212
 
213
-      mesg.msg_control = cmsg.buf;
214
-      mesg.msg_controllen = sizeof (union fdmsg);
213
+      mesg.msg_controllen = cmsg_size ();
214
+      mesg.msg_control = (char *) malloc (mesg.msg_controllen);
215
+      check_malloc_return (mesg.msg_control);
215 216
       mesg.msg_flags = 0;
216 217
 
217 218
       h = CMSG_FIRSTHDR(&mesg);
... ...
@@ -235,6 +235,7 @@ port_share_sendmsg (const socket_descriptor_t sd,
235 235
 
236 236
       close_socket_if_defined (sd_null[0]);
237 237
       close_socket_if_defined (sd_null[1]);
238
+      free (mesg.msg_control);
238 239
     }
239 240
 }
240 241
 
... ...
@@ -437,7 +438,6 @@ control_message_from_parent (const socket_descriptor_t sd_control,
437 437
 {
438 438
   struct buffer buf = alloc_buf (PROXY_CONNECTION_BUFFER_SIZE);
439 439
   struct msghdr mesg;
440
-  union fdmsg cmsg;
441 440
   struct cmsghdr* h;
442 441
   struct iovec iov[2];
443 442
   char command = 0;
... ...
@@ -453,8 +453,9 @@ control_message_from_parent (const socket_descriptor_t sd_control,
453 453
   mesg.msg_iov = iov;
454 454
   mesg.msg_iovlen = 2;
455 455
 
456
-  mesg.msg_control = cmsg.buf;
457
-  mesg.msg_controllen = sizeof (union fdmsg);
456
+  mesg.msg_controllen = cmsg_size ();
457
+  mesg.msg_control = (char *) malloc (mesg.msg_controllen);
458
+  check_malloc_return (mesg.msg_control);
458 459
   mesg.msg_flags = 0;
459 460
 
460 461
   h = CMSG_FIRSTHDR(&mesg);
... ...
@@ -503,6 +504,7 @@ control_message_from_parent (const socket_descriptor_t sd_control,
503 503
 	    }
504 504
 	}
505 505
     }
506
+  free (mesg.msg_control);
506 507
   free_buf (&buf);
507 508
   return ret;
508 509
 }