]> git.vomp.tv Git - vompserver.git/blob - mvprelay.c
15 years that line of code has been waiting to crash
[vompserver.git] / mvprelay.c
1 /*
2     Copyright 2007 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 "mvprelay.h"
22
23 MVPRelay::MVPRelay()
24 {
25 }
26
27 MVPRelay::~MVPRelay()
28 {
29   shutdown();
30 }
31
32 int MVPRelay::shutdown()
33 {
34   if (threadIsActive()) threadCancel();
35
36   return 1;
37 }
38
39 int MVPRelay::run()
40 {
41   if (threadIsActive()) return 1;
42
43   if (!ds.init(16881))
44   {
45     Log::getInstance()->log("MVPRelay", Log::CRIT, "Could not open UDP 16881");
46     shutdown();
47     return 0;
48   }
49
50   if (!threadStart())
51   {
52     shutdown();
53     return 0;
54   }
55
56   Log::getInstance()->log("MVPRelay", Log::DEBUG, "MVPRelay replier started");
57   return 1;
58 }
59
60 void MVPRelay::threadMethod()
61 {
62   int retval;
63   while(1)
64   {
65     retval = ds.waitforMessage(0);
66     if (retval == 1) continue;
67
68     Log::getInstance()->log("MVPRelay", Log::DEBUG, "MVPRelay request from %s", ds.getFromIPA());
69
70 //    TCP::dump((UCHAR*)ds.getData(), ds.getDataLength());
71
72     UCHAR* in = (UCHAR*)ds.getData();
73
74     // Check incoming packet magic number
75
76     ULONG inMagic = ntohl(*(ULONG*)&in[4]);
77     if (inMagic != 0xbabefafe)
78     {
79       Log::getInstance()->log("MVPRelay", Log::DEBUG, "inMagic not correct");
80       continue;
81     }
82
83     // Get peer info
84     USHORT peerPort = ntohs(*(USHORT*)&in[20]);
85
86     // Get my IP for this connection
87     ULONG myIP = ds.getMyIP(*(ULONG*)&in[16]);
88     Log::getInstance()->log("MVPRelay", Log::DEBUG, "Sending my IP as %x", ntohl(myIP));
89
90     // Required return parameters:
91     // -Seq number
92     // -Magic number
93     // -Client IP & port
94     // -First server IP
95
96     // Construct reply packet
97
98     ULONG* p;
99     UCHAR out[52];
100     memset(out, 0, 52);
101
102     // Copy sequence number
103     memcpy(out, in, 4);
104
105     // Return magic number is 0xfafebabe
106     p = (ULONG*)&out[4];
107     *p = htonl(0xfafebabe);
108
109     // Copy client IP and port to reply
110     memcpy(&out[16], &in[16], 6);
111
112     // Insert server address
113     p = (ULONG*)&out[24];
114     *p = myIP;
115
116     // Send it
117     ds.send(ds.getFromIPA(), peerPort, (char*)out, 52);
118
119 //    TCP::dump(out, 52);
120   }
121 }