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 DatagramSocket::DatagramSocket(short port)
27 addrlen = sizeof(struct sockaddr);
29 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
30 { perror("socket"); exit(1); }
32 myAddr.sin_family = AF_INET; // host byte order
33 myAddr.sin_port = htons(myPort); // short, network byte order
34 myAddr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
35 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
37 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
38 { perror("bind"); exit(1); }
41 FD_SET(socketnum, &readfds);
46 setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, &allowed, sizeof(allowed));
49 DatagramSocket::~DatagramSocket()
54 DatagramSocket* DatagramSocket::theInstance = 0;
55 DatagramSocket* DatagramSocket::getInstance(void)
60 unsigned char DatagramSocket::waitforMessage(unsigned char how)
63 how = 1 - start new wait
64 how = 2 - continue wait
67 struct timeval* passToSelect = NULL;
82 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
90 FD_SET(socketnum, &readfds);
92 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
95 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
96 (struct sockaddr *)&theirAddr, &addrlen)) == -1)
97 { perror("recvfrom"); return 0; }
100 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
101 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
102 fromPort = ntohs(theirAddr.sin_port);
106 //printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
108 for(k = 0; k < mlength; k++)
109 printf("%u ", (unsigned char)buf[k]);
116 Return 1, nothing happened, timer expired
117 Return 2, packet arrived (timer not expired)
121 int DatagramSocket::getDataLength(void) const
126 char *DatagramSocket::getData(void) { return buf; }
127 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
128 short DatagramSocket::getFromPort(void) const { return fromPort; }
130 void DatagramSocket::send(char *ipa, short port, char *message, int length)
134 printf("%s:%i\tOUT %i\t", ipa, port, length);
137 for (k = 0; k < length; k++)
138 { l = (uchar)message[k]; printf("%u ", l); }
143 theirAddr.sin_family = AF_INET; // host byte order
144 theirAddr.sin_port = htons(port); // short, network byte order
145 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
146 tad.s_addr = inet_addr(ipa);
147 theirAddr.sin_addr = tad; // address
148 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
152 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
153 if (sentLength == length)
155 if (DSOCKDEBUG) printf(" GOOD\n");
161 printf(" --BAD--"); fflush(stdout);
163 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
164 if (sentLength == length)
166 if (DSOCKDEBUG) printf(" GOOD\n");
170 if (DSOCKDEBUG && (sentLength != length))
173 printf(" -#-FAILED-#-\n");
174 printf("--------------\n");
175 printf("Sendto failure\n");
176 printf("--------------\n");
177 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
178 perror("Perror reports");
179 printf("errno value: %d\n", errno);
180 printf("errno translated: %s\n", strerror(errno));
181 // printf("h_errno value: %d\n", h_errno);
182 // printf("\nActual address: %s\n", inet_ntoa(tad));
183 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
184 printf("continuing...\n\n");