This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsock6.c
229 lines (188 loc) · 5.57 KB
/
dsock6.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright 2019 Chris Tallon
This file is part of VOMP.
VOMP is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
VOMP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VOMP. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <sys/ioctl.h>
#include <vector>
#include <algorithm>
#include "dsock6.h"
DatagramSocket6::DatagramSocket6()
{
addrlen = sizeof(struct sockaddr_in6);
log = Log::getInstance();
initted = 0;
}
DatagramSocket6::~DatagramSocket6()
{
shutdown();
}
bool DatagramSocket6::init(USHORT port)
{
if ((socketnum = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
log->log("UDP6", Log::CRIT, "Socket error");
return false;
}
char* one = (char*)1;
if (setsockopt(socketnum, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
{
log->log("UDP6", Log::CRIT, "Reuse addr fail");
return false;
}
struct sockaddr_in6 saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin6_family = AF_INET6;
saddr.sin6_port = htons(port);
saddr.sin6_addr = in6addr_any;
if (bind(socketnum, (struct sockaddr *)&saddr, addrlen) == -1)
{
log->log("UDP6", Log::CRIT, "Bind error %u", port);
close(socketnum);
return false;
}
struct ifaddrs* ifas;
if(getifaddrs(&ifas))
{
log->log("UDP6", Log::CRIT, "getifaddrs error");
close(socketnum);
return false;
}
struct if_nameindex* ifs = if_nameindex();
std::vector<unsigned int> mcastIndexes;
struct ifaddrs* ifaNext = ifas;
for(int i = 0; ifaNext; i++)
{
if ((ifaNext->ifa_flags & IFF_MULTICAST) && (ifaNext->ifa_addr->sa_family == AF_INET6))
{
for(int i = 0; ifs[i].if_index > 0; i++)
{
if (!strcmp(ifaNext->ifa_name, ifs[i].if_name))
{
mcastIndexes.push_back(ifs[i].if_index);
break;
}
}
}
ifaNext = ifaNext->ifa_next;
}
freeifaddrs(ifas);
if_freenameindex(ifs);
std::sort(mcastIndexes.begin(), mcastIndexes.end());
auto last = std::unique(mcastIndexes.begin(), mcastIndexes.end());
mcastIndexes.erase(last, mcastIndexes.end());
for(auto mif : mcastIndexes)
{
//log->log("UDP6", Log::DEBUG, "To listen on index %u", mif);
struct ipv6_mreq mGroup;
inet_pton(AF_INET6, "ff15:766f:6d70:2064:6973:636f:7665:7279", &mGroup.ipv6mr_multiaddr);
mGroup.ipv6mr_interface = mif;
if (!setsockopt(socketnum, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mGroup, sizeof(mGroup)))
log->log("UDP6", Log::DEBUG, "Listening on IF %u", mif);
else
log->log("UDP6", Log::ERR, "Cannot listen on IF %u", mif);
}
FD_ZERO(&readfds);
FD_SET(socketnum, &readfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
initted = 1;
return true;
}
void DatagramSocket6::shutdown()
{
if (initted) close(socketnum);
initted = 0;
}
unsigned char DatagramSocket6::waitforMessage(unsigned char how)
{
if (!initted) return 0;
/* how = 0 - block
how = 1 - start new wait
how = 2 - continue wait
*/
struct timeval* passToSelect = NULL;
if (how == 0)
{
passToSelect = NULL;
}
else if (how == 1)
{
tv.tv_sec = 1;
tv.tv_usec = 100000;
passToSelect = &tv;
}
else if (how == 2)
{
if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
{
tv.tv_sec = 1;
tv.tv_usec = 100000;
}
passToSelect = &tv;
}
FD_ZERO(&readfds);
FD_SET(socketnum, &readfds);
if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0) return 1;
struct sockaddr_in6 theirAddr;
if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0, (struct sockaddr *)&theirAddr, &addrlen)) == -1)
{
log->log("UDP6", Log::DEBUG, "recvfrom error");
return 0;
}
else
{
memset(&buf[mlength], 0, MAXBUFLEN - mlength);
inet_ntop(AF_INET6, &theirAddr.sin6_addr, fromIPA, 40);
fromPort = ntohs(theirAddr.sin6_port);
log->log("UDP", Log::DEBUG, "%s:%i received length %i", fromIPA, fromPort, mlength);
return 2;
}
/* Return 0, failure
Return 1, nothing happened, timer expired
Return 2, packet arrived (timer not expired)
*/
}
void DatagramSocket6::send(const char *ipa, USHORT port, char *message, int length)
{
int sentLength = 0;
struct sockaddr_in6 theirAddr;
memset(&theirAddr, 0, sizeof(struct sockaddr_in6));
theirAddr.sin6_family = AF_INET6;
theirAddr.sin6_port = htons(port);
inet_pton(AF_INET6, ipa, &theirAddr.sin6_addr);
sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
if (sentLength == length)
{
log->log("UDP", Log::DEBUG, "%s:%u sent length %i", ipa, port, length);
return;
}
log->log("UDP", Log::DEBUG, "%s:%u send failed %i", ipa, port, length);
sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
if (sentLength == length)
{
log->log("UDP", Log::DEBUG, "%s:%u sent length %i 2nd try", ipa, port, length);
return;
}
log->log("UDP", Log::DEBUG, "%s:%u send failed %i 2nd try", ipa, port, length);
}