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 char* tftpFileName = config.getValueString("Boot", "TFTP file name");
228 strncpy((char*)&data[108], tftpFileName, 127);
229 delete[] tftpFileName;
240 // dump(data, (USHORT)length);
242 ds.send("255.255.255.255", 16868, (char*)data, length);
246 int Bootpd::getmyip(in_addr_t destination, in_addr_t* result)
249 struct sockaddr_in my_addr;
250 struct sockaddr_in dest_addr;
251 socklen_t my_addr_len = sizeof(struct sockaddr_in);
253 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
254 if (sockfd == -1) return 0;
256 dest_addr.sin_family = AF_INET;
257 dest_addr.sin_port = htons(1);
258 dest_addr.sin_addr.s_addr = destination;
259 memset(&(dest_addr.sin_zero), 0, 8);
261 r1 = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
268 memset(&my_addr, 0, sizeof(struct sockaddr_in));
269 r2 = getsockname(sockfd, (struct sockaddr *)&my_addr, &my_addr_len);
273 if (r2 == -1) return 0;
275 *result = my_addr.sin_addr.s_addr;
280 void dump(unsigned char* data, USHORT size)
282 printf("Size = %u\n", size);
289 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",
290 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
291 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15],
292 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]),
293 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]));
301 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",
302 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
303 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14],
304 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]),
305 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]));
309 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",
310 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
311 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13],
312 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]),
313 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]));
317 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",
318 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
319 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12],
320 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]),
321 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]));
325 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",
326 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
327 data[c+8], data[c+9], data[c+10], data[c+11],
328 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]),
329 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]));
333 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c\n",
334 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
335 data[c+8], data[c+9], data[c+10],
336 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]),
337 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]));
341 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c\n",
342 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
343 data[c+8], data[c+9],
344 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]),
345 dcc(data[c+8]), dcc(data[c+9]));
349 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c\n",
350 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
352 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]),
357 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
358 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
359 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]));
363 printf(" %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c\n",
364 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6],
365 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]));
369 printf(" %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c\n",
370 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5],
371 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]));
375 printf(" %02X %02X %02X %02X %02X %c%c%c%c%c\n",
376 data[c], data[c+1], data[c+2], data[c+3], data[c+4],
377 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]));
381 printf(" %02X %02X %02X %02X %c%c%c%c\n",
382 data[c], data[c+1], data[c+2], data[c+3],
383 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]));
387 printf(" %02X %02X %02X %c%c%c\n",
388 data[c], data[c+1], data[c+2],
389 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]));
393 printf(" %02X %02X %c%c\n",
395 dcc(data[c]), dcc(data[c+1]));
409 unsigned char dcc(UCHAR c)
411 if (isspace(c)) return ' ';
412 if (isprint(c)) return c;