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