1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2018 Google Inc.
4 * Author: Soheil Hassas Yeganeh (soheil@google.com)
6 * Simple example on how to use TCP_INQ and TCP_CM_INQ.
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
18 #include <sys/socket.h>
26 #define TCP_CM_INQ TCP_INQ
32 static int family
= AF_INET6
;
33 static socklen_t addr_len
= sizeof(struct sockaddr_in6
);
34 static int port
= 4974;
36 static void setup_loopback_addr(int family
, struct sockaddr_storage
*sockaddr
)
38 struct sockaddr_in6
*addr6
= (void *) sockaddr
;
39 struct sockaddr_in
*addr4
= (void *) sockaddr
;
43 memset(addr4
, 0, sizeof(*addr4
));
44 addr4
->sin_family
= AF_INET
;
45 addr4
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
46 addr4
->sin_port
= htons(port
);
49 memset(addr6
, 0, sizeof(*addr6
));
50 addr6
->sin6_family
= AF_INET6
;
51 addr6
->sin6_addr
= in6addr_loopback
;
52 addr6
->sin6_port
= htons(port
);
55 error(1, 0, "illegal family");
59 void *start_server(void *arg
)
61 int server_fd
= (int)(unsigned long)arg
;
62 struct sockaddr_in addr
;
63 socklen_t addrlen
= sizeof(addr
);
68 buf
= malloc(BUF_SIZE
);
71 fd
= accept(server_fd
, (struct sockaddr
*)&addr
, &addrlen
);
77 r
= send(fd
, buf
, BUF_SIZE
, 0);
78 } while (r
< 0 && errno
== EINTR
);
82 fprintf(stderr
, "can only send %d bytes\n", r
);
83 /* TCP_INQ can overestimate in-queue by one byte if we send
84 * the FIN packet. Sleep for 1 second, so that the client
85 * likely invoked recvmsg().
96 int main(int argc
, char *argv
[])
98 struct sockaddr_storage listen_addr
, addr
;
99 int c
, one
= 1, inq
= -1;
100 pthread_t server_thread
;
101 char cmsgbuf
[CMSG_SIZE
];
108 while ((c
= getopt(argc
, argv
, "46p:")) != -1) {
112 addr_len
= sizeof(struct sockaddr_in
);
116 addr_len
= sizeof(struct sockaddr_in6
);
124 server_fd
= socket(family
, SOCK_STREAM
, 0);
126 error(1, errno
, "server socket");
127 setup_loopback_addr(family
, &listen_addr
);
128 if (setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
,
129 &one
, sizeof(one
)) != 0)
130 error(1, errno
, "setsockopt(SO_REUSEADDR)");
131 if (bind(server_fd
, (const struct sockaddr
*)&listen_addr
,
133 error(1, errno
, "bind");
134 if (listen(server_fd
, 128) == -1)
135 error(1, errno
, "listen");
136 if (pthread_create(&server_thread
, NULL
, start_server
,
137 (void *)(unsigned long)server_fd
) != 0)
138 error(1, errno
, "pthread_create");
140 fd
= socket(family
, SOCK_STREAM
, 0);
142 error(1, errno
, "client socket");
143 setup_loopback_addr(family
, &addr
);
144 if (connect(fd
, (const struct sockaddr
*)&addr
, addr_len
) == -1)
145 error(1, errno
, "connect");
146 if (setsockopt(fd
, SOL_TCP
, TCP_INQ
, &one
, sizeof(one
)) != 0)
147 error(1, errno
, "setsockopt(TCP_INQ)");
153 msg
.msg_control
= cmsgbuf
;
154 msg
.msg_controllen
= sizeof(cmsgbuf
);
157 buf
= malloc(BUF_SIZE
);
158 iov
[0].iov_base
= buf
;
159 iov
[0].iov_len
= BUF_SIZE
/ 2;
161 if (recvmsg(fd
, &msg
, 0) != iov
[0].iov_len
)
162 error(1, errno
, "recvmsg");
163 if (msg
.msg_flags
& MSG_CTRUNC
)
164 error(1, 0, "control message is truncated");
166 for (cm
= CMSG_FIRSTHDR(&msg
); cm
; cm
= CMSG_NXTHDR(&msg
, cm
))
167 if (cm
->cmsg_level
== SOL_TCP
&& cm
->cmsg_type
== TCP_CM_INQ
)
168 inq
= *((int *) CMSG_DATA(cm
));
170 if (inq
!= BUF_SIZE
- iov
[0].iov_len
) {
171 fprintf(stderr
, "unexpected inq: %d\n", inq
);