]> git.vomp.tv Git - vompserver.git/blob - udpreplier.c
Add forgotten vdrcommand.h changes
[vompserver.git] / udpreplier.c
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 "vompclientrrproc.h"
22
23 #include "udpreplier.h"
24
25 UDPReplier::UDPReplier()
26 {
27   message = NULL;
28   messageLen = 0;
29 }
30
31 UDPReplier::~UDPReplier()
32 {
33   shutdown();
34 }
35
36 int UDPReplier::shutdown()
37 {
38   if (threadIsActive()) threadCancel();
39
40   if (message) delete[] message;
41   message = NULL;
42   return 1;
43 }
44
45 int UDPReplier::run(USHORT port, char* serverName, USHORT serverPort)
46 {
47   if (threadIsActive()) return 1;
48
49   /*
50   VOMP Discovery Protocol V1
51
52   Client transmits: "VDP-0001\0<6-byte MAC>" on ports 51051-51055
53
54   Server responds:
55
56   Field 1 p0: 9 bytes "VDP-0002\0"
57   
58   Field 2 p9, 1 byte:
59
60   0 = no IP specified
61   4 = first 4 bytes of field 2 are IPv4 address of server
62   6 = field 2 16 bytes are IPv6 address of server
63
64   Field 3, p10, 16 bytes:
65   As described above. If field 1 is 0, this should be all zeros. If this is an IPv4 address, remaining bytes should be zeros.
66
67   Field 4 p26, 2 bytes:
68   Port number of server
69
70   Field 5 p28, 4 bytes:
71   VOMP protocol version (defined in vdr.cc)
72   
73   Field 6 p32, variable length
74   String of server name, null terminated
75   */
76   
77   messageLen = strlen(serverName) + 33;
78   message = new char[messageLen];
79   memset(message, 0, messageLen);
80   // by zeroing the packet, this sets no ip address return information
81   
82   strcpy(message, "VDP-0002");
83   
84   USHORT temp = htons(serverPort);
85   memcpy(&message[26], &temp, 2);
86   
87   ULONG temp2 = htonl(VompClientRRProc::getProtocolVersionMin());
88   memcpy(&message[28], &temp2, 4);
89   
90   strcpy(&message[32], serverName);
91   // Fix Me add also the maximum version somewhere
92   if (!ds.init(port))
93   {
94     shutdown();
95     return 0;
96   }
97
98   if (!threadStart())
99   {
100     shutdown();
101     return 0;
102   }
103
104   Log::getInstance()->log("UDPReplier", Log::DEBUG, "UDP replier started");
105   return 1;
106 }
107
108 void UDPReplier::threadMethod()
109 {
110   int retval;
111   while(1)
112   {
113     retval = ds.waitforMessage(0);
114     if (retval == 1) continue;
115
116     if (!strncmp(ds.getData(), "VDP-0001", 8))
117     {
118       Log::getInstance()->log("UDPReplier", Log::DEBUG, "UDP request from %s", ds.getFromIPA());
119       ds.send(ds.getFromIPA(), ds.getFromPort(), message, messageLen);
120     }
121   }
122 }