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)
26 addrlen = sizeof(struct sockaddr);
30 DatagramSocket::~DatagramSocket()
32 if (initted) shutdown();
35 int DatagramSocket::init()
37 if (initted) return 0;
39 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
40 { perror("socket"); return 0; }
42 myAddr.sin_family = AF_INET; // host byte order
43 myAddr.sin_port = htons(myPort); // short, network byte order
44 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
45 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
47 if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
48 { perror("bind"); return 0; }
51 FD_SET(socketnum, &readfds);
56 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&allowed), sizeof(allowed));
63 void DatagramSocket::shutdown()
66 CLOSESOCKET(socketnum);
70 unsigned char DatagramSocket::waitforMessage(unsigned char how)
72 if (!initted) return 0;
75 how = 1 - start new wait
76 how = 2 - continue wait
79 struct timeval* passToSelect = NULL;
94 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
102 FD_SET(socketnum, &readfds);
104 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
107 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
108 reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
109 { perror("recvfrom"); return 0; }
112 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
113 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
114 fromPort = ntohs(theirAddr.sin_port);
118 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
120 for(k = 0; k < mlength; k++)
121 printf("%u ", static_cast<unsigned char>(buf[k]));
128 Return 1, nothing happened, timer expired
129 Return 2, packet arrived (timer not expired)
133 UINT DatagramSocket::getDataLength(void) const
135 return static_cast<UINT>(mlength);
138 const void* DatagramSocket::getData() const { return buf; }
139 const char* DatagramSocket::getFromIPA() const { return fromIPA; }
140 short DatagramSocket::getFromPort() const { return fromPort; }
142 void DatagramSocket::send(const char *ipa, short port, char *message, int length)
144 if (!initted) return;
148 printf("%s:%i\tOUT %i\t", ipa, port, length);
151 for (k = 0; k < length; k++)
152 { l = static_cast<UCHAR>(message[k]); printf("%u ", l); }
157 theirAddr.sin_family = AF_INET; // host byte order
158 theirAddr.sin_port = htons(port); // short, network byte order
159 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
160 tad.s_addr = inet_addr(ipa);
161 theirAddr.sin_addr = tad; // address
162 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
166 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
167 if (sentLength == length)
169 if (DSOCKDEBUG) printf(" GOOD\n");
175 printf(" --BAD--"); fflush(stdout);
177 sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
178 if (sentLength == length)
180 if (DSOCKDEBUG) printf(" GOOD\n");
184 if (DSOCKDEBUG && (sentLength != length))
187 printf(" -#-FAILED-#-\n");
188 printf("--------------\n");
189 printf("Sendto failure\n");
190 printf("--------------\n");
191 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
192 perror("Perror reports");
193 printf("errno value: %d\n", errno);
194 printf("errno translated: %s\n", strerror(errno));
195 // printf("h_errno value: %d\n", h_errno);
196 // printf("\nActual address: %s\n", inet_ntoa(tad));
197 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
198 printf("continuing...\n\n");
206 ULONG DatagramSocket::getIPNumber(ULONG)
211 ULONG DatagramSocket::getIPNumber(ULONG num)
216 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
218 return INADDR_ANY; //well take any address, if we fail
221 struct hostent *hosts=gethostbyname(buffer);
224 return INADDR_ANY; //well take any address, if we fail
228 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
230 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
231 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
232 return returnaddress;