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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 DatagramSocket::DatagramSocket(short port)
27 addrlen = sizeof(struct sockaddr);
31 DatagramSocket::~DatagramSocket()
33 if (initted) shutdown();
36 int DatagramSocket::init()
38 if (initted) return 0;
40 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
41 { perror("socket"); return 0; }
43 myAddr.sin_family = AF_INET; // host byte order
44 myAddr.sin_port = htons(myPort); // short, network byte order
45 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
46 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
48 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
49 { perror("bind"); return 0; }
52 FD_SET(socketnum, &readfds);
57 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, (char*)&allowed, sizeof(allowed));
64 void DatagramSocket::shutdown()
67 CLOSESOCKET(socketnum);
71 unsigned char DatagramSocket::waitforMessage(unsigned char how)
73 if (!initted) return 0;
76 how = 1 - start new wait
77 how = 2 - continue wait
80 struct timeval* passToSelect = NULL;
95 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
103 FD_SET(socketnum, &readfds);
105 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
108 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
109 (struct sockaddr *)&theirAddr, &addrlen)) == -1)
110 { perror("recvfrom"); return 0; }
113 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
114 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
115 fromPort = ntohs(theirAddr.sin_port);
119 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
121 for(k = 0; k < mlength; k++)
122 printf("%u ", (unsigned char)buf[k]);
129 Return 1, nothing happened, timer expired
130 Return 2, packet arrived (timer not expired)
134 int DatagramSocket::getDataLength(void) const
139 char *DatagramSocket::getData(void) { return buf; }
140 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
141 short DatagramSocket::getFromPort(void) const { return fromPort; }
143 void DatagramSocket::send(const char *ipa, short port, char *message, int length)
145 if (!initted) return;
149 printf("%s:%i\tOUT %i\t", ipa, port, length);
152 for (k = 0; k < length; k++)
153 { l = (UCHAR)message[k]; printf("%u ", l); }
158 theirAddr.sin_family = AF_INET; // host byte order
159 theirAddr.sin_port = htons(port); // short, network byte order
160 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
161 tad.s_addr = inet_addr(ipa);
162 theirAddr.sin_addr = tad; // address
163 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
167 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
168 if (sentLength == length)
170 if (DSOCKDEBUG) printf(" GOOD\n");
176 printf(" --BAD--"); fflush(stdout);
178 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
179 if (sentLength == length)
181 if (DSOCKDEBUG) printf(" GOOD\n");
185 if (DSOCKDEBUG && (sentLength != length))
188 printf(" -#-FAILED-#-\n");
189 printf("--------------\n");
190 printf("Sendto failure\n");
191 printf("--------------\n");
192 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
193 perror("Perror reports");
194 printf("errno value: %d\n", errno);
195 printf("errno translated: %s\n", strerror(errno));
196 // printf("h_errno value: %d\n", h_errno);
197 // printf("\nActual address: %s\n", inet_ntoa(tad));
198 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
199 printf("continuing...\n\n");
206 ULONG DatagramSocket::getIPNumber(ULONG num)
212 // warning: Using 'gethostbyname' in statically linked applications requires at runtime
213 // the shared libraries from the glibc version used for linking
220 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
222 return INADDR_ANY; //well take any address, if we fail
225 struct hostent *hosts=gethostbyname(buffer);
228 return INADDR_ANY; //well take any address, if we fail
232 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
234 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
235 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
236 return returnaddress;