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>
43 static const char* TAG = "UDP4";
47 if (initted) shutdown();
50 int UDP4::init(USHORT tport)
52 if (initted) return 0;
55 addrlen = sizeof(struct sockaddr);
57 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
58 { perror("socket"); return 0; }
60 myAddr.sin_family = AF_INET; // host byte order
61 myAddr.sin_port = htons(myPort); // short, network byte order
62 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
63 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
65 if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
66 { perror("bind"); return 0; }
69 FD_SET(socketnum, &readfds);
76 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&allowed), sizeof(allowed));
78 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&allowed), sizeof(allowed));
89 CLOSESOCKET(socketnum);
94 unsigned char UDP4::waitforMessage(unsigned char how, SOCKET quitPipe)
96 unsigned char UDP4::waitforMessage(unsigned char how, int quitPipe)
99 if (!initted) return 0;
102 how = 1 - start new wait
103 how = 2 - continue wait
104 how = 3 - block, return on byte from quitPipe
108 FD_SET(socketnum, &readfds);
110 struct timeval* passToSelect = NULL;
112 int sockMaxP1 = socketnum + 1;
122 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
131 FD_SET(quitPipe, &readfds);
132 if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
136 if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
139 if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
141 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
142 reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
143 { perror("recvfrom"); return 0; }
146 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
147 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
148 fromPort = ntohs(theirAddr.sin_port);
154 Return 1, nothing happened, timer expired
155 Return 2, packet arrived (timer not expired)
159 UINT UDP4::getDataLength(void) const
161 return static_cast<UINT>(mlength);
164 const void* UDP4::getData() const { return buf; }
165 const char* UDP4::getFromIPA() const { return fromIPA; }
166 short UDP4::getFromPort() const { return fromPort; }
168 bool UDP4::send(const char *ipa, USHORT port, char *message, int length)
170 if (!initted) return false;
174 struct sockaddr_in sendAddr;
175 sendAddr.sin_family = AF_INET; // host byte order
176 sendAddr.sin_port = htons(port); // short, network byte order
177 struct in_addr tad; // temp struct tad needed to pass to sendAddr.sin_addr
178 tad.s_addr = inet_addr(ipa);
179 sendAddr.sin_addr = tad; // address
180 memset(&(sendAddr.sin_zero), 0, 8); // zero the rest of the struct
184 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&sendAddr), addrlen);
185 if (sentLength == length) return true;
186 LogNT::getInstance()->error(TAG, "sendto failed, errno = {}", errno);
191 ULONG UDP4::getIPNumber(ULONG)
196 ULONG UDP4::getIPNumber(ULONG num)
201 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
203 return INADDR_ANY; //well take any address, if we fail
206 struct hostent *hosts=gethostbyname(buffer);
209 return INADDR_ANY; //well take any address, if we fail
213 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
215 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
216 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
217 return returnaddress;