Skip to contentSkip to conversation

sendmmsg.2: Updated fixme, added example

PATCH In review Elie De Brauwer 2 participants 4 messages Dec 14, 2012 – Dec 15, 2012 3 messages not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sendmmsg.2: Updated fixme, added example

Hello all,

A patch which updates a fixme which extends man 2 sendmmsg with an example.

The example uses sendmmsg to sends out a string "onetwo" on a first datagram, where both halves originate from distinct buffers and a second datagram contains "three", coming from a single buffer.

Tested with netcat listening:

root@ubuntu:~# nc -l -u -p 1234
onetwothree

And tcpdump peeking:

root@ubuntu:~# tcpdump -c 2 -s 0 -X -ni lo tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
18:45:16.632134 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 6
     0x0000:  4500 0022 c21c 4000 4011 7aac 7f00 0001  E.."..@[email protected].....
     0x0010:  7f00 0001 879b 04d2 000e fe21 6f6e 6574  ...........!onet
     0x0020:  776f                                     wo
18:45:16.633267 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 5
     0x0000:  4500 0021 c21d 4000 4011 7aac 7f00 0001  E..!..@[email protected].....
     0x0010:  7f00 0001 879b 04d2 000d fe20 7468 7265  ............thre
     0x0020:  65                                       e
2 packets captured
4 packets received by filter
0 packets dropped by kernel

my 2 cents
E.

---
 man2/sendmmsg.2 |   69 
+++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)
man2/sendmmsg.2 +66 -2
index 12ad3ff..5027619 100644
@@ -23,8 +23,6 @@
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
 .\"
-.\" FIXME Adding an example program would improve this page
-.\"
 .TH SENDMMSG 2 2012-02-27 "Linux" "Linux Programmer's Manual"
 .SH NAME
 sendmmsg \- send multiple messages on a socket
@@ -165,6 +163,73 @@ is capped to
 .\"     For error handling an application using sendmmsg needs to retry at
 .\"     the first unsent message, so capping is simpler and requires less
 .\"     application logic than returning EINVAL.
+.SH EXAMPLE
+The example below uses
+.BR sendmmsg ()
+to send
+.I onetwo
+and
+.I three
+in two distinct UDP datagrams using one system call. Where the contents
+of the first datagram originates from multiple buffers.
+
+.nf
+#define _GNU_SOURCE
+#include <netinet/ip.h> +#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+int main()
+{
+    int sockfd;
+    struct sockaddr_in sa;
+    struct mmsghdr msg[2];
+    struct iovec msg1[2], msg2;
+    int retval;
+
+    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sockfd == -1) {
+        perror("socket()");
+        exit(EXIT_FAILURE);
+    }
+
+    sa.sin_family = AF_INET;
+    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    sa.sin_port = htons(1234);
+    if (connect(sockfd, (struct sockaddr *)&sa, sizeof(sa))) {
+        perror("connect()");
+        exit(EXIT_FAILURE);
+    }
+
+    memset(msg1, 0, sizeof(msg1));
+    msg1[0].iov_base = "one";
+    msg1[0].iov_len = 3;
+    msg1[1].iov_base = "two";
+    msg1[1].iov_len = 3;
+
+    memset(&msg2, 0, sizeof(msg2));
+    msg2.iov_base = "three";
+    msg2.iov_len = 5;
+
+    memset(msg, 0, sizeof(msg));
+    msg[0].msg_hdr.msg_iov = msg1;
+    msg[0].msg_hdr.msg_iovlen = 2;
+
+    msg[1].msg_hdr.msg_iov = &msg2;
+    msg[1].msg_hdr.msg_iovlen = 1;
+
+    retval = sendmmsg(sockfd, msg, 2, 0);
+    if (retval == -1)
+        perror("sendmmsg()");
+    else
+        printf("%d messages sent\n", retval);
+
+    exit(0);
+}
+.fi
 .SH SEE ALSO
 .BR recvmmsg (2),
 .BR sendmsg (2),
Signature · 1 line
1.7.10.4

Hello all,

A patch which updates a fixme and which extends sendmmsg with an example.

The example uses sendmmsg to sends out a string "onetwo" on a first datagram, where both halves originate from distinct buffers and a second datagram contains "three", coming from a single buffer.

Tested with netcat listening:

root@ubuntu:~# nc -l -u -p 1234
onetwothree

And tcpdump peeking:

root@ubuntu:~# tcpdump -c 2 -s 0 -X -ni lo 
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
18:45:16.632134 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 6
        0x0000:  4500 0022 c21c 4000 4011 7aac 7f00 0001  E.."..@[email protected].....
        0x0010:  7f00 0001 879b 04d2 000e fe21 6f6e 6574  ...........!onet
        0x0020:  776f                                     wo
18:45:16.633267 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 5
        0x0000:  4500 0021 c21d 4000 4011 7aac 7f00 0001  E..!..@[email protected].....
        0x0010:  7f00 0001 879b 04d2 000d fe20 7468 7265  ............thre
        0x0020:  65                                       e
2 packets captured
4 packets received by filter
0 packets dropped by kernel

my 2 cents
E.

---
 man2/sendmmsg.2 |   69 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)
man2/sendmmsg.2 +67 -2

81 lines not on this page · raw message

Signature · 1 line
1.7.10.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

Hello Elie,

Thanks for this patch. See comments below.

Quoted 4 lines · Elie De Brauwer

Hello all,

A patch which updates a fixme which extends man 2 sendmmsg with an example.

Quoted 8 lines

The example uses sendmmsg to sends out a string "onetwo" on a first datagram,
where both halves originate from distinct buffers and a second datagram contains "three", coming from a single buffer.

Tested with netcat listening:

root@ubuntu:~# nc -l -u -p 1234
onetwothree

Could you work something like the above into the patch, as a shell session that demonstrates the use of the program.

Quoted 38 lines

And tcpdump peeking:

root@ubuntu:~# tcpdump -c 2 -s 0 -X -ni lo tcpdump: verbose output
suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
18:45:16.632134 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 6
     0x0000:  4500 0022 c21c 4000 4011 7aac 7f00 0001  E.."..@[email protected].....
     0x0010:  7f00 0001 879b 04d2 000e fe21 6f6e 6574  ...........!onet
     0x0020:  776f                                     wo
18:45:16.633267 IP 127.0.0.1.34715 > 127.0.0.1.1234: UDP, length 5
     0x0000:  4500 0021 c21d 4000 4011 7aac 7f00 0001  E..!..@[email protected].....
     0x0010:  7f00 0001 879b 04d2 000d fe20 7468 7265  ............thre
     0x0020:  65                                       e
2 packets captured
4 packets received by filter
0 packets dropped by kernel

my 2 cents
E.

---
 man2/sendmmsg.2 |   69
+++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)
man2/sendmmsg.2 +0 -2
index 12ad3ff..5027619 100644
@@ -23,8 +23,6 @@
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
 .\"
-.\" FIXME Adding an example program would improve this page
-.\"
 .TH SENDMMSG 2 2012-02-27 "Linux" "Linux Programmer's Manual"
 .SH NAME
 sendmmsg \- send multiple messages on a socket
@@ -165,6 +163,73 @@ is capped to
 .\"     For error handling an application using sendmmsg needs to retry at

man2/sendmmsg.2 is capped to · line 173

Quoted 10 lines
man2/sendmmsg.2 +8 -0
 .\"     the first unsent message, so capping is simpler and requires less
 .\"     application logic than returning EINVAL.
+.SH EXAMPLE
+The example below uses
+.BR sendmmsg ()
+to send
+.I onetwo
+and
+.I three
+in two distinct UDP datagrams using one system call. Where the contents

s/Where the/The/

man2/sendmmsg.2 is capped to · line 174

Quoted 1 line
man2/sendmmsg.2 +1 -0
+of the first datagram originates from multiple buffers.

s/multiple/a pair of/

man2/sendmmsg.2 is capped to · line 178

Quoted 4 lines
man2/sendmmsg.2 +4 -0
+
+.nf
+#define _GNU_SOURCE
+#include <netinet/ip.h> +#include <stdio.h>

Broken line above.

man2/sendmmsg.2 is capped to · line 184

Quoted 6 lines
man2/sendmmsg.2 +6 -0
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+int main()

int
main()

[K&R style please.]

man2/sendmmsg.2 is capped to · line 193

Quoted 9 lines
man2/sendmmsg.2 +9 -0
+{
+    int sockfd;
+    struct sockaddr_in sa;
+    struct mmsghdr msg[2];
+    struct iovec msg1[2], msg2;
+    int retval;
+
+    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sockfd == -1) {

All instances of '-' in code should be '\-'

man2/sendmmsg.2 is capped to · line 201

Quoted 8 lines
man2/sendmmsg.2 +8 -0
+        perror("socket()");
+        exit(EXIT_FAILURE);
+    }
+
+    sa.sin_family = AF_INET;
+    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    sa.sin_port = htons(1234);
+    if (connect(sockfd, (struct sockaddr *)&sa, sizeof(sa))) {
    if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {

(Space before '&' please.)

Quoted 16 lines
man2/sendmmsg.2 +16 -0
+        perror("connect()");
+        exit(EXIT_FAILURE);
+    }
+
+    memset(msg1, 0, sizeof(msg1));
+    msg1[0].iov_base = "one";
+    msg1[0].iov_len = 3;
+    msg1[1].iov_base = "two";
+    msg1[1].iov_len = 3;
+
+    memset(&msg2, 0, sizeof(msg2));
+    msg2.iov_base = "three";
+    msg2.iov_len = 5;
+
+    memset(msg, 0, sizeof(msg));
+    msg[0].msg_hdr.msg_iov = msg1;

man2/sendmmsg.2 is capped to · line 227

Quoted 10 lines
man2/sendmmsg.2 +10 -0
+    msg[0].msg_hdr.msg_iovlen = 2;
+
+    msg[1].msg_hdr.msg_iov = &msg2;
+    msg[1].msg_hdr.msg_iovlen = 1;
+
+    retval = sendmmsg(sockfd, msg, 2, 0);
+    if (retval == -1)
+        perror("sendmmsg()");
+    else
+        printf("%d messages sent\n", retval);

Make the \n into \\n

man2/sendmmsg.2 is capped to · line 234

Quoted 7 lines
man2/sendmmsg.2 +4 -0
+
+    exit(0);
+}
+.fi
 .SH SEE ALSO
 .BR recvmmsg (2),
 .BR sendmsg (2),

Thanks,

Michael

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAKgNAkj9MaU5fHbJMEwSDgh_vhmmNxfA9fq7uJYCCXD-=NAapQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#4 lore raw

PATCH 1/1 sendmmsg.2: Updated fixme, added example

Hello all,

Updated my patch with the comment from Michael (thanks for the feedback btw). I however did not include the shell session using netcat for the simple reason that the output of netcat does not illustrate that the call functions correctly. The output of netcat would be the same no matter how much UDP datagrams were created in the transmission, the only reason why I showed it in my previous e-mail was to avoid the generation of icmp port unreachable which would break my tcpdump capture. This latter one is what I really wanted to show, but i think that one's a bit too far fetched to be held in a manpage.

gr
E.

---
 man2/sendmmsg.2 |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 2 deletions(-)
man2/sendmmsg.2 +68 -2
index 12ad3ff..b426b4d 100644
@@ -23,8 +23,6 @@
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
 .\"
-.\" FIXME Adding an example program would improve this page
-.\"
 .TH SENDMMSG 2 2012-02-27 "Linux" "Linux Programmer's Manual"
 .SH NAME
 sendmmsg \- send multiple messages on a socket
@@ -165,6 +163,74 @@ is capped to
 .\"     For error handling an application using sendmmsg needs to retry at
 .\"     the first unsent message, so capping is simpler and requires less
 .\"     application logic than returning EINVAL.
+.SH EXAMPLE
+The example below uses 
+.BR sendmmsg ()
+to send  
+.I onetwo
+and 
+.I three
+in two distinct UDP datagrams using one system call. The contents
+of the first datagram originates from a pair of buffers.
+
+.nf
+#define _GNU_SOURCE
+#include <netinet/ip.h> 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+int 
+main()
+{
+    int sockfd;
+    struct sockaddr_in sa;
+    struct mmsghdr msg[2];
+    struct iovec msg1[2], msg2;
+    int retval;
+
+    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sockfd == \-1) {
+        perror("socket()");
+        exit(EXIT_FAILURE);
+    }
+
+    sa.sin_family = AF_INET;             
+    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);  
+    sa.sin_port = htons(1234);  
+    if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa))) {
+        perror("connect()");
+        exit(EXIT_FAILURE);
+    }
+
+    memset(msg1, 0, sizeof(msg1)); 
+    msg1[0].iov_base = "one";
+    msg1[0].iov_len = 3;
+    msg1[1].iov_base = "two";
+    msg1[1].iov_len = 3;
+
+    memset(&msg2, 0, sizeof(msg2));
+    msg2.iov_base = "three";
+    msg2.iov_len = 5;
+
+    memset(msg, 0, sizeof(msg));
+    msg[0].msg_hdr.msg_iov = msg1;
+    msg[0].msg_hdr.msg_iovlen = 2;
+
+    msg[1].msg_hdr.msg_iov = &msg2;
+    msg[1].msg_hdr.msg_iovlen = 1;
+
+    retval = sendmmsg(sockfd, msg, 2, 0);
+    if (retval == -1)
+        perror("sendmmsg()");
+    else
+        printf("%d messages sent\\n", retval);
+
+    exit(0);
+}
+.fi
 .SH SEE ALSO
 .BR recvmmsg (2),
 .BR sendmsg (2),
Signature · 6 lines
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1355593447-9170-1-git-send-email-eliedebrauwer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw

Hello Elie,

Quoted 11 lines · Elie De Brauwer

Hello all,

Updated my patch with the comment from Michael (thanks for the feedback btw). I however did not include the shell session using netcat for the simple reason that the output of netcat does not illustrate that the call functions correctly. The output of netcat would be the same no matter how much UDP datagrams were created in the transmission, the only reason why I showed it in my previous e-mail was to avoid the generation of icmp port unreachable which would break my tcpdump capture. This latter one is what I really wanted to show, but i think that one's a bit too far fetched to be held in a manpage.

Fair enough.

I've applied this patch for 3.45.

Cheers,

Michael

Quoted 95 lines
---
 man2/sendmmsg.2 |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 2 deletions(-)
man2/sendmmsg.2 +68 -3
index 12ad3ff..b426b4d 100644
@@ -23,8 +23,6 @@
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
 .\"
-.\" FIXME Adding an example program would improve this page
-.\"
 .TH SENDMMSG 2 2012-02-27 "Linux" "Linux Programmer's Manual"
 .SH NAME
 sendmmsg \- send multiple messages on a socket
@@ -165,6 +163,74 @@ is capped to
 .\"     For error handling an application using sendmmsg needs to retry at
 .\"     the first unsent message, so capping is simpler and requires less
 .\"     application logic than returning EINVAL.
+.SH EXAMPLE
+The example below uses
+.BR sendmmsg ()
+to send
+.I onetwo
+and
+.I three
+in two distinct UDP datagrams using one system call. The contents
+of the first datagram originates from a pair of buffers.
+
+.nf
+#define _GNU_SOURCE
+#include <netinet/ip.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+int
+main()
+{
+    int sockfd;
+    struct sockaddr_in sa;
+    struct mmsghdr msg[2];
+    struct iovec msg1[2], msg2;
+    int retval;
+
+    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sockfd == \-1) {
+        perror("socket()");
+        exit(EXIT_FAILURE);
+    }
+
+    sa.sin_family = AF_INET;
+    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    sa.sin_port = htons(1234);
+    if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa))) {
+        perror("connect()");
+        exit(EXIT_FAILURE);
+    }
+
+    memset(msg1, 0, sizeof(msg1));
+    msg1[0].iov_base = "one";
+    msg1[0].iov_len = 3;
+    msg1[1].iov_base = "two";
+    msg1[1].iov_len = 3;
+
+    memset(&msg2, 0, sizeof(msg2));
+    msg2.iov_base = "three";
+    msg2.iov_len = 5;
+
+    memset(msg, 0, sizeof(msg));
+    msg[0].msg_hdr.msg_iov = msg1;
+    msg[0].msg_hdr.msg_iovlen = 2;
+
+    msg[1].msg_hdr.msg_iov = &msg2;
+    msg[1].msg_hdr.msg_iovlen = 1;
+
+    retval = sendmmsg(sockfd, msg, 2, 0);
+    if (retval == -1)
+        perror("sendmmsg()");
+    else
+        printf("%d messages sent\\n", retval);
+
+    exit(0);
+}
+.fi
 .SH SEE ALSO
 .BR recvmmsg (2),
 .BR sendmsg (2),
--

1.7.10.4

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html