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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ULONG DatagramSocket::iterate_ip = 0;
25 DatagramSocket::DatagramSocket(short port)
29 addrlen = sizeof(struct sockaddr);
33 DatagramSocket::~DatagramSocket()
35 if (initted) shutdown();
38 DatagramSocket* DatagramSocket::theInstance = 0;
39 DatagramSocket* DatagramSocket::getInstance(void)
44 int DatagramSocket::init()
46 if (initted) return 0;
48 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
49 { perror("socket"); return 0; }
51 myAddr.sin_family = AF_INET; // host byte order
52 myAddr.sin_port = htons(myPort); // short, network byte order
53 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
54 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
56 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
57 { perror("bind"); return 0; }
60 FD_SET(socketnum, &readfds);
65 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, (char*)&allowed, sizeof(allowed));
72 void DatagramSocket::shutdown()
75 CLOSESOCKET(socketnum);
79 unsigned char DatagramSocket::waitforMessage(unsigned char how)
81 if (!initted) return 0;
84 how = 1 - start new wait
85 how = 2 - continue wait
88 struct timeval* passToSelect = NULL;
103 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
111 FD_SET(socketnum, &readfds);
113 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
116 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
117 (struct sockaddr *)&theirAddr, &addrlen)) == -1)
118 { perror("recvfrom"); return 0; }
121 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
122 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
123 fromPort = ntohs(theirAddr.sin_port);
127 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
129 for(k = 0; k < mlength; k++)
130 printf("%u ", (unsigned char)buf[k]);
137 Return 1, nothing happened, timer expired
138 Return 2, packet arrived (timer not expired)
142 int DatagramSocket::getDataLength(void) const
147 char *DatagramSocket::getData(void) { return buf; }
148 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
149 short DatagramSocket::getFromPort(void) const { return fromPort; }
151 void DatagramSocket::send(char *ipa, short port, char *message, int length)
153 if (!initted) return;
157 printf("%s:%i\tOUT %i\t", ipa, port, length);
160 for (k = 0; k < length; k++)
161 { l = (UCHAR)message[k]; printf("%u ", l); }
166 theirAddr.sin_family = AF_INET; // host byte order
167 theirAddr.sin_port = htons(port); // short, network byte order
168 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
169 tad.s_addr = inet_addr(ipa);
170 theirAddr.sin_addr = tad; // address
171 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
175 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
176 if (sentLength == length)
178 if (DSOCKDEBUG) printf(" GOOD\n");
184 printf(" --BAD--"); fflush(stdout);
186 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
187 if (sentLength == length)
189 if (DSOCKDEBUG) printf(" GOOD\n");
193 if (DSOCKDEBUG && (sentLength != length))
196 printf(" -#-FAILED-#-\n");
197 printf("--------------\n");
198 printf("Sendto failure\n");
199 printf("--------------\n");
200 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
201 perror("Perror reports");
202 printf("errno value: %d\n", errno);
203 printf("errno translated: %s\n", strerror(errno));
204 // printf("h_errno value: %d\n", h_errno);
205 // printf("\nActual address: %s\n", inet_ntoa(tad));
206 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
207 printf("continuing...\n\n");
214 ULONG DatagramSocket::getIPNumber(ULONG num)
219 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
221 return INADDR_ANY; //well take any address, if we fail
224 struct hostent *hosts=gethostbyname(buffer);
227 return INADDR_ANY; //well take any address, if we fail
231 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
233 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
234 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
235 return returnaddress;