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)
26 addrlen = sizeof(struct sockaddr);
27 log = Log::getInstance();
30 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
32 log->log("UDP", Log::CRIT, "Socket error");
36 myAddr.sin_family = AF_INET; // host byte order
37 myAddr.sin_port = htons(myPort); // short, network byte order
38 myAddr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
39 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
40 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
42 log->log("UDP", Log::CRIT, "Bind error");
49 FD_SET(socketnum, &readfds);
54 DatagramSocket::~DatagramSocket()
56 if (initted) close(socketnum);
60 unsigned char DatagramSocket::waitforMessage(unsigned char how)
62 if (!initted) return 0;
65 how = 1 - start new wait
66 how = 2 - continue wait
69 struct timeval* passToSelect = NULL;
84 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
92 FD_SET(socketnum, &readfds);
94 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0) return 1;
96 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0, (struct sockaddr *)&theirAddr, &addrlen)) == -1)
98 log->log("UDP", Log::DEBUG, "recvfrom error");
103 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
104 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
105 fromPort = ntohs(theirAddr.sin_port);
106 log->log("UDP", Log::DEBUG, "%s:%i received length %i", fromIPA, fromPort, mlength);
111 Return 1, nothing happened, timer expired
112 Return 2, packet arrived (timer not expired)
116 int DatagramSocket::getDataLength(void) const
121 char *DatagramSocket::getData(void) { return buf; }
122 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
123 short DatagramSocket::getFromPort(void) const { return fromPort; }
125 void DatagramSocket::send(char *ipa, short port, char *message, int length)
129 theirAddr.sin_family = AF_INET; // host byte order
130 theirAddr.sin_port = htons(port); // short, network byte order
131 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
132 tad.s_addr = inet_addr(ipa);
133 theirAddr.sin_addr = tad; // address
134 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
136 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
137 if (sentLength == length)
139 log->log("UDP", Log::DEBUG, "%s:%i sent length %i", ipa, port, length);
143 log->log("UDP", Log::DEBUG, "%s:%i send failed %i", ipa, port, length);
145 sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen);
146 if (sentLength == length)
148 log->log("UDP", Log::DEBUG, "%s:%i sent length %i 2nd try", ipa, port, length);
152 log->log("UDP", Log::DEBUG, "%s:%i send failed %i 2nd try", ipa, port, length);