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