1 | #include <sys/types.h>
|
---|
2 | #include <sys/socket.h>
|
---|
3 | #include <netinet/in.h>
|
---|
4 | #include <unistd.h>
|
---|
5 | #include <errno.h>
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <string.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 |
|
---|
10 | static int wrap_err(int ret)
|
---|
11 | {
|
---|
12 | if (ret < 0) {
|
---|
13 | fprintf(stderr, "ERROR: %s\n", strerror(errno));
|
---|
14 | exit(-1);
|
---|
15 | }
|
---|
16 | return ret;
|
---|
17 | }
|
---|
18 |
|
---|
19 | int main()
|
---|
20 | {
|
---|
21 | struct iovec iov;
|
---|
22 | char string[256];
|
---|
23 | struct msghdr msg;
|
---|
24 | int bufsize = 4194304;
|
---|
25 | struct sockaddr_in local_addr = {
|
---|
26 | .sin_family = AF_INET,
|
---|
27 | .sin_port = 0, // Pick whatever free local port
|
---|
28 | .sin_addr = 0x0100007F, // 127.0.0.1
|
---|
29 | };
|
---|
30 | socklen_t addr_len = sizeof(local_addr);
|
---|
31 | int listener = wrap_err(socket(AF_INET, SOCK_STREAM, 0));
|
---|
32 | int conn = wrap_err(socket(AF_INET, SOCK_STREAM, 0));
|
---|
33 |
|
---|
34 | wrap_err(bind(listener, (void*)&local_addr, addr_len));
|
---|
35 | wrap_err(listen(listener, SOMAXCONN));
|
---|
36 | wrap_err(getsockname(listener, (void*)&local_addr, &addr_len));
|
---|
37 | wrap_err(connect(conn, (void*)&local_addr, addr_len));
|
---|
38 |
|
---|
39 | int client = wrap_err(accept(listener, (void*)&local_addr, &addr_len));
|
---|
40 |
|
---|
41 | // At this point we have 2 connected socket and no longer need a listener
|
---|
42 | close(listener);
|
---|
43 |
|
---|
44 | setsockopt(client, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
|
---|
45 | setsockopt(client, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
|
---|
46 | setsockopt(conn, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
|
---|
47 | setsockopt(conn, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
|
---|
48 |
|
---|
49 | memset(&msg, 0, sizeof(msg));
|
---|
50 | msg.msg_iov = &iov;
|
---|
51 | msg.msg_iovlen = 1;
|
---|
52 | iov.iov_base = "Aiya!\n";
|
---|
53 | iov.iov_len = 6;
|
---|
54 | sendmsg(conn, &msg, 0);
|
---|
55 | iov.iov_base = "test";
|
---|
56 | iov.iov_len = 4;
|
---|
57 | sendmsg(conn, &msg, 0);
|
---|
58 | iov.iov_base = "1234567890";
|
---|
59 | iov.iov_len = 10;
|
---|
60 | sendmsg(conn, &msg, 0);
|
---|
61 | iov.iov_base = "1";
|
---|
62 | iov.iov_len = 1;
|
---|
63 | sendmsg(conn, &msg, 0);
|
---|
64 |
|
---|
65 | msg.msg_iov = &iov;
|
---|
66 | msg.msg_iovlen = 1;
|
---|
67 | iov.iov_base = "Aiya!\n";
|
---|
68 | iov.iov_len = 6;
|
---|
69 | sendmsg(client, &msg, 0);
|
---|
70 | iov.iov_base = "test";
|
---|
71 | iov.iov_len = 4;
|
---|
72 | sendmsg(client, &msg, 0);
|
---|
73 | iov.iov_base = "1234567890";
|
---|
74 | iov.iov_len = 10;
|
---|
75 | sendmsg(client, &msg, 0);
|
---|
76 | iov.iov_base = "1";
|
---|
77 | iov.iov_len = 1;
|
---|
78 | sendmsg(client, &msg, 0);
|
---|
79 |
|
---|
80 | iov.iov_base = string;
|
---|
81 | iov.iov_len = 6;
|
---|
82 | recvmsg(conn, &msg, 0);
|
---|
83 | iov.iov_len = 4;
|
---|
84 | recvmsg(conn, &msg, 0);
|
---|
85 | iov.iov_len = 10;
|
---|
86 | recvmsg(conn, &msg, 0);
|
---|
87 | iov.iov_len = 1;
|
---|
88 | recvmsg(conn, &msg, 0);
|
---|
89 |
|
---|
90 | iov.iov_len = 6;
|
---|
91 | recvmsg(client, &msg, 0);
|
---|
92 | iov.iov_len = 4;
|
---|
93 | recvmsg(client, &msg, 0);
|
---|
94 | iov.iov_len = 10;
|
---|
95 | recvmsg(client, &msg, 0);
|
---|
96 | iov.iov_len = 1;
|
---|
97 | recvmsg(client, &msg, 0);
|
---|
98 |
|
---|
99 | close(conn);
|
---|
100 | close(client);
|
---|
101 |
|
---|
102 | printf("Test passed!\n");
|
---|
103 | }
|
---|