2 Copyright 2004-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 #define SOCKET_ERROR 0
29 #include <sys/timeb.h>
32 #include <sys/types.h>
45 if (initted) shutdown();
48 int UDP4::init(USHORT tport)
50 if (initted) return 0;
53 addrlen = sizeof(struct sockaddr);
55 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
56 { perror("socket"); return 0; }
58 myAddr.sin_family = AF_INET; // host byte order
59 myAddr.sin_port = htons(myPort); // short, network byte order
60 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
61 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
63 if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
64 { perror("bind"); return 0; }
67 FD_SET(socketnum, &readfds);
74 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&allowed), sizeof(allowed));
76 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&allowed), sizeof(allowed));
87 CLOSESOCKET(socketnum);
92 unsigned char UDP4::waitforMessage(unsigned char how, SOCKET quitPipe)
94 unsigned char UDP4::waitforMessage(unsigned char how, int quitPipe)
97 if (!initted) return 0;
100 how = 1 - start new wait
101 how = 2 - continue wait
102 how = 3 - block, return on byte from quitPipe
106 FD_SET(socketnum, &readfds);
108 struct timeval* passToSelect = NULL;
110 int sockMaxP1 = socketnum + 1;
120 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
129 FD_SET(quitPipe, &readfds);
130 if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
134 if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
137 if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
139 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
140 reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
141 { perror("recvfrom"); return 0; }
144 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
145 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
146 fromPort = ntohs(theirAddr.sin_port);
152 Return 1, nothing happened, timer expired
153 Return 2, packet arrived (timer not expired)
157 UINT UDP4::getDataLength(void) const
159 return static_cast<UINT>(mlength);
162 const void* UDP4::getData() const { return buf; }
163 const char* UDP4::getFromIPA() const { return fromIPA; }
164 short UDP4::getFromPort() const { return fromPort; }
166 bool UDP4::send(const char *ipa, USHORT port, char *message, int length)
168 if (!initted) return false;
172 struct sockaddr_in sendAddr;
173 sendAddr.sin_family = AF_INET; // host byte order
174 sendAddr.sin_port = htons(port); // short, network byte order
175 struct in_addr tad; // temp struct tad needed to pass to sendAddr.sin_addr
176 tad.s_addr = inet_addr(ipa);
177 sendAddr.sin_addr = tad; // address
178 memset(&(sendAddr.sin_zero), 0, 8); // zero the rest of the struct
182 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
183 if (sentLength == length) return true;
184 Log::getInstance()->log("UDP6", Log::ERR, "sendto failed, errno = %i", errno);
189 ULONG UDP4::getIPNumber(ULONG)
194 ULONG UDP4::getIPNumber(ULONG num)
199 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
201 return INADDR_ANY; //well take any address, if we fail
204 struct hostent *hosts=gethostbyname(buffer);
207 return INADDR_ANY; //well take any address, if we fail
211 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
213 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
214 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
215 return returnaddress;