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);
32 DatagramSocket::~DatagramSocket()
34 CLOSESOCKET(socketnum);
37 DatagramSocket* DatagramSocket::theInstance = 0;
38 DatagramSocket* DatagramSocket::getInstance(void)
43 int DatagramSocket::init()
45 CLOSESOCKET(socketnum);
46 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
47 { perror("socket"); return 0; }
49 myAddr.sin_family = AF_INET; // host byte order
50 myAddr.sin_port = htons(myPort); // short, network byte order
51 myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
52 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
54 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
55 { perror("bind"); return 0; }
58 FD_SET(socketnum, &readfds);
63 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, (char*)&allowed, sizeof(allowed));
68 unsigned char DatagramSocket::waitforMessage(unsigned char how)
71 how = 1 - start new wait
72 how = 2 - continue wait
75 struct timeval* passToSelect = NULL;
90 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
98 FD_SET(socketnum, &readfds);
100 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
103 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
104 (struct sockaddr *)&theirAddr, &addrlen)) == -1)
105 { perror("recvfrom"); return 0; }
108 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
109 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
110 fromPort = ntohs(theirAddr.sin_port);
114 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
116 for(k = 0; k < mlength; k++)
117 printf("%u ", (unsigned char)buf[k]);
124 Return 1, nothing happened, timer expired
125 Return 2, packet arrived (timer not expired)
129 int DatagramSocket::getDataLength(void) const
134 char *DatagramSocket::getData(void) { return buf; }
135 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
136 short DatagramSocket::getFromPort(void) const { return fromPort; }
138 void DatagramSocket::send(char *ipa, short port, char *message, int length)
142 printf("%s:%i\tOUT %i\t", ipa, port, length);
145 for (k = 0; k < length; k++)
146 { l = (UCHAR)message[k]; printf("%u ", l); }
151 theirAddr.sin_family = AF_INET; // host byte order
152 theirAddr.sin_port = htons(port); // short, network byte order
153 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
154 tad.s_addr = inet_addr(ipa);
155 theirAddr.sin_addr = tad; // address
156 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
160 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
161 if (sentLength == length)
163 if (DSOCKDEBUG) printf(" GOOD\n");
169 printf(" --BAD--"); fflush(stdout);
171 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
172 if (sentLength == length)
174 if (DSOCKDEBUG) printf(" GOOD\n");
178 if (DSOCKDEBUG && (sentLength != length))
181 printf(" -#-FAILED-#-\n");
182 printf("--------------\n");
183 printf("Sendto failure\n");
184 printf("--------------\n");
185 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
186 perror("Perror reports");
187 printf("errno value: %d\n", errno);
188 printf("errno translated: %s\n", strerror(errno));
189 // printf("h_errno value: %d\n", h_errno);
190 // printf("\nActual address: %s\n", inet_ntoa(tad));
191 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
192 printf("continuing...\n\n");
199 ULONG DatagramSocket::getIPNumber(ULONG num)
204 if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
206 return INADDR_ANY; //well take any address, if we fail
209 struct hostent *hosts=gethostbyname(buffer);
212 return INADDR_ANY; //well take any address, if we fail
216 for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
218 int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
219 memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
220 return returnaddress;