2 Copyright 2006 Chris Tallon
4 This file is part of VOMP.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //void dump(unsigned char* data, USHORT size);
24 //unsigned char dcc(UCHAR c);
28 log = Log::getInstance();
36 int Bootpd::shutdown()
38 if (threadIsActive()) threadCancel();
46 if (threadIsActive()) return 1;
47 log->log("BOOTPD", Log::DEBUG, "Starting bootpd");
51 log->log("BOOTPD", Log::DEBUG, "DSock init error");
58 log->log("BOOTPD", Log::DEBUG, "Thread start error");
63 log->log("BOOTPD", Log::DEBUG, "Bootp replier started");
67 void Bootpd::threadMethod()
72 log->log("BOOTPD", Log::DEBUG, "Starting wait");
73 retval = ds.waitforMessage(0);
74 log->log("BOOTPD", Log::DEBUG, "Wait finished");
78 log->log("BOOTPD", Log::CRIT, "Wait for packet error");
87 processRequest((UCHAR*)ds.getData(), ds.getDataLength());
92 void Bootpd::processRequest(UCHAR* data, int length)
94 log->log("BOOTPD", Log::DEBUG, "Got request");
95 // dump(data, (USHORT)length);
97 if (data[0] != 1) return; // Check it's a request
100 // Open a config file for the given MAC
102 const char* configDir = cPlugin::ConfigDirectory();
103 // char* configDir = "/opt/dvb/vdr-config-1.3/plugins";
106 log->log("BOOTPD", Log::ERR, "No config dir!");
111 char configFileName[PATH_MAX];
112 snprintf(configFileName, PATH_MAX, "%s/vomp-%02X-%02X-%02X-%02X-%02X-%02X.conf", configDir, data[28], data[29], data[30], data[31], data[32], data[33]);
113 if (config.init(configFileName))
115 log->log("BOOTPD", Log::DEBUG, "Opened config file: %s", configFileName);
119 log->log("BOOTPD", Log::ERR, "Could not open/create config file: %s", configFileName);
123 // Get an IP for the MVP (make a local copy so future returns don't all have to free string)
124 char newClientIP[100];
125 newClientIP[0] = '\0';
126 bool configHasIP = false;
127 char* cfnewClientIP = config.getValueString("Boot", "IP");
130 strncpy(newClientIP, cfnewClientIP, 99);
131 delete[] cfnewClientIP;
134 log->log("BOOTPD", Log::DEBUG, "Found IP %s for MVP", newClientIP);
138 log->log("BOOTPD", Log::WARN, "No IP found for MVP. Hopefully it has one already...");
141 // See if we should enforce the IP from the config file
143 long enforceConfigIP = config.getValueLong("Boot", "Override IP", &failure);
144 if (newClientIP && enforceConfigIP)
146 log->log("BOOTPD", Log::DEBUG, "Will enforce IP %s on MVP even if it already has another", newClientIP);
150 log->log("BOOTPD", Log::DEBUG, "Will not change MVP IP if it already has one");
153 // See if the MVP already has an IP
154 bool clientAlreadyHasIP = (data[12] || data[13] || data[14] || data[15]);
157 Subset of Bootp protocol for MVP
161 Opcode 0 Check for value 1 Set to value 2
163 Hardware address length 2
169 Your IP 16-19 Set to MVP IP
170 Server IP 20-23 Set to server IP
172 Client HW address 28-43 (16) Use to lookup IP for MVP
173 Server Host Name 44-107 (64)
174 Boot filename 108-235 (128) Fill with filename for TFTP
175 Vendor info 236-299 (64)
179 mvpAlreadyHasIP configHasIP enforceConfigIP result
182 3 Y 0 1 0 set config ip
183 4 Y 0 1 1 set config ip
185 6 Y 1 0 1 set enforce false, set mvp ip
187 8 Y 1 1 1 set config ip
190 if (!clientAlreadyHasIP && !configHasIP) // cases 1 & 2
192 log->log("BOOTPD", Log::DEBUG, "No IP found to give to MVP");
196 if (!configHasIP) enforceConfigIP = 0; // case 6
198 // Ok, we will send a reply
200 in_addr_t finalMVPIP;
202 if ((!clientAlreadyHasIP) || (configHasIP && enforceConfigIP))
204 // Cases 3 & 4, case 8
205 log->log("BOOTPD", Log::DEBUG, "Giving MVP IP from config");
207 *((in_addr_t*)&data[16]) = inet_addr(newClientIP);
208 finalMVPIP = *((in_addr_t*)&data[16]);
213 // copy existing ciaddr to yiaddr?
214 log->log("BOOTPD", Log::DEBUG, "Leave YI=0 as MVP already has good IP");
215 finalMVPIP = *((in_addr_t*)&data[12]);
218 // Set Server IP in packet
219 if (!getmyip(finalMVPIP, (in_addr_t*)&data[20]))
221 log->log("BOOTPD", Log::ERR, "Get my IP failed");
225 strncpy((char*)&data[108], "/mvp/vomp-dongle", 127);
230 // dump(data, (USHORT)length);
232 ds.send("255.255.255.255", 16868, (char*)data, length);
236 int Bootpd::getmyip(in_addr_t destination, in_addr_t* result)
239 struct sockaddr_in my_addr;
240 struct sockaddr_in dest_addr;
241 socklen_t my_addr_len = sizeof(struct sockaddr_in);
243 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
244 if (sockfd == -1) return 0;
246 dest_addr.sin_family = AF_INET;
247 dest_addr.sin_port = htons(1);
248 dest_addr.sin_addr.s_addr = destination;
249 memset(&(dest_addr.sin_zero), 0, 8);
251 r1 = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
258 memset(&my_addr, 0, sizeof(struct sockaddr_in));
259 r2 = getsockname(sockfd, (struct sockaddr *)&my_addr, &my_addr_len);
263 if (r2 == -1) return 0;
265 *result = my_addr.sin_addr.s_addr;
270 void dump(unsigned char* data, USHORT size)
272 printf("Size = %u\n", size);
279 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
280 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
281 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15],
282 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
283 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]), dcc(data[c+15]));
291 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
292 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
293 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14],
294 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
295 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]));
299 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
300 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
301 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13],
302 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
303 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]));
307 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c\n",
308 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
309 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12],
310 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
311 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]));
315 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c\n",
316 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
317 data[c+8], data[c+9], data[c+10], data[c+11],
318 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
319 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]));
323 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c\n",
324 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
325 data[c+8], data[c+9], data[c+10],
326 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
327 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]));
331 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c\n",
332 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
333 data[c+8], data[c+9],
334 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
335 dcc(data[c+8]), dcc(data[c+9]));
339 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c\n",
340 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
342 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
347 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
348 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
349 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]));
353 printf(" %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c\n",
354 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6],
355 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]));
359 printf(" %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c\n",
360 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5],
361 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]));
365 printf(" %02X %02X %02X %02X %02X %c%c%c%c%c\n",
366 data[c], data[c+1], data[c+2], data[c+3], data[c+4],
367 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]));
371 printf(" %02X %02X %02X %02X %c%c%c%c\n",
372 data[c], data[c+1], data[c+2], data[c+3],
373 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]));
377 printf(" %02X %02X %02X %c%c%c\n",
378 data[c], data[c+1], data[c+2],
379 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]));
383 printf(" %02X %02X %c%c\n",
385 dcc(data[c]), dcc(data[c+1]));
399 unsigned char dcc(UCHAR c)
401 if (isspace(c)) return ' ';
402 if (isprint(c)) return c;