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