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