]> git.vomp.tv Git - vompclient.git/blob - dsock.cc
Change WSelectList option data to void*. About 65 CWFs
[vompclient.git] / dsock.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "dsock.h"
21
22 DatagramSocket::DatagramSocket(short port)
23 : myPort(port)
24 {
25   addrlen = sizeof(struct sockaddr);
26 }
27
28 DatagramSocket::~DatagramSocket()
29 {
30   if (initted) shutdown();
31 }
32
33 int DatagramSocket::init()
34 {
35   if (initted) return 0;
36
37   if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
38   { perror("socket"); return 0; }
39
40   myAddr.sin_family = AF_INET;         // host byte order
41   myAddr.sin_port = htons(myPort);     // short, network byte order
42   myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
43   memset(&(myAddr.sin_zero), 0, 8);    // zero the rest of the struct
44
45   if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
46   { perror("bind"); return 0; }
47
48   FD_ZERO(&readfds);
49   FD_SET(socketnum, &readfds);
50   tv.tv_sec = 0;
51   tv.tv_usec = 0;
52
53   int allowed = 1;
54   setsockopt(socketnum, SOL_SOCKET, SO_BROADCAST, static_cast<void*>(&allowed), sizeof(allowed));
55
56   initted = true;
57
58   return 1;
59 }
60
61 void DatagramSocket::shutdown()
62 {
63   if (!initted) return;
64   CLOSESOCKET(socketnum);
65   initted = false;
66 }
67
68 unsigned char DatagramSocket::waitforMessage(unsigned char how, int quitPipe)
69 {
70   if (!initted) return 0;
71
72   /* how = 0 - block
73      how = 1 - start new wait
74      how = 2 - continue wait
75      how = 3 - block, return on byte from quitPipe
76   */
77
78   FD_ZERO(&readfds);
79   FD_SET(socketnum, &readfds);
80
81   struct timeval* passToSelect = NULL;
82
83   int sockMaxP1 = socketnum + 1;
84
85   if (how == 1)
86   {
87     tv.tv_sec = 1;
88     tv.tv_usec = 500000;
89     passToSelect = &tv;
90   }
91   else if (how == 2)
92   {
93     if ((tv.tv_sec == 0) && (tv.tv_usec == 0))  // protection in case timer = 0
94     {
95       tv.tv_sec = 1;
96       tv.tv_usec = 500000;
97     }
98     passToSelect = &tv;
99   }
100   else if (how == 3)
101   {
102     FD_SET(quitPipe, &readfds);
103     if (quitPipe > socketnum) sockMaxP1 = quitPipe + 1;
104   }
105
106
107   if (select(sockMaxP1, &readfds, NULL, NULL, passToSelect) <= 0)
108   {  return 1;  }
109
110   if ((how == 3) && FD_ISSET(quitPipe, &readfds)) return 3;
111
112   if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0,
113       reinterpret_cast<struct sockaddr *>(&theirAddr), &addrlen)) == -1)
114   { perror("recvfrom"); return 0; }
115   else
116   {
117     memset(&buf[mlength], 0, MAXBUFLEN - mlength);
118     strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr));
119     fromPort = ntohs(theirAddr.sin_port);
120
121     if (DSOCKDEBUG)
122     {
123       //printf("%s:%i\tIN  %i\t", fromIPA, fromPort, mlength);
124       int k;
125       for(k = 0; k < mlength; k++)
126         printf("%u ", static_cast<unsigned char>(buf[k]));
127       printf("\n");
128     }
129     return 2;
130   }
131
132   /* Return 0, failure
133      Return 1, nothing happened, timer expired
134      Return 2, packet arrived (timer not expired)
135   */
136 }
137
138 UINT DatagramSocket::getDataLength(void) const
139 {
140   return static_cast<UINT>(mlength);
141 }
142
143 const void* DatagramSocket::getData() const     {  return buf;  }
144 const char* DatagramSocket::getFromIPA() const  {  return fromIPA;  }
145 short DatagramSocket::getFromPort() const       {  return fromPort; }
146
147 void DatagramSocket::send(const char *ipa, short port, char *message, int length)
148 {
149   if (!initted) return;
150
151   if (DSOCKDEBUG)
152   {
153     printf("%s:%i\tOUT %i\t", ipa, port, length);
154     int k;
155     UCHAR l;
156     for (k = 0; k < length; k++)
157       { l = static_cast<UCHAR>(message[k]); printf("%u ", l); }
158   }
159
160   int sentLength = 0;
161
162   theirAddr.sin_family = AF_INET;      // host byte order
163   theirAddr.sin_port = htons(port);    // short, network byte order
164   struct in_addr tad;                  // temp struct tad needed to pass to theirAddr.sin_addr
165   tad.s_addr = inet_addr(ipa);
166   theirAddr.sin_addr = tad;            // address
167   memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct
168
169   errno = 0;
170
171   sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
172   if (sentLength == length)
173   {
174     if (DSOCKDEBUG) printf(" GOOD\n");
175   }
176   else
177   {
178     if (DSOCKDEBUG)
179     {
180       printf(" --BAD--");  fflush(stdout);
181     }
182     sentLength = sendto(socketnum, message, length, 0, reinterpret_cast<struct sockaddr *>(&theirAddr), addrlen);
183     if (sentLength == length)
184     {
185       if (DSOCKDEBUG) printf(" GOOD\n");
186     }
187     else
188     {
189       if (DSOCKDEBUG && (sentLength != length))
190       {
191
192         printf(" -#-FAILED-#-\n");
193         printf("--------------\n");
194         printf("Sendto failure\n");
195         printf("--------------\n");
196         printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength);
197         perror("Perror reports");
198         printf("errno value: %d\n", errno);
199         printf("errno translated: %s\n", strerror(errno));
200       //  printf("h_errno value: %d\n", h_errno);
201       //  printf("\nActual address: %s\n", inet_ntoa(tad));
202       //  printf("Actual port: %i\n", ntohs(theirAddr.sin_port));
203         printf("continuing...\n\n");
204
205       }
206     }
207   }
208 }
209
210 #ifndef WIN32
211 ULONG DatagramSocket::getIPNumber(ULONG)
212 {
213   return INADDR_ANY;
214 }
215 #else
216 ULONG DatagramSocket::getIPNumber(ULONG num)
217 {
218   char buffer[100];
219   ULONG returnaddress;
220
221   if (gethostname(buffer,sizeof(buffer))==SOCKET_ERROR)
222   {
223     return INADDR_ANY; //well take any address, if we fail
224   }
225
226   struct hostent *hosts=gethostbyname(buffer);
227   if (hosts==NULL)
228   {
229     return INADDR_ANY; //well take any address, if we fail
230   }
231
232   int num_ip=0;
233   for (num_ip=0;hosts->h_addr_list[num_ip]!=NULL;num_ip++);
234
235   int get_ip=(num%num_ip);//Just wrap around, if no interface are present any more
236   memcpy(&returnaddress, hosts->h_addr_list[get_ip], sizeof(ULONG));
237   return returnaddress;
238 }
239 #endif