On Fri, Mar 31, 2017 at 07:45:31AM +0000, Luke Small wrote:

> I found the code from
> http://www.techdeviancy.com/uds.html, but there are many references to code
> like this on google with the line:
> "message_buffer[0] = 'F';".
> 
> I made an amalgamation of that code and the EXAMPLE section of CMSG_DATA(3):
> 
> EXAMPLES
> The following example constructs a control message containing a file
> descriptor and passes it over a socket:
> 
> struct msghdr msg;
> struct iovec io_vector[1];
> struct cmsghdr *cmsg;
> char message_buffer[1];
> char ancillary_buffer[CMSG_SPACE(sizeof(int))];
> 
> /*
>  * at least one vector of one byte must be
>  * sent or successive reads will fail.
>  */
> message_buffer[0] = 'F';
> io_vector[0].iov_base = message_buffer;
> io_vector[0].iov_len = 1;
> 
> memset(&msg, 0, sizeof(struct msghdr));
> msg.msg_iov = io_vector;
> msg.msg_iovlen = 1;
> memset(ancillary_buffer, 0, CMSG_SPACE(sizeof(int)));
> msg.msg_control = ancillary_buffer;
> msg.msg_controllen = CMSG_SPACE(sizeof(int));
> 
> cmsg = CMSG_FIRSTHDR(&msg);
> cmsg->cmsg_level = SOL_SOCKET;
> cmsg->cmsg_type = SCM_RIGHTS;
> cmsg->cmsg_len = CMSG_LEN(sizeof(int));
> *((int *) CMSG_DATA(cmsg)) = fd;
> 
> if (sendmsg(socket, &msg, 0) == -1)
>         err(1, "sendmsg");
> 
> And an example that receives and decomposes the control message:
> 
> struct msghdr    msg;
> struct cmsghdr  *cmsg;
> struct iovec io_vector[1];
> char message_buffer[1];
> char ancillary_buffer[CMSG_SPACE(sizeof(int))];
> 
> memset(&msg, 0, sizeof(struct msghdr));
> memset(ancillary_buffer, 0, CMSG_SPACE(sizeof(int)));
> io_vector[0].iov_base = message_buffer;
> io_vector[0].iov_len = 1;
> msg.msg_iov = io_vector;
> msg.msg_iovlen = 1;
> msg.msg_control = ancillary_buffer;
> msg.msg_controllen = CMSG_SPACE(sizeof(int));
> 
> if (recvmsg(socket, &msg, 0) == -1)
>         err(1, "recvmsg");
> if (message_buffer[0] != 'F')
>         errx(1, "corrupted message");
> if ((msg.msg_flags & MSG_TRUNC) || (msg.msg_flags & MSG_CTRUNC))
>         errx(1, "control message truncated");
> for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
>     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
>         if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
>             cmsg->cmsg_level == SOL_SOCKET &&
>             cmsg->cmsg_type == SCM_RIGHTS) {
>                 fd = *((int *) CMSG_DATA(cmsg));
>                 /* Do something with the descriptor. */
>         }
> }
> 
> 
> 
> 
> On Fri, Mar 31, 2017 at 12:03 AM Theo de Raadt <[email protected]> wrote:
> 
> > This is not a "i want help" group.
> >
> > If you have a diff, you can try submitting it.
> >
> > If you don't, your questions come off very false.
> >

If you want help, this is not the right mailing list. This list is for
bug reports.

And if you send stuff to anoyther mailing list show real, complete
example code demonstrating your problem, including a description of
expected and observed behaviour.

        -Otto


Reply via email to