2 Copyright 2004-2005 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/>.
22 DatagramSocket::DatagramSocket(short port)
25 addrlen = sizeof(struct sockaddr);
28 DatagramSocket::~DatagramSocket()
30 if (initted) shutdown();
33 int DatagramSocket::init()
35 if (initted) return 0;
37 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
38 { perror("socket"); return 0; }
40 myAddr.sin_family = AF_INET; // host byte order
41 myAddr.sin_port = htons(myPort); // short, network byte order
42 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
43 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
45 if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
46 { perror("bind"); return 0; }
49 FD_SET(socketnum, &readfds);
56 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&allowed), sizeof(allowed));
58 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&allowed), sizeof(allowed));
66 void DatagramSocket::shutdown()
69 CLOSESOCKET(socketnum);
74 unsigned char DatagramSocket::waitforMessage(unsigned char how, SOCKET quitPipe)
76 unsigned char DatagramSocket::waitforMessage(unsigned char how, int quitPipe)
79 if (!initted) return 0;
82 how = 1 - start new wait
83 how = 2 - continue wait
84 how = 3 - block, return on byte from quitPipe
88 FD_SET(socketnum, &readfds);
90 struct timeval* passToSelect = NULL;
92 int sockMaxP1 = socketnum + 1;
102 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
111 FD_SET(quitPipe, &readfds);
112 if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
116 if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
119 if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
121 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
122 reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
123 { perror("recvfrom"); return 0; }
126 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
127 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
128 fromPort = ntohs(theirAddr.sin_port);
132 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
134 for(k = 0; k < mlength; k++)
135 printf("%u ", static_cast<unsigned char>(buf[k]));
142 Return 1, nothing happened, timer expired
143 Return 2, packet arrived (timer not expired)
147 UINT DatagramSocket::getDataLength(void) const
149 return static_cast<UINT>(mlength);
152 const void* DatagramSocket::getData() const { return buf; }
153 const char* DatagramSocket::getFromIPA() const { return fromIPA; }
154 short DatagramSocket::getFromPort() const { return fromPort; }
156 void DatagramSocket::send(const char *ipa, short port, char *message, int length)
158 if (!initted) return;
162 printf("%s:%i\tOUT %i\t", ipa, port, length);
165 for (k = 0; k < length; k++)
166 { l = static_cast<UCHAR>(message[k]); printf("%u ", l); }
171 theirAddr.sin_family = AF_INET; // host byte order
172 theirAddr.sin_port = htons(port); // short, network byte order
173 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
174 tad.s_addr = inet_addr(ipa);
175 theirAddr.sin_addr = tad; // address
176 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
180 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
181 if (sentLength == length)
183 if (DSOCKDEBUG) printf(" GOOD\n");
189 printf(" --BAD--"); fflush(stdout);
191 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
192 if (sentLength == length)
194 if (DSOCKDEBUG) printf(" GOOD\n");
198 if (DSOCKDEBUG && (sentLength != length))
201 printf(" -#-FAILED-#-\n");
202 printf("--------------\n");
203 printf("Sendto failure\n");
204 printf("--------------\n");
205 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
206 perror("Perror reports");
207 printf("errno value: %d\n", errno);
208 printf("errno translated: %s\n", strerror(errno));
209 // printf("h_errno value: %d\n", h_errno);
210 // printf("\nActual address: %s\n", inet_ntoa(tad));
211 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
212 printf("continuing...\n\n");
220 ULONG DatagramSocket::getIPNumber(ULONG)
225 ULONG DatagramSocket::getIPNumber(ULONG num)
230 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
232 return INADDR_ANY; //well take any address, if we fail
235 struct hostent *hosts=gethostbyname(buffer);
238 return INADDR_ANY; //well take any address, if we fail
242 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
244 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
245 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
246 return returnaddress;