-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_qos_helper.h
115 lines (98 loc) · 3.28 KB
/
ip_qos_helper.h
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
#ifndef __RCL_IP_QOS_HELPER_H__
#define __RCL_IP_QOS_HELPER_H__
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
// sudo tc -s qdisc ls dev eth0
// sudo tcpdump -i lo tcp port 1234 -nnvv
#ifdef __cplusplus
namespace newplan_toolkit
{
class IPQoSHelper
{
public:
#endif
static int set_and_check_ip_tos(int sock, int tos)
{
int old_tos = 0, new_tos = 0;
int tos_length = sizeof(old_tos);
if (getsockopt(sock, IPPROTO_IP, IP_TOS, &old_tos, (socklen_t *)&tos_length) < 0)
{
printf("[Warning] Get IP tos error\n");
return -1;
}
if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
{
printf("[Warning] set IP tos error\n");
return -1;
}
if (getsockopt(sock, IPPROTO_IP, IP_TOS, &new_tos, (socklen_t *)&tos_length) < 0)
{
printf("[Warning] Get IP tos error\n");
return -1;
}
if (new_tos != tos)
{
printf("[Warning] set IP ToS error\n");
return -1;
}
printf("[INFO] Having modified IP ToS for socket: %d from 0x%x(old) to 0x%x(new)\n",
sock, old_tos, new_tos);
return 0;
}
static int set_and_check_ip_priority(int sock, int prio)
{
int old_prio = 0, new_prio = 0;
int prio_length = sizeof(old_prio);
if (prio > 6 || prio < 0)
{
printf("[Warning] IP priority: %d should be in [0,6]\n", prio);
return -1;
}
if (getsockopt(sock, SOL_SOCKET, SO_PRIORITY, &old_prio, (socklen_t *)&prio_length) < 0)
{
printf("[Warning] Get IP priority error\n");
return -1;
}
if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
{
printf("[Warning] set IP priority error\n");
return -1;
}
if (getsockopt(sock, SOL_SOCKET, SO_PRIORITY, &new_prio, (socklen_t *)&prio_length) < 0)
{
printf("[Warning] Get IP priority error\n");
return -1;
}
if (new_prio != prio)
{
printf("[Warning] set IP priority error\n");
return -1;
}
printf("[INFO] Having modified IP priority for socket: %d from %d(old) to %d(new)\n",
sock, old_prio, new_prio);
return 0;
}
static int set_socket_reuse(int sock)
{
int opt = 1;
// sockfd为需要端口复用的套接字
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(const void *)&opt, sizeof(opt))
< 0)
{
printf("[Warning] failed to set IP port resue\n");
return -1;
}
printf("[INFO] Having set IP port resue\n");
return 0;
}
#ifdef __cplusplus
};
}; // namespace newplan
#endif //cplusplus
#endif /* __NEWPLAN_IP_QOS_H*/