1 #include <headers.h>
2 #include <datatypes.h>
3 #include <options.h>
4 #include <display.h>
5 #include <leases.h>
6 #include <parser.h>
7 
8 #define MYPORT  67
9 DHCPLIST *list;
10 /*DHCPLIST *leased_list;*/
11 
12 int main( int argc, char *argv[] )
13 {
14 #ifdef __MINGW32__
15   WSADATA wsaData;
16   int nCode;
17 #endif
18   int sockfd;
19   struct sockaddr_in my_addr;
20   struct sockaddr_in their_addr;
21   int addr_len, numbytes;
22   DHCPMESSAGE dhcpm;
23   DHCPOPTIONS dhcpo;
24 
25 #ifdef __MINGW32__
26   if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0)
27     {
28       perror("WSAStartup");
29       return 0;
30     }
31 #endif
32 
33   if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
34     perror("socket");
35     exit(1);
36   }
37 
38   init_leases_list();
39 
40   my_addr.sin_family = AF_INET;
41   my_addr.sin_port = htons(MYPORT);
42   my_addr.sin_addr.s_addr = INADDR_ANY;
43   memset(&(my_addr.sin_zero), '\0', 8);
44 
45   if (bind(sockfd, (struct sockaddr *)&my_addr,
46 	   sizeof(struct sockaddr)) == -1) {
47     perror("bind");
48     exit(1);
49   }
50 
51   addr_len = sizeof(struct sockaddr);
52   while((numbytes=recvfrom(sockfd,&dhcpm, sizeof( DHCPMESSAGE ), 0,
53 			 (struct sockaddr *)&their_addr, &addr_len)) != -1) {
54     /* Parse DHCP */
55     display_dhcp_packet( &dhcpm, &dhcpo );
56     if( parse_dhcp_options( &dhcpm, &dhcpo ) < 0 )
57       continue;
58     if( display_dhcp_packet( &dhcpm, &dhcpo ) < 0 )
59       continue;
60     if( process_dhcp_packet( &dhcpm, &dhcpo ) < 0 )
61       continue;
62   }
63 
64   close(sockfd);
65 
66 #ifdef __MINGW32__
67   WSACleanup();
68 #endif
69 
70   return 0;
71 
72 }
73