File: packetstore.cc

package info (click to toggle)
pxe 1.4.2-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 444 kB
  • ctags: 208
  • sloc: sh: 2,813; cpp: 2,353; makefile: 84
file content (1104 lines) | stat: -rw-r--r-- 27,933 bytes parent folder | download | duplicates (3)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
/*
 * PXE daemon - enable the remote booting of PXE enabled machines.
 * Copyright (C) 2000 Tim Hurman (kano@kano.org.uk)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
/******************************************************************************
 * packetstore.cc - decode and store a packet in memory                       *
 ******************************************************************************/

#include "packetstore.h"

/* some global variables */
const char *DHCP_types[DHCP_MAX_TYPES] = 
{
	"INVALID", "DHCPDISCOVER", "DHCPOFFER", "DHCPREQUEST",
	"DHCPDECLINE", "DHCPACK", "DHCPNACK", "DHCPRELEASE",
	"DHCPINFORM"
};

/******************************************************************************
 * PacketStore - null packet constructor                                      *
 ******************************************************************************/
PacketStore::PacketStore(LogFile *_logger)
{
	this->logger = _logger;
	head = head43 = NULL;
	Initalise();
}


/******************************************************************************
 * PacketStore - full packet analyse                                          *
 ******************************************************************************/
PacketStore::PacketStore(LogFile *_logger, struct sockaddr_in *_address,
	uint8_t *bootp_pkt, int bootp_pkt_len)
{
	this->logger = _logger;
	memcpy(&(this->address), _address, sizeof(this->address));
	ReadPacket(bootp_pkt, bootp_pkt_len);
}


/******************************************************************************
 * ~PacketStore - descructor                                                  *
 ******************************************************************************/
PacketStore::~PacketStore()
{
	Initalise();
}


/******************************************************************************
 * Initalise - init/blank a packet                                            *
 ******************************************************************************/
void
PacketStore::Initalise(void)
{
	option *optptr, *optnext;

	// basic stuff 
	op=htype=hlen=hops=0;
	secs=0;
	xid=0;
	memset(chaddr, 0, 16);
	sname[0] = file[0] = 0;
	magic_cookie = 0;
	
	memset(&ciaddr, 0, sizeof(ciaddr));
	memset(&yiaddr, 0, sizeof(yiaddr));
	memset(&siaddr, 0, sizeof(siaddr));
	memset(&giaddr, 0, sizeof(giaddr));
	memset(&address, 0, sizeof(giaddr));

	// options
	for(optptr=head; optptr != NULL; optptr=optnext)
	{
		optnext = optptr->next;
		delete[] optptr->data;
		delete optptr;
	}
	head = NULL;

	// options 43
	for(optptr=head43; optptr != NULL; optptr=optnext)
	{
		optnext = optptr->next;
		delete[] optptr->data;
		delete optptr;
	}
	head43 = NULL;
}


/******************************************************************************
 * ReadPacket - read a bootp packet                                           *
 ******************************************************************************/
int
PacketStore::ReadPacket(uint8_t *bootp_pkt, int bootp_pkt_len)
{
	if(bootp_pkt_len < 300)
		throw new SysException(0, "PacketStore::ReadPacket",
			"Invalid packet length");

	op = bootp_pkt[0];
	htype = bootp_pkt[1];
	hlen = bootp_pkt[2];
	hops = bootp_pkt[3];

	// xid - store in network byte order
	xid = bootp_pkt[4];
	xid <<= 8;
	xid |= bootp_pkt[5];
	xid <<= 8;
	xid |= bootp_pkt[6];
	xid <<= 8;
	xid |= bootp_pkt[7];
	xid = htonl(xid);

	// no of secs
	secs = bootp_pkt[8];
	secs <<= 8;
	secs |= bootp_pkt[9];
	secs = htons(secs);

	// client IP addr (filled in by client)
	ciaddr.s_addr = bootp_pkt[12];
	ciaddr.s_addr <<= 8;
	ciaddr.s_addr |= bootp_pkt[13];
	ciaddr.s_addr <<= 8;
	ciaddr.s_addr |= bootp_pkt[14];
	ciaddr.s_addr <<= 8;
	ciaddr.s_addr |= bootp_pkt[15];
	ciaddr.s_addr = htonl(ciaddr.s_addr);

	// client IP addr (filled in by server)
	yiaddr.s_addr = bootp_pkt[16];
	yiaddr.s_addr <<= 8;
	yiaddr.s_addr |= bootp_pkt[17];
	yiaddr.s_addr <<= 8;
	yiaddr.s_addr |= bootp_pkt[18];
	yiaddr.s_addr <<= 8;
	yiaddr.s_addr |= bootp_pkt[19];
	yiaddr.s_addr = htonl(yiaddr.s_addr);

	// server IP address (filled in by server)
	siaddr.s_addr = bootp_pkt[20];
	siaddr.s_addr <<= 8;
	siaddr.s_addr |= bootp_pkt[21];
	siaddr.s_addr <<= 8;
	siaddr.s_addr |= bootp_pkt[22];
	siaddr.s_addr <<= 8;
	siaddr.s_addr |= bootp_pkt[23];
	siaddr.s_addr = htonl(siaddr.s_addr);

	// gateway IP address
	giaddr.s_addr = bootp_pkt[24];
	giaddr.s_addr <<= 8;
	giaddr.s_addr |= bootp_pkt[25];
	giaddr.s_addr <<= 8;
	giaddr.s_addr |= bootp_pkt[26];
	giaddr.s_addr <<= 8;
	giaddr.s_addr |= bootp_pkt[27];
	giaddr.s_addr = htonl(giaddr.s_addr);

	// client hardware address
	memcpy(chaddr, bootp_pkt+28, 16);

	// the servername
	if(bootp_pkt[107] != 0)
		throw new SysException(0, "PacketStore::ReadPacket",
			"Invalid servername");
	strcpy(sname, (char*)bootp_pkt+44);

	// the file
	if(bootp_pkt[235] != 0)
		throw new SysException(0, "PacketStore::ReadPacket",
			"Invalid filename");
	strcpy(file, (char*)bootp_pkt+108);

	// the magic cookie
	magic_cookie = bootp_pkt[236];
	magic_cookie <<= 8;
	magic_cookie |= bootp_pkt[237];
	magic_cookie <<= 8;
	magic_cookie |= bootp_pkt[238];
	magic_cookie <<= 8;
	magic_cookie |= bootp_pkt[239];
	magic_cookie = htonl(magic_cookie);

	if(bootp_pkt[236] == 0xff)
		return(0);
	
	// read the options
	head=head43=NULL;
	return(ReadOptions(bootp_pkt+240, bootp_pkt_len-240));
}


/******************************************************************************
 * print the packet to stdout                                                 *
 ******************************************************************************/
std::ostream&
operator<< (std::ostream& os, PacketStore &pkt)
{
	int i;
	option *optptr;

	// basic info
	os << "BOOTP type             : " << (int)pkt.op << "\n";
	os << "Hardware type          : " << (int)pkt.htype << "\n";
	os << "Hardware Length        : " << (int)pkt.hlen << "\n";
	os << "Hops                   : " << (int)pkt.hops << "\n";
	os << "Transaction ID         : 0x" << std::hex << ntohl(pkt.xid) <<
	  std::dec <<"\n";
	os << "Seconds                : " << ntohs(pkt.secs) << "\n";
	os << "Client IP (From Client): " << inet_ntoa(pkt.ciaddr) << "\n";
	os << "Client IP (From Server): " << inet_ntoa(pkt.yiaddr) << "\n";
	os << "Server IP              : " << inet_ntoa(pkt.siaddr) << "\n";
	os << "Gateway IP             : " << inet_ntoa(pkt.giaddr) << "\n";
	os << "Client Hardware address: " ;
	
	// client hardware addr
	for(i=0; i<pkt.hlen; i++)
		os << std::hex << (int)pkt.chaddr[i] << std::dec << ".";
	os << "\n";

	// string info
	os << "Server name            : " << pkt.sname << "\n";
	os << "Boot filename          : " << pkt.file << "\n";
	os << "Magic cookie           : 0x" << std::hex <<
	  ntohl(pkt.magic_cookie) << std::dec  << "\n";

	if(pkt.head == NULL)
		return os;

	// print the options
	os << "Options                :\n";

	for(optptr = pkt.head; optptr != NULL; optptr = optptr->next)
	{
		os << "Option major:" << (int)optptr->major_no
		   << " minor: " << (int)optptr->minor_no
		   << ", Length: " << (int)optptr->len << ", Data: ";
		for(i=0; i<optptr->len; i++)
			if((optptr->data[i] >0x20) && (optptr->data[i] < 0x7f))
				os << "[" << (int)optptr->data[i]
				   << "," << std::hex << (int)optptr->data[i] << std::dec
				   << "," << (char)optptr->data[i] << "] ";
			else
				os << "[" << (int)optptr->data[i]
				   << "," << std::hex << (int)optptr->data[i] << std::dec
				   << ", ] ";

		os << "\n";
	}

	if(pkt.head43 == NULL)
		return os;
			
	// option 43
	os << "Options (sub-packed)   :\n";
	for(optptr = pkt.head43; optptr != NULL; optptr = optptr->next)
	{
		os << "Option [43] " << (int)optptr->major_no
		   << " minor: " << (int)optptr->minor_no
		   << ", Length: " << (int)optptr->len << ", Data: ";
		for(i=0; i<optptr->len; i++)
			if((optptr->data[i] >0x20) && (optptr->data[i] < 0x7f))
				os << "[" << (int)optptr->data[i]
				   << "," << std::hex << (int)optptr->data[i] << std::dec 
				   << "," << (char)optptr->data[i] << "] ";
			else
				os << "[" << (int)optptr->data[i]
				   << "," << std::hex << (int)optptr->data[i] << std::dec
				   << ", ] ";

		os << "\n";
	}

	return os;
}


/******************************************************************************
 * ReadOptions - read all the options contained within a packet               *
 ******************************************************************************/
int
PacketStore::ReadOptions(uint8_t *bootp_pkt, int bootp_pkt_len)
{
	// everything will always be null here
	int pos;
	option *optptr = NULL;
	option *optprev = NULL;

	for(pos=0; pos < bootp_pkt_len; pos++)
	{
		if(bootp_pkt[pos] == 0)
			goto ReadOptions_next; // pad

		if(bootp_pkt[pos] == 255)
			break;	// end options

		// lots of sub options
		if(bootp_pkt[pos] == 43)
		{
			ReadOptions43(bootp_pkt+pos+2, bootp_pkt[pos+1]);
			pos += bootp_pkt[pos+1]+1;
			goto ReadOptions_next; // next
		}

		// default
		optptr = new option;
		optptr->major_no = bootp_pkt[pos++];
		optptr->minor_no = 0;
		optptr->len = bootp_pkt[pos++];
		optptr->data = new uint8_t[optptr->len];
		optptr->next = NULL;
		memcpy(optptr->data, bootp_pkt+pos, optptr->len);

		// increment pos, accounting to cyclic increment
		pos += (optptr->len-1);

		if(head != NULL)
		{
			optprev->next = optptr; // assign
			optprev = optprev->next; // advance
		}
		else
			optprev = head = optptr;

		ReadOptions_next:
		pos=pos;
	}

	return(0);
}


/******************************************************************************
 * ReadOptions43 - read all the options contained within a packet             *
 ******************************************************************************/
int
PacketStore::ReadOptions43(uint8_t *bootp_pkt, int bootp_pkt_len)
{
	// everything will always be null here
	int pos;
	option *optptr=NULL;
	option *optprev=NULL;

	for(pos=0; pos < bootp_pkt_len; pos++)
	{
		if(bootp_pkt[pos] == 0)
			goto ReadOptions43_next; // pad

		if(bootp_pkt[pos] == 255)
			break;	// end options

		// lots of sub options
		if(bootp_pkt[pos] == 43)
		{
			std::cerr << "Option 43 detected\n";
			break;
		}

		// default
		optptr = new option;
		optptr->major_no = 43;
		optptr->minor_no = bootp_pkt[pos++];
		optptr->len = bootp_pkt[pos++];
		optptr->data = new uint8_t[optptr->len];
		optptr->next = NULL;
		memcpy(optptr->data, bootp_pkt+pos, optptr->len);

		// increment pos, accounting to cyclic increment
		pos += (optptr->len-1);

		if(head43 != NULL)
		{
			optprev->next = optptr; // assign
			optprev = optprev->next; // advance
		}
		else
			optprev = head43 = optptr;

		ReadOptions43_next:
		pos=pos;
	}

	return(0);
}


/******************************************************************************
 * operator() - get an option from the list                                   *
 ******************************************************************************/
option *
PacketStore::operator()(int major_opt=0, int minor_opt=0)
{
	return(GetOption(major_opt, minor_opt));
}


/******************************************************************************
 * GetOption - get an option fromthe option list                              *
 ******************************************************************************/
option *
PacketStore::GetOption(int major_opt=0, int minor_opt=0)
{
	option *opt = new option;
	option *optptr;

	if(major_opt == 43)
		optptr = head43;
	else
		optptr = head;
	
	for(; optptr != NULL; optptr = optptr->next)
		if((major_opt == optptr->major_no) &&
		   (minor_opt == optptr->minor_no))
			break;
	
	if(optptr == NULL)
		return(NULL);

	opt->major_no = optptr->major_no;
	opt->minor_no = optptr->minor_no;
	opt->len = optptr->len;
	opt->next = NULL;
	opt->data = new uint8_t[opt->len];
	memcpy(opt->data, optptr->data, opt->len);

	return(opt);
}


/******************************************************************************
 * DelOption - delete an option from the list                                 *
 ******************************************************************************/
int
PacketStore::DelOption(int major_opt=0, int minor_opt=0)
{
	option *optptr, *optprev;

	if(major_opt == 43)
		optptr = optprev = head43;
	else
		optptr = optprev = head;

	for(; ((major_opt != optptr->major_no) &&
	       (minor_opt != optptr->minor_no)) ||
	       (optptr != NULL); optptr = optptr->next)
		optprev = optptr;

	if(optptr != NULL) // option found
	{
		// re-arrange the pointers
		if(optprev == optptr) // head
			if(optptr == head43)
				head43 = optptr->next;
			else
				head = optptr->next;
		else
			optprev->next = optptr->next;

		// delete the memory
		delete[] optptr->data;
		delete optptr;
	}
	else
		return(1); // no found

	return(0);
}


/******************************************************************************
 * AddOption - add an option to the list (overwrite)                          *
 ******************************************************************************/
int
PacketStore::AddOption(const option *opt)
{
	option *optptr, *optprev;

	if(opt->major_no == 43)
		optptr = optprev = head43;
	else
		optptr = optprev = head;

	// find the entry or null;
	while(optptr != NULL)
	{
		if((opt->major_no != optptr->major_no) &&
		   (opt->minor_no != optptr->minor_no))
			break;
		optprev = optptr;
		optptr = optptr->next;
	}

	if(optptr == NULL) // new item
	{
		optptr = new option;
		optptr->major_no = opt->major_no;
		optptr->minor_no = opt->minor_no;
		optptr->next = NULL;

		if((43 == opt->major_no) && (NULL == head43))
			head43 = optptr;
		else if(NULL == head)
			head = optptr;
		else
			optprev->next = optptr;
	}
	else  // old item
	{
		delete[] optptr->data;
	}

	optptr->len = opt->len;
	optptr->data = new uint8_t[optptr->len];
	memcpy(optptr->data, opt->data, opt->len);

	return(0);
}


/******************************************************************************
 * htois - host to intel order (short)                                        *
 ******************************************************************************/
uint16_t
PacketStore::htois(uint16_t value)
{
	uint8_t t1, t2;
	uint16_t val;

	// convert to a standard
	val = htons(value);

	// check if we need to go any further
	// since Intel order != network order
	if(val != value)
		return(value);

	// we now have a standard, can you read this Intel?, this is
	// STANDARD!

	// we know that Intel ordering is the opposite to network byte ordering
	// so just swap bytes all the way through
	// out
	t1 = (val & 0x00ff);
	t2 = (val & 0xff00) >> 8;
	// in
	val = (t1<<8);
	val |= t2;
	// we are now in a non-standard
	return val;
}


/******************************************************************************
 * htoil - host to intel order (long)                                         *
 ******************************************************************************/
uint32_t
PacketStore::htoil(uint32_t value)
{
	uint8_t t1, t2, t3, t4;
	uint32_t val;

	// convert to a standard
	val = htonl(value);

	// check if we need to go any further
	// since Intel order != network order
	if(val != value)
		return(value);

	// we now have a standard, can you read this Intel?, this is
	// STANDARD!

	// we know that Intel ordering is the opposite to network byte ordering
	// so just swap bytes all the way through
	// out
	t1 = (val & 0x000000ff);
	t2 = (val & 0x0000ff00) >> 8;
	t3 = (val & 0x00ff0000) >> 16;
	t4 = (val & 0xff000000) >> 24;
	// in
	val = (t1<<24);
	val |= (t2<<16);
	val |= (t3<<8);
	val |= (t4);

	return(val);
}


/******************************************************************************
 * SetAddress - set the address fiel of the packet (from/to)                  *
 ******************************************************************************/
void
PacketStore::SetAddress(const struct sockaddr_in *_address)
{
	memcpy(&address, _address, sizeof(address));
}


/******************************************************************************
 * MakeReply - generate a reply to an incoming request                        *
 ******************************************************************************/
int
PacketStore::MakeReply(PacketStore &request, Options *config,
	struct sockaddr_in *server_addr)
{
	// local vars
	option *opt = NULL;
	option *req_list = NULL;
	int pkttype = 0;
	uint16_t reqCSA;

	// first, check for a valid bootp packet
	if(BOOTREQUEST != request.op)
	{
		logger->Event(LEVEL_INFO, "MakeReply", 1,
		  "Packet is not a bootp request");
		return(-1);
	}

	// valid client system arch?
	reqCSA = request.GetCSA();
	if((uint16_t)-1 == reqCSA)
	{
		logger->Event(LEVEL_INFO, "MakeReply", 1,
		  "packet contains an unknown client system architecture");
		return(-1);
	}
	
	// see if there are any menus for this packet
	if((uint16_t)-1 == config->CheckMenu(reqCSA))
		return(-1);

	// next, check what the packet type was
	opt = request(43,71);
	if(opt == NULL)
	{
		logger->Event(LEVEL_INFO, "MakeReply", 1,
		  "Received proxy DHCP packet");
		pkttype = 1;
	}
	else
	{
		delete[] opt->data;
		delete opt;
		logger->Event(LEVEL_INFO, "MakeReply", 1,
		  "Received PXE request packet");
		pkttype = 2;
	}

	// was it a valid request packet?
	opt = request(53);
	if((NULL == opt) || (DHCPREQUEST != opt->data[0]))
	{
		logger->Event(LEVEL_INFO, "MakeReply", 1,
		  "Packet does not contain a valid DHCP message type");
		return(-1);
	}
	opt->data[0] = DHCPACK;
	opt->len = 1;
	AddOption(opt);
	delete[] opt->data;
	delete opt;

	// is the param request list ok?
	req_list = request(55);
	if(NULL == req_list)
	{
		logger->Event(LEVEL_INFO, "MakeReply", 1,
			"No parameter request list found");
		return(-1);
	}

	// good packet ok, build reply
	memset(file, 0, 128);
	memset(sname, 0, 64);

	// copy over the basic info
	op = BOOTREPLY;
	htype = request.htype;
	hlen = request.hlen;
	hops = request.hops;
	xid = request.xid;
	secs = request.secs;
	magic_cookie = htonl(MAGIC_COOKIE);

	// slightly harder
	memcpy(chaddr, request.chaddr, 16);
	memset(&ciaddr, 0, sizeof(ciaddr));
	memset(&yiaddr, 0, sizeof(yiaddr));
	memcpy(&siaddr, &(server_addr->sin_addr), sizeof(siaddr));
	memset(&giaddr, 0, sizeof(yiaddr));

	// set the destination address
	memcpy(&address, &(request.address), sizeof(address));

	// good, now go through the option list
	for(int i=0; i < req_list->len; i++)
		switch(req_list->data[i])
		{
		case 43:
			if(-1 == GenOpt43(config, reqCSA, request, pkttype))
				return(-1);
			break;
		case 54:
			GenOpt54();
			break;
		case 60:
			GenOpt60();
			break;
		}

	return(0);
}


/******************************************************************************
 * GenOpt43 - generate reply options                                          *
 ******************************************************************************/
int
PacketStore::GenOpt43(Options *config, int reqCSA, PacketStore &request,
 int pkttype)
{
	option opt, *optptr;
	struct sockaddr_in t_addr;
	char *tmpc;
	int arch_id, menu_id;
	uint8_t req_layer;
	int tmpi;

	opt.major_no = 43;

	// Proxy DHCP packet
	if(1 == pkttype)
	{

		// minor 1
		opt.minor_no = 1;
		t_addr.sin_addr.s_addr = htonl(config->GetMTFTPAddr());
		opt.len = sizeof(t_addr.sin_addr.s_addr);
		opt.data = new uint8_t[opt.len];
		memcpy(opt.data, &(t_addr.sin_addr.s_addr), opt.len);
		AddOption(&opt);
		delete [] opt.data;

		// minor 2 - n.b. Intel byte ordering - do intel have no common sense?
		opt.minor_no = 2;
		t_addr.sin_port = htois(config->GetMTFTPcport());
		opt.len = sizeof(t_addr.sin_port);
		opt.data = new uint8_t[opt.len];
		memcpy(opt.data, &(t_addr.sin_port), opt.len);
		AddOption(&opt);
		delete [] opt.data;

		// minor 3 - again Intel's bel^Wbig endian
		opt.minor_no = 3;
		t_addr.sin_port = htois(config->GetMTFTPsport());
		opt.len = sizeof(t_addr.sin_port);
		opt.data = new uint8_t[opt.len];
		memcpy(opt.data, &(t_addr.sin_port), opt.len);
		AddOption(&opt);
		delete [] opt.data;

		// minor 4
		opt.minor_no = 4;
		opt.len = 1;
		opt.data = new uint8_t[opt.len];
		opt.data[0] = MTFTP_OPEN_TIMEOUT;
		AddOption(&opt);

		// minor 5
		opt.minor_no = 5;
		opt.data[0] = MTFTP_REOPEN_TIMEOUT;
		AddOption(&opt);

		// minor 6
		opt.minor_no = 6;
		opt.data[0] = 0;
		// check for disabled broad/multicast
		if(0 == config->UseMulticast())
			opt.data[0] |= DISABLE_MULTICAST;
		if(0 == config->UseBroadcast())
			opt.data[0] |= DISABLE_BROADCAST;
		AddOption(&opt);
		delete [] opt.data;

		// minor 7
		opt.minor_no = 7;
		t_addr.sin_addr.s_addr = htonl(config->GetMulticast());
		opt.len = sizeof(t_addr.sin_addr.s_addr);
		opt.data = new uint8_t[opt.len];
		memcpy(opt.data, &(t_addr.sin_addr.s_addr), opt.len);
		AddOption(&opt);
		delete [] opt.data;

		// minor 9 - will always be ok, as we checked the menus earlier
		optptr = config->MakeBootMenu(reqCSA, &arch_id, &menu_id);
		optptr->major_no = 43;
		optptr->minor_no = 9;
		AddOption(optptr);
		delete [] optptr->data;
		delete optptr;
	
		// minor 10 - the menu
		opt.minor_no = 10;
		tmpc = config->GetMenuPrompt();
		opt.len = strlen(tmpc)+1;
		opt.data = new uint8_t[opt.len];
		opt.data[0] = config->GetMenuTimeout();
		memcpy(opt.data+1, tmpc, opt.len-1);
		AddOption(&opt);
		delete [] opt.data;
		delete [] tmpc;

		// minor 71
		opt.minor_no = 71;
		opt.len = 4;
		opt.data = new uint8_t[4];
		tmpi = htons(PXE_SERVER_TYPE);
		memcpy(opt.data, &tmpi, 2);
		opt.data[2] = 0;
		req_layer = opt.data[3] = config->GetMinLayer(arch_id, menu_id);
		AddOption(&opt);
		delete [] opt.data;

	}
	else
	// PXE layer request
	{
		arch_id = reqCSA;

		// get option 71 fromthe request packet
		optptr = request(43, 71);
		menu_id = optptr->data[0];
		menu_id <<= 8;
		menu_id |= optptr->data[1];
		// don't worry about the third byte, it is for 'credentials'
		req_layer = optptr->data[3];

		// make the reply option
		opt.minor_no = 71;
		opt.len = 4;
		opt.data = new uint8_t[opt.len];
		memcpy(opt.data, optptr->data, 2);
		opt.data[2] = 0;

		delete [] optptr->data;
		delete optptr;
		// which layer to get next/any
		if(config->CheckLayer(menu_id, arch_id, req_layer) == 0)
		{
			opt.data[3] = req_layer;
		}
		else
		{
			delete [] opt.data;
			return(-1);
		}
		AddOption(&opt);
		delete [] opt.data;
	}

	// make filename
	tmpc = config->MakeFilename(menu_id, arch_id, req_layer);
	if(strlen(tmpc) > 127) // gotta think of a better way
		tmpi =  127;
	else
		tmpi = strlen(tmpc);

	memcpy(file, tmpc, tmpi);
	file[tmpi] = 0;
	delete [] tmpc;

	// cool, send the reply
	return(0);
}


/******************************************************************************
 * GenOpt54 - generate reply options                                          *
 ******************************************************************************/
int
PacketStore::GenOpt54(void)
{
	option opt;

	opt.len = sizeof(siaddr.s_addr);

	opt.major_no = 54;
	opt.minor_no = 0;
	opt.data = new uint8_t[opt.len];
	memcpy(opt.data, &(siaddr.s_addr), opt.len);
	AddOption(&opt);
	delete [] opt.data;
	return(0);
}


/******************************************************************************
 * GenOpt60 - generate reply options                                          *
 ******************************************************************************/
int
PacketStore::GenOpt60(void)
{
	option opt;
	opt.major_no=60;
	opt.minor_no=0;
	opt.len = strlen(DHCP_T60);
	opt.data = new uint8_t[opt.len];
	memcpy(opt.data, DHCP_T60, opt.len);
	AddOption(&opt);
	delete [] opt.data;
	return(0);
}


/******************************************************************************
 * GetCSA - get the Client System Archetecture of the packet                  *
 ******************************************************************************/
uint16_t
PacketStore::GetCSA(void)
{
	uint16_t csa1, csa2;
	option *opt;
	char csac[6];
	int csa1_nok=0;
	int csa2_nok=0;

	// should have received two copies of the csa, compare
	csa1 = csa2 = 0;

	// the binary packed copy
	opt = GetOption(93);
	if(NULL == opt)
		csa1_nok = 1;
	else
	{
		memcpy(&csa1, opt->data, 2);
		csa1 = ntohs(csa1);
		delete [] opt->data;
		delete opt;
	}

	// get the ascii packed version
	opt = GetOption(60);
	if(NULL == opt)
		csa2_nok = 1;
	else if(32 != opt->len) {
		csa2_nok = 1;
		delete [] opt->data;
		delete opt;
	} else {
		memcpy(csac, opt->data+15, 5);
		csac[5] = 0;
		csa2 = atoi(csac);
		delete [] opt->data;
		delete opt;
	}

	// check details
	if((0 == csa1_nok) && (0 == csa2_nok ) && (csa1 == csa2))
		return(checkCSA(csa1));
	else if ((1 == csa1_nok) && (0 == csa2_nok ))
		return(checkCSA(csa2));
	else if ((0 == csa1_nok) && (1 == csa2_nok ))
		return(checkCSA(csa1));
	
	return((uint16_t)-1);
}


/******************************************************************************
 * checkCSA - see if we are supporting the type of arch                       *
 ******************************************************************************/
uint16_t
PacketStore::checkCSA(uint16_t reqCSA)
{
	int i;
	for(i=0; (uint16_t)-1 != CSA_types[i].arch_id; i++)
		if(CSA_types[i].arch_id == reqCSA)
			break;

	return(CSA_types[i].arch_id);
}


/******************************************************************************
 * MakeReply - pack the packet into a raw data form                           *
 ******************************************************************************/
bootp_packet_t *
PacketStore::PackPacket(void)
{
	int pktlen, pos, len43;
	option *optptr;
	bootp_packet_t *opt = new bootp_packet_t;

	// first go through the packet and see how big it is
	pktlen = 241; // 255 closing tag
	optptr = head;
	while(NULL != optptr)
	{
		pktlen += optptr->len+2; // <tag>,<len>,<data>
		optptr = optptr->next;
	}
	optptr = head43;
	if(NULL != optptr)
		pktlen += 3; // 43,<len>,...,255
	len43 = 0;
	while(NULL != optptr)
	{
		
		pktlen += optptr->len+2; // <tag>,<len>,<data>
		len43 = pktlen;
		optptr = optptr->next;
	}
	
	// initalise
	opt->data = new uint8_t[pktlen];
	pos = 0;

	// basic packet info
	opt->data[0] = op;
	opt->data[1] = htype;
	opt->data[2] = hlen;
	opt->data[3] = hops;
	memcpy(opt->data+4, &xid, 4);
	memcpy(opt->data+8, &secs, 2);
	memset(opt->data+10, 0, 2);

	// the addresses
	memcpy(opt->data+12, &(ciaddr.s_addr), 4);
	memcpy(opt->data+16, &(yiaddr.s_addr), 4);
	memcpy(opt->data+20, &(siaddr.s_addr), 4);
	memcpy(opt->data+24, &(giaddr.s_addr), 4);

	// hardware addr
	memcpy(opt->data+28, chaddr, 16);
	// server name
	memcpy(opt->data+44, sname, 64);
	// filename
	memcpy(opt->data+108, file, 128);

	// magic cookie
	memcpy(opt->data+236, &magic_cookie, 4);

	// basic options
	pos = 240;
	optptr = head;
	while(NULL != optptr)
	{
		opt->data[pos] = optptr->major_no;
		opt->data[pos+1] = optptr->len;
		memcpy(opt->data+pos+2, optptr->data, optptr->len);
		pos += optptr->len+2;
		optptr = optptr->next;
	}
	// option 43
	optptr = head43;
	if(NULL != optptr)
	{
		opt->data[pos] = 43;
		opt->data[pos+1] = len43;
		pos+=2;
	}
	while(NULL != optptr)
	{
		opt->data[pos] = optptr->minor_no;
		opt->data[pos+1] = optptr->len;
		memcpy(opt->data+pos+2, optptr->data, optptr->len);
		pos += optptr->len+2;
		optptr = optptr->next;
	}
	if(NULL != head43)
	{
		opt->data[pos] = 255;
		pos++;
	}

	// close the packet off
	opt->data[pos] = 255;
	opt->len=pos;

	return(opt);
}