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