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