-
Notifications
You must be signed in to change notification settings - Fork 615
/
Copy pathsocket_udplite.c
182 lines (148 loc) · 3.75 KB
/
socket_udplite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "zdtmtst.h"
const char *test_doc = "static test for UDP socket\n";
const char *test_author = "Pavel Emelyanov <xemul@parallels.com<>\n";
/* Description:
* Create two tcp socket, server send asynchronous request on
* read data and client write data after migration
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <sys/wait.h>
static int port = 8890;
static char buf[8];
#define MSG1 "msg1"
#define MSG2 "msg_2"
int main(int argc, char **argv)
{
int ret, sk1, sk2, sk3, sk4;
socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in addr1, addr2, addr3, addr4, addr;
test_init(argc, argv);
sk1 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE);
if (sk1 < 0) {
pr_perror("Can't create socket");
return 1;
}
memset(&addr1, 0, sizeof(addr1));
addr1.sin_family = AF_INET;
addr1.sin_addr.s_addr = inet_addr("127.0.0.1");
addr1.sin_port = htons(port);
ret = bind(sk1, (struct sockaddr *)&addr1, len);
if (ret < 0) {
pr_perror("Can't bind socket");
return 1;
}
sk2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE);
if (sk2 < 0) {
pr_perror("Can't create socket");
return 1;
}
memset(&addr2, 0, sizeof(addr1));
addr2.sin_family = AF_INET;
addr2.sin_addr.s_addr = inet_addr("127.0.0.1");
addr2.sin_port = htons(port + 1);
ret = bind(sk2, (struct sockaddr *)&addr2, len);
if (ret < 0) {
pr_perror("Can't bind socket");
return 1;
}
ret = connect(sk2, (struct sockaddr *)&addr1, len);
if (ret < 0) {
pr_perror("Can't connect");
return 1;
}
sk3 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE);
if (sk3 < 0) {
pr_perror("Can't create socket");
return 1;
}
memset(&addr3, 0, sizeof(addr3));
addr3.sin_family = AF_INET;
addr3.sin_addr.s_addr = inet_addr("127.0.0.1");
addr3.sin_port = htons(port + 2);
ret = bind(sk3, (struct sockaddr *)&addr3, len);
if (ret < 0) {
pr_perror("Can't bind socket");
return 1;
}
sk4 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE);
if (sk4 < 0) {
pr_perror("Can't create socket");
return 1;
}
memset(&addr4, 0, sizeof(addr4));
addr4.sin_family = AF_INET;
addr4.sin_addr.s_addr = inet_addr("0.0.0.0");
addr4.sin_port = htons(0);
ret = bind(sk4, (struct sockaddr *)&addr4, len);
if (ret < 0) {
pr_perror("Can't bind socket");
return 1;
}
ret = connect(sk4, (struct sockaddr *)&addr3, len);
if (ret < 0) {
pr_perror("Can't connect");
return 1;
}
ret = connect(sk3, (struct sockaddr *)&addr4, len);
if (ret < 0) {
pr_perror("Can't connect");
return 1;
}
if (shutdown(sk4, SHUT_RDWR)) {
pr_perror("Can't shutdown socket");
return 1;
}
if (shutdown(sk3, SHUT_RDWR)) {
pr_perror("Can't shutdown socket");
return 1;
}
test_daemon();
test_waitsig();
ret = sendto(sk1, MSG1, sizeof(MSG1), 0, (struct sockaddr *)&addr2, len);
if (ret < 0) {
fail("Can't send");
return 1;
}
ret = send(sk2, MSG2, sizeof(MSG2), 0);
if (ret < 0) {
fail("Can't send C");
return 1;
}
ret = recvfrom(sk1, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &len);
if (ret <= 0) {
fail("Can't recv C");
return 1;
}
if (len != sizeof(struct sockaddr_in) || memcmp(&addr2, &addr, len)) {
fail("Wrong peer C");
return 1;
}
if (ret != sizeof(MSG2) || memcmp(buf, MSG2, ret)) {
fail("Wrong message C");
return 1;
}
ret = recvfrom(sk2, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &len);
if (ret <= 0) {
fail("Can't recv");
return 1;
}
if (len != sizeof(struct sockaddr_in) || memcmp(&addr1, &addr, len)) {
fail("Wrong peer");
return 1;
}
if (ret != sizeof(MSG1) || memcmp(buf, MSG1, ret)) {
fail("Wrong message");
return 1;
}
pass();
return 0;
}