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);
28 if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
29 { perror("socket"); exit(1); }
31 myAddr.sin_family = AF_INET; // host byte order
32 myAddr.sin_port = htons(myPort); // short, network byte order
33 myAddr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
34 memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct
35 if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1)
36 { perror("bind"); printf(" %s ", strerror(errno)); exit(1); }
39 FD_SET(socketnum, &readfds);
44 DatagramSocket::~DatagramSocket()
49 unsigned char DatagramSocket::waitforMessage(unsigned char how)
52 how = 1 - start new wait
53 how = 2 - continue wait
56 struct timeval* passToSelect = NULL;
71 if ((tv.tv_sec == 0) && (tv.tv_usec == 0)) // protection in case timer = 0
79 FD_SET(socketnum, &readfds);
81 if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0)
84 if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
85 (struct sockaddr *)&theirAddr, &addrlen)) == -1)
86 { perror("recvfrom"); return 0; }
89 memset(&buf[mlength], 0, MAXBUFLEN - mlength);
90 strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
91 fromPort = ntohs(theirAddr.sin_port);
95 printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength);
97 for(k = 0; k < mlength; k++)
98 printf("%u ", (unsigned char)buf[k]);
105 Return 1, nothing happened, timer expired
106 Return 2, packet arrived (timer not expired)
110 int DatagramSocket::getDataLength(void) const
115 char *DatagramSocket::getData(void) { return buf; }
116 char *DatagramSocket::getFromIPA(void) { return fromIPA; }
117 short DatagramSocket::getFromPort(void) const { return fromPort; }
119 void DatagramSocket::send(char *ipa, short port, char *message, int length)
123 printf("%s:%i\tOUT %i\t", ipa, port, length);
126 for (k = 0; k < length; k++)
127 { l = (uchar)message[k]; printf("%u ", l); }
132 theirAddr.sin_family = AF_INET; // host byte order
133 theirAddr.sin_port = htons(port); // short, network byte order
134 struct in_addr tad; // temp struct tad needed to pass to theirAddr.sin_addr
135 tad.s_addr = inet_addr(ipa);
136 theirAddr.sin_addr = tad; // address
137 memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
139 unsigned char crypt[MAXBUFLEN];
140 memcpy(crypt, message, length);
142 sentLength = sendto(socketnum, crypt, length, 0, (struct sockaddr *)&theirAddr, addrlen);
143 if (sentLength == length)
149 printf(" --BAD--"); fflush(stdout);
150 sentLength = sendto(socketnum, crypt, length, 0, (struct sockaddr *)&theirAddr, addrlen);
151 if (sentLength == length)
155 printf(" -#-FAILED-#-\n");
157 if (DSOCKDEBUG && (sentLength != length))
159 printf("--------------\n");
160 printf("Sendto failure\n");
161 printf("--------------\n");
162 printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
163 perror("Perror reports");
164 printf("errno value: %d\n", errno);
165 printf("errno translated: %s\n", strerror(errno));
166 // printf("h_errno value: %d\n", h_errno);
167 // printf("\nActual address: %s\n", inet_ntoa(tad));
168 // printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
169 printf("continuing...\n\n");