2 Copyright 2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
23 #include <arpa/inet.h>
25 #include <netinet/in.h>
26 #define SOCKET_ERROR 0
31 #include <sys/timeb.h>
34 #include <sys/types.h>
45 static const char* TAG = "UDP6";
49 if (initted) shutdown();
52 int UDP6::init(u2 tPort)
54 if (initted) return 0;
56 addrlen = sizeof(struct sockaddr_in6);
58 if ((socketnum = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
59 { perror("socket"); return 0; }
61 memset(&myAddr, 0, sizeof(myAddr));
62 myAddr.sin6_family = AF_INET6; // host byte order
63 myAddr.sin6_port = htons(myPort); // short, network byte order
64 myAddr.sin6_addr = in6addr_any;
67 * FIXME This _might_ need porting to Windows
68 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
69 inet_pton(AF_INET6, "", &myAddr.sin6_addr);
72 if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
73 { perror("bind6"); return 0; }
76 FD_SET(socketnum, &readfds);
88 CLOSESOCKET(socketnum);
93 unsigned char UDP6::waitforMessage(unsigned char how, SOCKET quitPipe)
95 unsigned char UDP6::waitforMessage(unsigned char how, int quitPipe)
98 if (!initted) return 0;
101 how = 1 - start new wait
102 how = 2 - continue wait
103 how = 3 - block, return on byte from quitPipe
107 FD_SET(socketnum, &readfds);
109 struct timeval* passToSelect = NULL;
111 int sockMaxP1 = socketnum + 1;
121 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
130 FD_SET(quitPipe, &readfds);
131 if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
135 if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
138 if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
140 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
141 reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
142 { perror("recvfrom"); return 0; }
145 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
146 inet_ntop(AF_INET6, &theirAddr.sin6_addr, fromIPA, 40);
147 fromPort = ntohs(theirAddr.sin6_port);
148 fromIPisLL = IN6_IS_ADDR_LINKLOCAL(&theirAddr.sin6_addr);
153 Return 1, nothing happened, timer expired
154 Return 2, packet arrived (timer not expired)
158 u4 UDP6::getDataLength(void) const
160 return static_cast<u4>(mlength);
163 const void* UDP6::getData() const { return buf; }
164 const char* UDP6::getFromIPA() const { return fromIPA; }
165 short UDP6::getFromPort() const { return fromPort; }
166 bool UDP6::getFromIPisLL() const { return fromIPisLL; }
168 bool UDP6::send(const char *ipa, u2 port, char *message, int length, bool mcast)
170 if (!initted) return false;
174 struct sockaddr_in6 sendAddr;
175 memset(&sendAddr, 0, sizeof(struct sockaddr_in6));
176 sendAddr.sin6_family = AF_INET6;
177 inet_pton(AF_INET6, ipa, &sendAddr.sin6_addr);
178 sendAddr.sin6_port = htons(port);
182 struct if_nameindex* ifs = if_nameindex();
185 for(int i = 0; ifs[i].if_index > 0; i++)
187 ifIndex = ifs[i].if_index;
188 int res = setsockopt(socketnum, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, sizeof(ifIndex));
189 LogNT::getInstance()->debug(TAG, "Transmitting IPv6 MC UDP on {}", ifs[i].if_name);
190 res = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
191 LogNT::getInstance()->debug(TAG, "Result: {}. Errno: {}", res, errno);
195 if_freenameindex(ifs);
199 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
200 if (sentLength == length) return true;
201 LogNT::getInstance()->error(TAG, "sendto failed, errno = {}", errno);
208 u4 UDP6::getIPNumber(u4)
213 u4 UDP6::getIPNumber(u4 num)
218 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
220 return INADDR_ANY; //well take any address, if we fail
223 struct hostent *hosts=gethostbyname(buffer);
226 return INADDR_ANY; //well take any address, if we fail
230 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
232 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
233 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(u4));
234 return returnaddress;