-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
197 lines (167 loc) · 5.39 KB
/
client.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Created by Marian Babik on 11/18/20.
//
#include <linux/in6.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
int socket_reuse(int sock) {
int reuse = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(const char*)&reuse, sizeof(reuse)) < 0)
{
perror("SO_REUSEADDR failed");
return -1;
}
if(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
(const char*)&reuse, sizeof(reuse)) < 0)
{
perror("SO_REUSEPORT failed");
return -1;
}
}
int enable_flow_label(int sock) {
int on = 1;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_FLOWINFO_SEND, &on, sizeof(on)) == -1) {
printf("setsockopt(IPV6_FLOWINFO_SEND): %s\n", strerror(errno));
return 1;
}
if (setsockopt(sock, IPPROTO_IPV6, IPV6_FLOWINFO, &on, sizeof(on)) == -1) {
printf("setsockopt(IPV6_FLOWINFO): %s\n", strerror(errno));
return 1;
}
return 0;
}
int set_flow_label(int sock, struct sockaddr_in6 *sa6P, int flowlabel) {
char freq_buf[sizeof(struct in6_flowlabel_req)];
struct in6_flowlabel_req *freq = (struct in6_flowlabel_req *) freq_buf;
int freq_len = sizeof(*freq);
memset(freq, 0, sizeof(*freq));
freq->flr_label = htonl(flowlabel & IPV6_FLOWINFO_FLOWLABEL);
printf("flow label - 0x%x\n", (flowlabel & IPV6_FLOWINFO_FLOWLABEL));
freq->flr_action = IPV6_FL_A_GET;
freq->flr_flags = IPV6_FL_F_CREATE;
freq->flr_share = IPV6_FL_S_ANY;
memcpy(&freq->flr_dst, &sa6P->sin6_addr, 16);
if (setsockopt(sock, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, freq, freq_len) < 0) {
printf("setsockopt: %s\n", strerror(errno));
return 1;
}
// sa6P->sin6_flowinfo = freq->flr_label;
return 0;
}
int get_flow_labels(int sockfd)
{
int s;
struct in6_flowlabel_req freq;
int size = sizeof(freq);
freq.flr_action = IPV6_FL_A_GET;
getsockopt(sockfd, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, &freq, &size);
printf("Local Label %05X share %d expires %d linger %d\n", ntohl(freq.flr_label), freq.flr_share,
freq.flr_linger, freq.flr_expires);
return 0;
}
unsigned int get_remote_flow_label(int sockfd)
{
int s;
struct in6_flowlabel_req freq;
int size = sizeof(freq);
freq.flr_action = IPV6_FL_F_REMOTE;
getsockopt(sockfd, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, &freq, &size);
printf("Remote Label %05X\n", ntohl(freq.flr_label));
return freq.flr_label;
}
void enable_tclass(int sockfd)
{
int on = 1;
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVTCLASS, &on, sizeof(on)) < 0 ){
printf("setsockopt tclass enable: %s\n", strerror(errno));
}
}
int set_tclass(int sockfd, int tclass)
{
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass)) < 0) {
printf("setsockopt tclass: %s\n", strerror(errno));
return 1;
}
}
int main(int argc, char *argv[]) {
int sock, i;
struct sockaddr_in6 server_addr;
char message[1000], server_reply[2000];
if (argc != 2) {
printf("Usage: %s ipv6_addr\n", argv[0]);
return -1;
}
//Create socket
sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
printf("Could not create socket\n");
return -1;
}
printf("socket created\n");
server_addr.sin6_family = AF_INET6;
inet_pton(AF_INET6, argv[1], &server_addr.sin6_addr);
server_addr.sin6_port = htons(24999);
printf("socket reuse enabled\n");
socket_reuse(sock);
printf("flow label enabled\n");
enable_flow_label(sock);
set_flow_label(sock, &server_addr, 255);
server_addr.sin6_flowinfo = htonl(255 & IPV6_FLOWINFO_FLOWLABEL);
enable_tclass(sock);
printf("tclass: 0x%x\n", 252);
enable_tclass(sock);
set_tclass(sock, 252);
//Connect to remote server
if (connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("connect failed. Error");
return 1;
}
printf("connected\n");
//keep communicating with server
for (i = 2; i < 6; ++i) {
sprintf(message, "test message # %d\n", i);
//Send some data
if (send(sock, message, strlen(message), 0) < 0) {
printf("send failed");
return 1;
}
//Receive a reply from the server
if (recv(sock, server_reply, 2000, 0) < 0) {
printf("recv failed");
break;
}
get_flow_labels(sock);
get_remote_flow_label(sock);
printf("server replied:\n");
printf(server_reply);
sleep(2);
}
//try to change options while communicating
for (i = 2; i < 6; ++i) {
set_flow_label(sock, &server_addr, 254);
server_addr.sin6_flowinfo = htonl(254 & IPV6_FLOWINFO_FLOWLABEL);
printf("tclass 0x%x\n", 140);
set_tclass(sock, 140);
sprintf(message, "test message # %d\n", i);
//Send some data
if (send(sock, message, strlen(message), 0) < 0) {
printf("send failed\n");
return 1;
}
//Receive a reply from the server
if (recv(sock, server_reply, 2000, 0) < 0) {
printf("recv failed\n");
break;
}
printf("server replied:\n");
printf(server_reply);
sleep(2);
}
close(sock);
return 0;
}