]> git.vomp.tv Git - vompclient.git/blob - udp6.cc
Rewrite VConnect server selection logic. More things to config
[vompclient.git] / udp6.cc
1 /*
2     Copyright 2020 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef WIN32
21 #include <netdb.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #define SOCKET_ERROR 0
27 #include <net/if.h>
28 #else
29 #include <winsock2.h>
30 #include <Ws2tcpip.h>
31 #include <sys/timeb.h>
32 #endif
33
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <fcntl.h>
40
41 #include "log.h"
42
43 #include "udp6.h"
44
45 static const char* TAG = "UDP6";
46
47 UDP6::~UDP6()
48 {
49   if (initted) shutdown();
50 }
51
52 int UDP6::init(USHORT tPort)
53 {
54   if (initted) return 0;
55   myPort = tPort;
56   addrlen = sizeof(struct sockaddr_in6);
57
58   if ((socketnum = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
59   { perror("socket"); return 0; }
60
61   // FIXME - implement server side
62
63   /*
64
65   memset(&myAddr, 0, sizeof(myAddr));
66   myAddr.sin6_family = AF_INET6;        // host byte order
67   myAddr.sin6_port = htons(myPort);     // short, network byte order
68
69 //  myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
70
71   inet_pton(AF_INET6, "", &myAddr.sin6_addr);
72
73 / *
74   if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
75   { perror("bind"); return 0; }
76 * /
77
78 */
79
80   FD_ZERO(&readfds);
81   FD_SET(socketnum, &readfds);
82   tv.tv_sec = 0;
83   tv.tv_usec = 0;
84
85   initted = true;
86
87   return 1;
88 }
89
90 void UDP6::shutdown()
91 {
92   if (!initted) return;
93   CLOSESOCKET(socketnum);
94   initted = false;
95 }
96
97 #ifdef WIN32
98 unsigned char UDP6::waitforMessage(unsigned char how, SOCKET quitPipe)
99 #else
100 unsigned char UDP6::waitforMessage(unsigned char how, int quitPipe)
101 #endif
102 {
103   if (!initted) return 0;
104
105   /* how = 0 - block
106      how = 1 - start new wait
107      how = 2 - continue wait
108      how = 3 - block, return on byte from quitPipe
109   */
110
111   FD_ZERO(&readfds);
112   FD_SET(socketnum, &readfds);
113
114   struct timeval* passToSelect = NULL;
115
116   int sockMaxP1 = socketnum + 1;
117
118   if (how == 1)
119   {
120     tv.tv_sec = 1;
121     tv.tv_usec = 500000;
122     passToSelect = &tv;
123   }
124   else if (how == 2)
125   {
126     if ((tv.tv_sec == 0) && (tv.tv_usec == 0))  // protection in case timer = 0
127     {
128       tv.tv_sec = 1;
129       tv.tv_usec = 500000;
130     }
131     passToSelect = &tv;
132   }
133   else if (how == 3)
134   {
135     FD_SET(quitPipe, &readfds);
136     if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
137   }
138
139
140   if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
141   {  return 1;  }
142
143   if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
144
145   if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
146       reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
147   { perror("recvfrom"); return 0; }
148   else
149   {
150     memset(&buf[mlength], 0, MAXBUFLEN - mlength);
151     inet_ntop(AF_INET6, &theirAddr.sin6_addr, fromIPA, 40);
152     fromPort = ntohs(theirAddr.sin6_port);
153     fromIPisLL = IN6_IS_ADDR_LINKLOCAL(&theirAddr.sin6_addr);
154     return 2;
155   }
156
157   /* Return 0, failure
158      Return 1, nothing happened, timer expired
159      Return 2, packet arrived (timer not expired)
160   */
161 }
162
163 UINT UDP6::getDataLength(void) const
164 {
165   return static_cast<UINT>(mlength);
166 }
167
168 const void* UDP6::getData() const     {  return buf;  }
169 const char* UDP6::getFromIPA() const  {  return fromIPA;  }
170 short UDP6::getFromPort() const       {  return fromPort; }
171 bool UDP6::getFromIPisLL() const      {  return fromIPisLL; }
172
173 bool UDP6::send(const char *ipa, USHORT port, char *message, int length, bool mcast)
174 {
175   if (!initted) return false;
176   int sentLength = 0;
177   errno = 0;
178
179   struct sockaddr_in6 sendAddr;
180   memset(&sendAddr, 0, sizeof(struct sockaddr_in6));
181   sendAddr.sin6_family = AF_INET6;
182   inet_pton(AF_INET6, ipa, &sendAddr.sin6_addr);
183   sendAddr.sin6_port = htons(port);
184
185   if (mcast)
186   {
187     struct if_nameindex* ifs = if_nameindex();
188     UINT ifIndex;
189
190     for(int i = 0; ifs[i].if_index > 0; i++)
191     {
192       ifIndex = ifs[i].if_index;
193       int res = setsockopt(socketnum, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, sizeof(ifIndex));
194       LogNT::getInstance()->debug(TAG, "Transmitting IPv6 MC UDP on {}", ifs[i].if_name);
195       res = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
196       LogNT::getInstance()->debug(TAG, "Result: {}. Errno: {}", res, errno);
197       errno = 0;
198     }
199
200     if_freenameindex(ifs);
201   }
202   else
203   {
204     sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
205     if (sentLength == length) return true;
206     LogNT::getInstance()->error(TAG, "sendto failed, errno = {}", errno);
207   }
208
209   return false;
210 }
211
212 #ifndef WIN32
213 ULONG UDP6::getIPNumber(ULONG)
214 {
215   return INADDR_ANY;
216 }
217 #else
218 ULONG UDP6::getIPNumber(ULONG num)
219 {
220   char buffer[100];
221   ULONG returnaddress;
222
223   if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
224   {
225     return INADDR_ANY; //well take any address, if we fail
226   }
227
228   struct hostent *hosts=gethostbyname(buffer);
229   if (hosts==NULL)
230   {
231     return INADDR_ANY; //well take any address, if we fail
232   }
233
234   int num_ip=0;
235   for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
236
237   int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
238   memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
239   return returnaddress;
240 }
241 #endif