1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <headers.h>
#include <datatypes.h>
#include <display.h>
#include <parser.h>
#include <leases.h>
#include <options.h>

int parse_dhcp_options( DHCPMESSAGE *dhcpm, DHCPOPTIONS *dhcpo )
{
  int pointer, opointer;
  int olength;

  pointer = 0;

  fprintf( stdout, "parse_dhcp_options [begin]!\n");

  /* Options Initialization */
  /* No message type */
  dhcpo->type = 0;
  /* No ip address, 0.0.0.0 */
  dhcpo->r_ip = 0;
  /* No mask address, 0.0.0.0 */
  dhcpo->r_mask = 0;
  /* No router, 0.0.0.0 */
  dhcpo->r_router = 0;
  /* No lease 0 */
  dhcpo->r_lease = 0;
  /* No name '\n' */
  dhcpo->hostname = NULL;

  while( pointer< 312 )
    {
      if(( dhcpm->options[0] != 99 ) && (dhcpm->options[1]!=130) && (dhcpm->options[2]!=83) && (dhcpm->options[3]!= 99))
	{
	  fprintf( stdout, "No magic cookie! Aborting! \n" );
	  return -1;
	}
      switch( dhcpm->options[pointer] ){
      case PAD:
	pointer++;
	break;
      case MESSAGETYPE:
	/* Try to figure out the kind of message and start the configuring process */
	pointer += 2;
	dhcpo->type = dhcpm->options[pointer++];
	break;
      case PREQUEST:
	/* Take note of the requested parameters */
	opointer = pointer + 2;
	olength = pointer + dhcpm->options[pointer + 1];
	while( opointer < olength )
	  {
	    switch( dhcpm->options[opointer] ){
	    case IP:
	      /* Take note of the requested ip */
	      opointer += 2;
	      dhcpo->r_ip += dhcpm->options[opointer++];
	      dhcpo->r_ip = dhcpo->r_ip << 8;
	      dhcpo->r_ip += dhcpm->options[opointer++];
	      dhcpo->r_ip = dhcpo->r_ip << 8;
	      dhcpo->r_ip += dhcpm->options[opointer++];
	      dhcpo->r_ip = dhcpo->r_ip << 8;
	      dhcpo->r_ip += dhcpm->options[opointer++];
	      break;
	    case MASK:
	      /* Take note of the requested mask */
	      opointer += 2;
	      dhcpo->r_mask += dhcpm->options[opointer++];<--- dhcpo->r_mask is assigned
	      dhcpo->r_mask = dhcpo->r_ip << 8;<--- dhcpo->r_mask is overwritten
	      dhcpo->r_mask += dhcpm->options[opointer++];<--- dhcpo->r_mask is assigned
	      dhcpo->r_mask = dhcpo->r_ip << 8;<--- dhcpo->r_mask is overwritten
	      dhcpo->r_mask += dhcpm->options[opointer++];<--- dhcpo->r_mask is assigned
	      dhcpo->r_mask = dhcpo->r_ip << 8;<--- dhcpo->r_mask is overwritten
	      dhcpo->r_mask += dhcpm->options[opointer++];
	      break;
	    case ROUTER:
	      /* Take note of the requested router */
	      opointer += 2;
	      dhcpo->r_router += dhcpm->options[opointer++];<--- dhcpo->r_router is assigned
	      dhcpo->r_router = dhcpo->r_ip << 8;<--- dhcpo->r_router is overwritten
	      dhcpo->r_router += dhcpm->options[opointer++];<--- dhcpo->r_router is assigned
	      dhcpo->r_router = dhcpo->r_ip << 8;<--- dhcpo->r_router is overwritten
	      dhcpo->r_router += dhcpm->options[opointer++];<--- dhcpo->r_router is assigned
	      dhcpo->r_router = dhcpo->r_ip << 8;<--- dhcpo->r_router is overwritten
	      dhcpo->r_router += dhcpm->options[opointer++];
	      break;
	    case LEASE:
	      opointer += 2;
	      dhcpo->r_lease += dhcpm->options[opointer++];<--- dhcpo->r_lease is assigned
	      dhcpo->r_lease = dhcpo->r_ip << 8;<--- dhcpo->r_lease is overwritten
	      dhcpo->r_lease += dhcpm->options[opointer++];<--- dhcpo->r_lease is assigned
	      dhcpo->r_lease = dhcpo->r_ip << 8;<--- dhcpo->r_lease is overwritten
	      dhcpo->r_lease += dhcpm->options[opointer++];<--- dhcpo->r_lease is assigned
	      dhcpo->r_lease = dhcpo->r_ip << 8;<--- dhcpo->r_lease is overwritten
	      dhcpo->r_lease += dhcpm->options[opointer++];
	      break;
	    case HOSTNAME:
	      opointer += 1;
	      dhcpo->hostname = (char *)malloc( dhcpm->options[opointer] + 1);
	      strncpy( dhcpo->hostname, &dhcpm->options[opointer+1], dhcpm->options[opointer] );
	      opointer += dhcpm->options[opointer] + 1;
	    default:
	      /* Ignore option */
	      opointer++;
	      break;
	    }
	  }
	pointer = opointer;
	break;
      case  TOFFSET:
      case  TIMESERVER:
      case  NS:
      case  DNS:
      case  LOGSERVER:
      case  COOKIESERVER:
      case  LPRSERVER:
      case  IMPSERVER:
      case  RESLOCSERVER:
      case  BOOTFILESIZE:
      case  MERITDUMPFILE:
      case  DOMAINNAME:
      case  SWAPSERVER:
      case  ROOTPATH:
      case  EXTENSIONPATH:
      case  IPFORWARD:
      case  NONLOCAL:
      case  POLICYFILTER:
      case  MAXIMUMDATAG:
      case  DEFAULTTTL:
      case  PATHMTUATO:
      case  PATHMTUPTO:
      case  IMTU:
      case  ALLSUBLOCAL:
      case  BROADCAST:
      case  PMASKDISCOVERY:
      case  MASKSUPPLIER:
      case  PROUTERDISCOVE:
      case  RSOLICIADDRESS:
      case  STATICROUTE:
      case  TENCAPSULATION:
      case  ARPCACHE:
      case  ETHENCAPSUL:
      case  TCPDEFTTL:
      case  TCPKAI:
      case  TCPKAG:
      case  NISDOMAIN:
      case  NISSERVER:
      case  NTPSERVER:
      case  VENDORSP:
      case  NBTCPIPNS:
      case  NBTCPIPDDS:
      case  NBTCPIPNT:
      case  NBTCPIPSC:
      case  XWINFONTSERVER:
      case  XWINDISPLAY:
      case  OVERLOAD:
      case  SERVER:
      case  MESSAGE:
      case  MAXIMUMDHCP:
      case  RENEWALTIME:
      case  REBINDING:
      case  VENDORCLASS:
      case  NISPLUSDOMAIN:
      case  NISPLUSSERVER:
      case  TFTPSERVER:
      case  BOOTFILE:
      case  MOBILEIP:
      case  SMTPSERVER:
      case  POP3SERVER:
      case  NNTPSERVER:
      case  HTTPSERVER:
      case  FINGERSERVER:
      case  IRCSERVER:
      case  STREETTALKSE:
      case  STREETTALKDA:
      case CLIENT:
	pointer++;
	pointer += dhcpm->options[pointer];
      case END:
	/* return to the calling functions because this is over */
	fprintf( stdout, "parse_dhcp_options: END option found! [end]!\n");
	return 0;
      default:
	/* ignored */
	pointer++;
	break;
      }
    }
  fprintf( stdout, "parse_dhcp_options [end]!\n");
  return 0;
}

int process_dhcp_packet( DHCPMESSAGE *dhcpm, DHCPOPTIONS *dhcpo )
{
  int pointer = 4;
  DHCPLEASE dhcpl;
  char *name;

  fprintf( stdout, "process_dhcp_packet [begin]!\n");

  if( (!dhcpm) || (!dhcpo) )
    return -1;

  name = (char *)malloc( 16 );
  switch( dhcpo->type ){

  case DHCPDISCOVER:
    /* We need to send a DHCPOFFER */
    if( find_lease( &dhcpl, dhcpm->xid, dhcpm->chaddr ) < 0 )
      {
	fprintf( stdout, "No free leases! \n" );
	free( name );
	return -1;
      }
    dhcpm->op = BOOTREPLY;
    dhcpm->yiaddr = dhcpl.ip;
    dhcpm->siaddr = dhcpl.siaddr;
    strcpy(dhcpm->sname, VERSION);
    dhcpm->options[pointer++] = MESSAGETYPE;
    dhcpm->options[pointer++] = 1;
    dhcpm->options[pointer++] = DHCPOFFER;
    dhcpm->options[pointer++] = ROUTER;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.router & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.router >> 8) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.router >> 16) &0xFF);
    dhcpm->options[pointer++] = (dhcpl.router >> 24);
    dhcpm->options[pointer++] = MASK;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.mask & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.mask >> 8) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.mask >> 16) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.mask >> 24);
    dhcpm->options[pointer++] = SERVER;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.siaddr & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.siaddr >> 8) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.siaddr >> 16) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.siaddr >> 24);
    dhcpm->options[pointer++] = LEASE;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF);
    dhcpm->options[pointer++] = REBINDING;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF) - 5;
    dhcpm->options[pointer++] = RENEWALTIME;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF) - 5;
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = END;
    for( ; pointer < 312; pointer++ )
      dhcpm->options[pointer] = PAD;
    dhcpo->type = DHCPOFFER;
    strcpy( name, "255.255.255.255" );
    break;

  case DHCPREQUEST:
    /* We need to send an DHCPACK */
    dhcpm->op = BOOTREPLY;
    dhcpm->yiaddr = dhcpm->ciaddr;
    strcpy(dhcpm->sname, VERSION);
    if( confirm_lease( &dhcpl, dhcpm->xid ) < 0)
      {
	dhcpm->options[pointer++] = MESSAGETYPE;
	dhcpm->options[pointer++] = 1;
	dhcpm->options[pointer++] = DHCPNAK;
	dhcpm->options[pointer++] = PAD;
	dhcpm->options[pointer++] = END;
	for( ; pointer < 312; pointer++ )
	  dhcpm->options[pointer] = PAD;
	sprintf( name, "%u.%u.%u.%u", (dhcpm->ciaddr &0xFF), ((dhcpm->ciaddr>>8)&0xFF), ((dhcpm->ciaddr>>16)&0xFF), ((dhcpm->ciaddr>>24)&0xFF));
	display_dhcp_packet( dhcpm, dhcpo );
	write_packet( dhcpm, name );
	free( name );
	return -1;
      }
    dhcpm->siaddr = dhcpl.siaddr;
    dhcpm->options[pointer++] = MESSAGETYPE;
    dhcpm->options[pointer++] = 1;
    dhcpm->options[pointer++] = DHCPACK;
    dhcpm->options[pointer++] = ROUTER;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.router >> 24);
    dhcpm->options[pointer++] = ((dhcpl.router >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.router >> 8) &0xFF);
    dhcpm->options[pointer++] = (dhcpl.router & 0xFF);
    dhcpm->options[pointer++] = MASK;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.mask >> 24);
    dhcpm->options[pointer++] = ((dhcpl.mask >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.mask >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.mask & 0xFF);
    dhcpm->options[pointer++] = SERVER;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = dhcpl.siaddr >> 24;
    dhcpm->options[pointer++] = ((dhcpl.siaddr >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.siaddr >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.siaddr & 0xFF);
    dhcpm->options[pointer++] = LEASE;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF);
    dhcpm->options[pointer++] = REBINDING;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF);
    dhcpm->options[pointer++] = RENEWALTIME;
    dhcpm->options[pointer++] = 4;
    dhcpm->options[pointer++] = (dhcpl.lease >> 24);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 16) & 0xFF);
    dhcpm->options[pointer++] = ((dhcpl.lease >> 8) & 0xFF);
    dhcpm->options[pointer++] = (dhcpl.lease & 0xFF);
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = PAD;
    dhcpm->options[pointer++] = END;
    for( ; pointer < 312; pointer++ )
      dhcpm->options[pointer] = PAD;
    dhcpo->type = DHCPACK;
    sprintf( name, "%u.%u.%u.%u", (dhcpl.ip & 0xFF), ((dhcpl.ip>>8) & 0xFF), ((dhcpl.ip>>16)&0xFF), (dhcpl.ip>>24));
    break;

  default:
    break;
  }
  display_dhcp_packet( dhcpm, dhcpo );
  write_packet( dhcpm, name );
  fprintf( stdout, "process_dhcp_packet [end]!\n");
  free( name );
  return 0;
}

int write_packet( DHCPMESSAGE *dhcpm, char *name )
{
  int sockfd;
  struct sockaddr_in their_addr; // connector's address information
  struct hostent *he;
  int numbytes;
  int enable = 1;

  fprintf( stdout, "write_packet [begin]\n" );

  if( !dhcpm )
    return -1;

  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }

  if( setsockopt( sockfd, SOL_SOCKET, SO_BROADCAST, &enable, sizeof( enable )) == -1 )
    {
      perror("setsockopt");
      exit(1);
    }

  if( strcmp( "255.255.255.255", name ) )
    {
      if ((he=gethostbyname(name)) == NULL) {  // get the host info
	perror("gethostbyname");
	fprintf( stdout, "Unknown host %s \n", name );
	return -1;
      }
      their_addr.sin_family = AF_INET;     // host byte order
      their_addr.sin_port = htons(68); // short, network byte order
      their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    } else {
      their_addr.sin_family = AF_INET;     // host byte order
      their_addr.sin_port = htons(68); // short, network byte order
      their_addr.sin_addr.s_addr = 0xFFFFFFFF;
    }

  fprintf( stdout, "IP a buscar: %s \n", name );
  memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct

  if ((numbytes=sendto(sockfd, dhcpm, sizeof(DHCPMESSAGE), 0,
		       (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
    perror("sendto");
    exit(1);
  }

  printf("sent %d bytes to %s\n", numbytes,
	 inet_ntoa(their_addr.sin_addr));

  close(sockfd);

  fprintf( stdout, "write_packet [end]\n" );

  return 0;
}