xref: /DragonOS/user/apps/test_bind/main.c (revision bc6f0a967c8cb1e9379ced184b25a7722fbda2a4)
1*821bb9a2SXshine #include <arpa/inet.h>
2*821bb9a2SXshine #include <fcntl.h>
3*821bb9a2SXshine #include <stdio.h>
4*821bb9a2SXshine #include <stdlib.h>
5*821bb9a2SXshine #include <sys/socket.h>
6*821bb9a2SXshine #include <sys/stat.h>
7*821bb9a2SXshine #include <unistd.h>
8*821bb9a2SXshine 
9*821bb9a2SXshine #define PORT 12580
10*821bb9a2SXshine #define MAX_REQUEST_SIZE 1500
11*821bb9a2SXshine #define MAX_RESPONSE_SIZE 1500
12*821bb9a2SXshine #define EXIT_CODE 1
13*821bb9a2SXshine #define min(a, b) ((a) < (b) ? (a) : (b))
14*821bb9a2SXshine 
15*821bb9a2SXshine struct sockaddr_in address;
16*821bb9a2SXshine int addrlen = sizeof(address);
17*821bb9a2SXshine char buffer[MAX_REQUEST_SIZE] = {0};
18*821bb9a2SXshine int opt = 1;
19*821bb9a2SXshine 
test_tcp_bind()20*821bb9a2SXshine void test_tcp_bind()
21*821bb9a2SXshine {
22*821bb9a2SXshine     int tcp_sk_fd1, tcp_sk_fd2, tcp_sk_fd3;
23*821bb9a2SXshine 
24*821bb9a2SXshine     // create tcp sockets
25*821bb9a2SXshine     if ((tcp_sk_fd1 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
26*821bb9a2SXshine     {
27*821bb9a2SXshine         perror("tcp socket (1) failed");
28*821bb9a2SXshine         exit(EXIT_CODE);
29*821bb9a2SXshine     }
30*821bb9a2SXshine     if ((tcp_sk_fd2 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
31*821bb9a2SXshine     {
32*821bb9a2SXshine         perror("tcp socket (2) failed");
33*821bb9a2SXshine         exit(EXIT_CODE);
34*821bb9a2SXshine     }
35*821bb9a2SXshine     if ((tcp_sk_fd3 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
36*821bb9a2SXshine     {
37*821bb9a2SXshine         perror("tcp socket (3) failed");
38*821bb9a2SXshine         exit(EXIT_CODE);
39*821bb9a2SXshine     }
40*821bb9a2SXshine 
41*821bb9a2SXshine     // TEST tcp bind diff ports
42*821bb9a2SXshine     if (bind(tcp_sk_fd1, (struct sockaddr *)&address, sizeof(address)) < 0)
43*821bb9a2SXshine     {
44*821bb9a2SXshine         perror("tcp bind (1) failed");
45*821bb9a2SXshine         exit(EXIT_CODE);
46*821bb9a2SXshine     }
47*821bb9a2SXshine     address.sin_port = htons(PORT+1);
48*821bb9a2SXshine     if (bind(tcp_sk_fd2, (struct sockaddr *)&address, sizeof(address)) < 0)
49*821bb9a2SXshine     {
50*821bb9a2SXshine         perror("tcp bind (2) failed");
51*821bb9a2SXshine         exit(EXIT_CODE);
52*821bb9a2SXshine     }
53*821bb9a2SXshine     printf("===TEST 4 PASSED===\n");
54*821bb9a2SXshine 
55*821bb9a2SXshine     // TEST tcp bind same ports
56*821bb9a2SXshine     address.sin_port = htons(PORT);
57*821bb9a2SXshine     if (bind(tcp_sk_fd3, (struct sockaddr *)&address, sizeof(address)) < 0)
58*821bb9a2SXshine     {
59*821bb9a2SXshine         perror("tcp bind (3) failed");
60*821bb9a2SXshine         // exit(EXIT_CODE);
61*821bb9a2SXshine     }
62*821bb9a2SXshine     printf("===TEST 5 PASSED===\n");
63*821bb9a2SXshine 
64*821bb9a2SXshine     if (close(tcp_sk_fd1) < 0)
65*821bb9a2SXshine     {
66*821bb9a2SXshine         perror("tcp close (1) failed");
67*821bb9a2SXshine         exit(EXIT_CODE);
68*821bb9a2SXshine     }
69*821bb9a2SXshine     if (close(tcp_sk_fd2) < 0)
70*821bb9a2SXshine     {
71*821bb9a2SXshine         perror("tcp close (2) failed");
72*821bb9a2SXshine         exit(EXIT_CODE);
73*821bb9a2SXshine     }
74*821bb9a2SXshine     if (close(tcp_sk_fd3) < 0)
75*821bb9a2SXshine     {
76*821bb9a2SXshine         perror("tcp close (3) failed");
77*821bb9a2SXshine         exit(EXIT_CODE);
78*821bb9a2SXshine     }
79*821bb9a2SXshine     printf("===TEST 6 PASSED===\n");
80*821bb9a2SXshine }
81*821bb9a2SXshine 
test_udp_bind()82*821bb9a2SXshine void test_udp_bind()
83*821bb9a2SXshine {
84*821bb9a2SXshine     int udp_sk_fd1, udp_sk_fd2, udp_sk_fd3;
85*821bb9a2SXshine 
86*821bb9a2SXshine     // create tcp sockets
87*821bb9a2SXshine     if ((udp_sk_fd1 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
88*821bb9a2SXshine     {
89*821bb9a2SXshine         perror("udp socket (1) failed");
90*821bb9a2SXshine         exit(EXIT_CODE);
91*821bb9a2SXshine     }
92*821bb9a2SXshine     if ((udp_sk_fd2 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
93*821bb9a2SXshine     {
94*821bb9a2SXshine         perror("udp socket (2) failed");
95*821bb9a2SXshine         exit(EXIT_CODE);
96*821bb9a2SXshine     }
97*821bb9a2SXshine     if ((udp_sk_fd3 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
98*821bb9a2SXshine     {
99*821bb9a2SXshine         perror("udp socket (3) failed");
100*821bb9a2SXshine         exit(EXIT_CODE);
101*821bb9a2SXshine     }
102*821bb9a2SXshine 
103*821bb9a2SXshine     // TEST udp bind diff ports
104*821bb9a2SXshine     if (bind(udp_sk_fd1, (struct sockaddr *)&address, sizeof(address)) < 0)
105*821bb9a2SXshine     {
106*821bb9a2SXshine         perror("udp bind (1) failed");
107*821bb9a2SXshine         exit(EXIT_CODE);
108*821bb9a2SXshine     }
109*821bb9a2SXshine     address.sin_port = htons(PORT+1);
110*821bb9a2SXshine     if (bind(udp_sk_fd2, (struct sockaddr *)&address, sizeof(address)) < 0)
111*821bb9a2SXshine     {
112*821bb9a2SXshine         perror("udp bind (2) failed");
113*821bb9a2SXshine         exit(EXIT_CODE);
114*821bb9a2SXshine     }
115*821bb9a2SXshine     printf("===TEST 7 PASSED===\n");
116*821bb9a2SXshine 
117*821bb9a2SXshine     // TEST udp bind same ports
118*821bb9a2SXshine     address.sin_port = htons(PORT);
119*821bb9a2SXshine     if (bind(udp_sk_fd3, (struct sockaddr *)&address, sizeof(address)) < 0)
120*821bb9a2SXshine     {
121*821bb9a2SXshine         perror("udp bind (3) failed");
122*821bb9a2SXshine         // exit(EXIT_CODE);
123*821bb9a2SXshine     }
124*821bb9a2SXshine     printf("===TEST 8 PASSED===\n");
125*821bb9a2SXshine 
126*821bb9a2SXshine     if (close(udp_sk_fd1) < 0)
127*821bb9a2SXshine     {
128*821bb9a2SXshine         perror("udp close (1) failed");
129*821bb9a2SXshine         exit(EXIT_CODE);
130*821bb9a2SXshine     }
131*821bb9a2SXshine     if (close(udp_sk_fd2) < 0)
132*821bb9a2SXshine     {
133*821bb9a2SXshine         perror("udp close (2) failed");
134*821bb9a2SXshine         exit(EXIT_CODE);
135*821bb9a2SXshine     }
136*821bb9a2SXshine     if (close(udp_sk_fd3) < 0)
137*821bb9a2SXshine     {
138*821bb9a2SXshine         perror("udp close (3) failed");
139*821bb9a2SXshine         exit(EXIT_CODE);
140*821bb9a2SXshine     }
141*821bb9a2SXshine     printf("===TEST 9 PASSED===\n");
142*821bb9a2SXshine }
143*821bb9a2SXshine 
test_all_ports()144*821bb9a2SXshine void test_all_ports() {
145*821bb9a2SXshine     int count = 0;
146*821bb9a2SXshine 
147*821bb9a2SXshine     while (1) {
148*821bb9a2SXshine     	int tcp_fd;
149*821bb9a2SXshine     	if ((tcp_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
150*821bb9a2SXshine     	{
151*821bb9a2SXshine             perror("socket failed");
152*821bb9a2SXshine             exit(EXIT_CODE);
153*821bb9a2SXshine     	}
154*821bb9a2SXshine 
155*821bb9a2SXshine     	address.sin_port = htons(0);
156*821bb9a2SXshine     	if (bind(tcp_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
157*821bb9a2SXshine     	{
158*821bb9a2SXshine             perror("bind failed");
159*821bb9a2SXshine             // exit(EXIT_CODE);
160*821bb9a2SXshine             break;
161*821bb9a2SXshine     	}
162*821bb9a2SXshine 
163*821bb9a2SXshine     	count++;
164*821bb9a2SXshine     }
165*821bb9a2SXshine     printf("===TEST 10===\n");
166*821bb9a2SXshine     printf("count: %d\n", count);
167*821bb9a2SXshine }
168*821bb9a2SXshine 
main(int argc,char const * argv[])169*821bb9a2SXshine int main(int argc, char const *argv[])
170*821bb9a2SXshine {
171*821bb9a2SXshine     int server_fd;
172*821bb9a2SXshine     int udp_sk_fd;
173*821bb9a2SXshine 
174*821bb9a2SXshine     // 创建socket
175*821bb9a2SXshine     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
176*821bb9a2SXshine     {
177*821bb9a2SXshine         perror("tcp socket failed");
178*821bb9a2SXshine         exit(EXIT_CODE);
179*821bb9a2SXshine     }
180*821bb9a2SXshine 
181*821bb9a2SXshine     if ((udp_sk_fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
182*821bb9a2SXshine     {
183*821bb9a2SXshine         perror("udp socket failed");
184*821bb9a2SXshine         exit(EXIT_CODE);
185*821bb9a2SXshine     }
186*821bb9a2SXshine 
187*821bb9a2SXshine     // 设置地址和端口
188*821bb9a2SXshine     address.sin_family = AF_INET;
189*821bb9a2SXshine     address.sin_addr.s_addr = INADDR_ANY;
190*821bb9a2SXshine     address.sin_port = htons(PORT);
191*821bb9a2SXshine 
192*821bb9a2SXshine     // TEST socket's bind
193*821bb9a2SXshine     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
194*821bb9a2SXshine     {
195*821bb9a2SXshine         perror("tcp bind failed");
196*821bb9a2SXshine         exit(EXIT_CODE);
197*821bb9a2SXshine     }
198*821bb9a2SXshine     address.sin_port = htons(PORT);
199*821bb9a2SXshine     if (bind(udp_sk_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
200*821bb9a2SXshine     {
201*821bb9a2SXshine         perror("udp bind failed");
202*821bb9a2SXshine         exit(EXIT_CODE);
203*821bb9a2SXshine     }
204*821bb9a2SXshine     printf("===TEST 1 PASSED===\n");
205*821bb9a2SXshine 
206*821bb9a2SXshine     // TEST socket's listen
207*821bb9a2SXshine     if (listen(server_fd, 3) < 0)
208*821bb9a2SXshine     {
209*821bb9a2SXshine         perror("listen failed");
210*821bb9a2SXshine         exit(EXIT_CODE);
211*821bb9a2SXshine     }
212*821bb9a2SXshine     printf("===TEST 2 PASSED===\n");
213*821bb9a2SXshine 
214*821bb9a2SXshine     // TEST socket's close
215*821bb9a2SXshine     if (close(server_fd) < 0)
216*821bb9a2SXshine     {
217*821bb9a2SXshine         perror("tcp close failed");
218*821bb9a2SXshine         exit(EXIT_CODE);
219*821bb9a2SXshine     }
220*821bb9a2SXshine     if (close(udp_sk_fd) < 0)
221*821bb9a2SXshine     {
222*821bb9a2SXshine         perror("udp close failed");
223*821bb9a2SXshine         exit(EXIT_CODE);
224*821bb9a2SXshine     }
225*821bb9a2SXshine     printf("===TEST 3 PASSED===\n");
226*821bb9a2SXshine 
227*821bb9a2SXshine 
228*821bb9a2SXshine     test_tcp_bind();
229*821bb9a2SXshine     test_udp_bind();
230*821bb9a2SXshine     test_all_ports();
231*821bb9a2SXshine 
232*821bb9a2SXshine     return 0;
233*821bb9a2SXshine }
234