annotate toys/pending/dhcpd.c @ 1005:03f72b57a092

DHCP client and server, from Ashwini Sharma.
author Rob Landley <rob@landley.net>
date Wed, 14 Aug 2013 19:09:33 -0500
parents
children d3f9e55e350a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1005
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* dhcpd.c - DHCP server for dynamic network configuration.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Madhur Verma <mad.flexi@gmail.com>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gamil.com>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * No Standard
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_DHCPD(NEWTOY(dhcpd, ">1P#<0>65535=67fS", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config DHCPD
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "dhcpd"
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default n
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 Usage: dhcpd [-fS] [-P N] [CONFFILE]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 -f Run in foreground
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 -S Log to syslog too
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 -P N Use port N (default 67)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 config DEBUG_DHCP
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 bool "debugging messeges ON/OFF"
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 default n
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 depends on DHCPD
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #define FOR_dhcpd
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include "toys.h"
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 #include "toynet.h"
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 #include <linux/sockios.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 #include <linux/if_ether.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 // Todo: headers not in posix
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #include <netinet/ip.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #include <netinet/udp.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #include <netpacket/packet.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #if CFG_DEBUG_DHCP==1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 # define dbg(fmt, arg...) printf(fmt, ##arg)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 #else
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 # define dbg(fmt, arg...)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 #endif
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 #define flag_get(f,v,d) ((toys.optflags & (f)) ? (v) : (d))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 #define flag_chk(f) ((toys.optflags & (f)) ? 1 : 0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
45
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 #define LOG_SILENT 0x0
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 #define LOG_CONSOLE 0x1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 #define LOG_SYSTEM 0x2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 #define DHCP_MAGIC 0x63825363
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 #define DHCPDISCOVER 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 #define DHCPOFFER 2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 #define DHCPREQUEST 3
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 #define DHCPDECLINE 4
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 #define DHCPACK 5
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 #define DHCPNAK 6
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 #define DHCPRELEASE 7
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 #define DHCPINFORM 8
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 #define DHCP_NUM8 (1<<8)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 #define DHCP_NUM16 (1<<9)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 #define DHCP_NUM32 DHCP_NUM16 | DHCP_NUM8
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 #define DHCP_STRING (1<<10)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 #define DHCP_STRLST (1<<11)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 #define DHCP_IP (1<<12)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 #define DHCP_IPLIST (1<<13)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 #define DHCP_IPPLST (1<<14)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 #define DHCP_STCRTS (1<<15)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 // DHCP option codes (partial list). See RFC 2132 and
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 #define DHCP_OPT_PADDING 0x00
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 #define DHCP_OPT_HOST_NAME DHCP_STRING | 0x0c // either client informs server or server gives name to client
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 #define DHCP_OPT_REQUESTED_IP DHCP_IP | 0x32 // sent by client if specific IP is wanted
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 #define DHCP_OPT_LEASE_TIME DHCP_NUM32 | 0x33
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 #define DHCP_OPT_OPTION_OVERLOAD 0x34
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 #define DHCP_OPT_MESSAGE_TYPE DHCP_NUM8 | 0x35
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 #define DHCP_OPT_SERVER_ID DHCP_IP | 0x36 // by default server's IP
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 #define DHCP_OPT_PARAM_REQ DHCP_STRING | 0x37 // list of options client wants
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 #define DHCP_OPT_END 0xff
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 GLOBALS(
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 long port;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 );
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
85
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 typedef struct __attribute__((packed)) dhcp_msg_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 uint8_t op;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 uint8_t htype;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 uint8_t hlen;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 uint8_t hops;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 uint32_t xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 uint16_t secs;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 uint16_t flags;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 uint32_t ciaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 uint32_t yiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 uint32_t nsiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 uint32_t ngiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 uint8_t chaddr[16];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 uint8_t sname[64];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 uint8_t file[128];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 uint32_t cookie;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 uint8_t options[308];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 } dhcp_msg_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 typedef struct __attribute__((packed)) dhcp_raw_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 struct iphdr iph;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 struct udphdr udph;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 dhcp_msg_t dhcp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 } dhcp_raw_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
110
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 typedef struct static_lease_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 struct static_lease_s *next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 uint32_t nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 int mac[6];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 } static_lease;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
116
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 typedef struct {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 uint32_t expires;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 uint32_t lease_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 uint8_t lease_mac[6];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 char hostname[20];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 uint8_t pad[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 } dyn_lease;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
124
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 typedef struct option_val_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 char *key;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 uint16_t code;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 void *val;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 size_t len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 } option_val_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 typedef struct __attribute__((__may_alias__)) server_config_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 char *interface; // interface to use
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 int ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 uint32_t server_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 uint32_t port;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 uint8_t server_mac[6]; // our MAC address (used only for ARP probing)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 void *options[256]; // list of DHCP options loaded from the config file
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 /* start,end are in host order: we need to compare start <= ip <= end*/
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 uint32_t start_ip; // start address of leases, in host order
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 uint32_t end_ip; // end of leases, in host order
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 uint32_t max_lease_sec; // maximum lease time (host order)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 uint32_t min_lease_sec; // minimum lease time a client can request
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 uint32_t max_leases; // maximum number of leases (including reserved addresses)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 uint32_t auto_time; // how long should dhcpd wait before writing a config file.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 // if this is zero, it will only write one on SIGUSR1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 uint32_t decline_time; // how long an address is reserved if a client returns a
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 // decline message
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 uint32_t conflict_time; // how long an arp conflict offender is leased for
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 uint32_t offer_time; // how long an offered address is reserved
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 uint32_t siaddr_nip; // "next server" bootp option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 char *lease_file;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 char *pidfile;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 char *notify_file; // what to run whenever leases are written
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 char *sname; // bootp server name
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 char *boot_file; // bootp boot file option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 struct static_lease *static_leases; // List of ip/mac pairs to assign static leases
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 } server_config_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
159
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 typedef struct __attribute__((__may_alias__)) server_state_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 uint8_t rqcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 int listensock;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 dhcp_msg_t rcvd_pkt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 uint8_t* rqopt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 dhcp_msg_t send_pkt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 static_lease *sleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 struct arg_list *dleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 } server_state_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 struct config_keyword {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 char *keyword;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 int (*handler)(const char *str, void *var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 void *var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 char *def;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
176
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 static option_val_t options_list[] = {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 {"lease" , DHCP_NUM32 | 0x33, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 {"subnet" , DHCP_IP | 0x01, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 {"broadcast" , DHCP_IP | 0x1c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 {"router" , DHCP_IP | 0x03, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 {"ipttl" , DHCP_NUM8 | 0x17, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 {"mtu" , DHCP_NUM16 | 0x1a, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 {"hostname" , DHCP_STRING | 0x0c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 {"domain" , DHCP_STRING | 0x0f, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 {"search" , DHCP_STRLST | 0x77, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 {"nisdomain" , DHCP_STRING | 0x28, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 {"timezone" , DHCP_NUM32 | 0x02, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 {"tftp" , DHCP_STRING | 0x42, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 {"bootfile" , DHCP_STRING | 0x43, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 {"bootsize" , DHCP_NUM16 | 0x0d, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 {"rootpath" , DHCP_STRING | 0x11, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 {"wpad" , DHCP_STRING | 0xfc, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 {"serverid" , DHCP_IP | 0x36, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 {"message" , DHCP_STRING | 0x38, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 {"vlanid" , DHCP_NUM32 | 0x84, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 {"vlanpriority" , DHCP_NUM32 | 0x85, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 {"dns" , DHCP_IPLIST | 0x06, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 {"wins" , DHCP_IPLIST | 0x2c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 {"nissrv" , DHCP_IPLIST | 0x29, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 {"ntpsrv" , DHCP_IPLIST | 0x2a, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 {"lprsrv" , DHCP_IPLIST | 0x09, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 {"swapsrv" , DHCP_IP | 0x10, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 {"routes" , DHCP_STCRTS | 0x21, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 {"staticroutes" , DHCP_STCRTS | 0x79, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
208
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 struct fd_pair { int rd; int wr; };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 static server_config_t gconfig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 static server_state_t gstate;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 static uint8_t infomode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 static struct fd_pair sigfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 static int constone = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
215
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 // calculate options size.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 static int dhcp_opt_size(uint8_t *optionptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 int i = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 for(;optionptr[i] != 0xff; i++) if(optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 return i;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
223
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 // calculates checksum for dhcp messeges.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 static uint16_t dhcp_checksum(void *addr, int count)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 int32_t sum = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 uint16_t tmp = 0, *source = (uint16_t *)addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
229
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 while (count > 1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 sum += *source++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 count -= 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 if (count > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
235 *(uint8_t*)&tmp = *(uint8_t*)source;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 sum += tmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 return ~sum;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
241
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 // gets information of INTERFACE and updates IFINDEX, MAC and IP
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 static int get_interface(const char *interface, int *ifindex, uint32_t *oip, uint8_t *mac)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 struct ifreq req;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 struct sockaddr_in *ip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
248
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 req.ifr_addr.sa_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 strncpy(req.ifr_name, interface, IFNAMSIZ);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 req.ifr_name[IFNAMSIZ-1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
252
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 xioctl(fd, SIOCGIFFLAGS, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
254
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 if (!(req.ifr_flags & IFF_UP)) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 if (oip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 xioctl(fd, SIOCGIFADDR, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 ip = (struct sockaddr_in*) &req.ifr_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 dbg("IP %s\n", inet_ntoa(ip->sin_addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 *oip = ntohl(ip->sin_addr.s_addr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 if (ifindex) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 xioctl(fd, SIOCGIFINDEX, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 dbg("Adapter index %d\n", req.ifr_ifindex);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 *ifindex = req.ifr_ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 if (mac) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 xioctl(fd, SIOCGIFHWADDR, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 memcpy(mac, req.ifr_hwaddr.sa_data, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
275
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 *logs messeges to syslog or console
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 *opening the log is still left with applet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 *FIXME: move to more relevent lib. probably libc.c
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 static void infomsg(uint8_t infomode, char *s, ...)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 int used;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 char *msg;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 va_list p, t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
286
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 if (infomode == LOG_SILENT) return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 va_start(p, s);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 va_copy(t, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 used = vsnprintf(NULL, 0, s, t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 used++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 va_end(t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
293
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 msg = xmalloc(used);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
295 vsnprintf(msg, used, s, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 va_end(p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
297
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 if (infomode & LOG_SYSTEM) syslog(LOG_INFO, "%s", msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 if (infomode & LOG_CONSOLE) printf("%s\n", msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 free(msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
302
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 * Writes self PID in file PATH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 * FIXME: libc implementation only writes in /var/run
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 * this is more generic as some implemenation may provide
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 * arguments to write in specific file. as dhcpd does.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 static void write_pid(char *path)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 int pidfile = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 if (pidfile > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 char *pidbuf = utoa(getpid());
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 write(pidfile, pidbuf, strlen(pidbuf));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 close(pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
318
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 // Generic signal handler real handling is done in main funcrion.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 static void signal_handler(int sig)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 unsigned char ch = sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 if (write(sigfd.wr, &ch, 1) != 1) dbg("can't send signal\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
325
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 // signal setup for SIGUSR1 SIGTERM
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 static int setup_signal()
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 if (pipe((int *)&sigfd) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 dbg("signal pipe failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 int flags = fcntl(sigfd.wr, F_GETFL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 signal(SIGUSR1, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 signal(SIGTERM, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
341
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 // String STR to UINT32 conversion strored in VAR
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 static int strtou32(const char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 char *endptr = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 int base = 10;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 errno=0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 *((uint32_t*)(var)) = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 if (str[0]=='0' && (str[1]=='x' || str[1]=='X')) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 base = 16;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 str+=2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
352 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 long ret_val = strtol(str, &endptr, base);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 if (errno) infomsg(infomode, "config : Invalid num %s",str);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 else if (endptr && (*endptr!='\0'||endptr == str))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
356 infomsg(infomode, "config : Not a valid num %s",str);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
357 else *((uint32_t*)(var)) = (uint32_t)ret_val;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
360
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 // copy string STR in variable VAR
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 static int strinvar(const char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 char **dest = var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 if (*dest) free(*dest);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
366 *dest = strdup(str);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
367 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
368 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
369
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 // IP String STR to binary data.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 static int striptovar(const char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 in_addr_t addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
374 *((uint32_t*)(var)) = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 if(!str) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 error_msg("config : NULL address string \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
379 if((addr = inet_addr(str)) == -1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 error_msg("config : wrong address %s \n",str );
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 *((uint32_t*)(var)) = (uint32_t)addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
385 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
386
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 // String to dhcp option conversion
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
388 static int strtoopt(const char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 char *option, *valstr, *grp, *tp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 uint32_t optcode = 0, inf = infomode, convtmp, mask, nip, router;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 uint16_t flag = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
393 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
394
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
395 if (!*str) return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 if (!(option = strtok((char*)str, " \t="))) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
397
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
398 infomode = LOG_SILENT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 strtou32(option, (uint32_t*)&optcode);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 infomode = inf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
401
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 if (optcode > 0 && optcode < 256) { // raw option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
403 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 if ((options_list[count].code & 0X00FF) == optcode) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
405 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
409 } else { //string option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
410 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 if (!strncmp(options_list[count].key, option, strlen(options_list[count].key))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 optcode = (options_list[count].code & 0X00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
415 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
417 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 if (count == size) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 infomsg(inf, "config : Obsolete OR Unknown Option : %s", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
422
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 if (!flag || !optcode) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
424
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 if (!(valstr = strtok(NULL, " \t"))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
426 dbg("config : option %s has no value defined.\n", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
427 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 dbg(" value : %-20s : ", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 switch (flag) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
431 case DHCP_NUM32:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 strtou32(valstr, &convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
435 memcpy(options_list[count].val, &convtmp, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
438 options_list[count].len = sizeof(uint16_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
439 options_list[count].val = xmalloc(sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 strtou32(valstr, &convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 memcpy(options_list[count].val, &convtmp, sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
442 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
443 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
444 options_list[count].len = sizeof(uint8_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 options_list[count].val = xmalloc(sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
446 strtou32(valstr, &convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
447 memcpy(options_list[count].val, &convtmp, sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
450 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 striptovar(valstr, options_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
453 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
454 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
455 options_list[count].len = strlen(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
456 options_list[count].val = strdup(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
457 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
458 case DHCP_IPLIST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
459 while(valstr){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
460 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
461 striptovar(valstr, ((uint8_t*)options_list[count].val)+options_list[count].len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
462 options_list[count].len += sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
463 valstr = strtok(NULL," \t");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
464 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
465 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 case DHCP_IPPLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 case DHCP_STCRTS:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
469 /* Option binary format:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 * mask [one byte, 0..32]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
471 * ip [0..4 bytes depending on mask]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
472 * router [4 bytes]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
473 * may be repeated
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
474 * staticroutes 10.0.0.0/8 10.127.0.1, 10.11.12.0/24 10.11.12.1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 grp = strtok(valstr, ",");;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 while(grp){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
478 while(*grp == ' ' || *grp == '\t') grp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
479 tp = strchr(grp, '/');
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 if (!tp) error_exit("wrong formated static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
481 *tp = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
482 mask = strtol(++tp, &tp, 10);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
483 if (striptovar(grp, (uint8_t*)&nip)<0) error_exit("wrong formated static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 if (striptovar(tp, (uint8_t*)&router)<0) error_exit("wrong formated static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
486 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + 1 + mask/8 + 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &mask, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 options_list[count].len += 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &nip, mask/8);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
490 options_list[count].len += mask/8;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
491 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &router, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 options_list[count].len += 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
493 tp = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
494 grp = strtok(NULL, ",");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
498 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
499 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
500
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
501 // Reads Static leases from STR and updates inner structures.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
502 static int get_staticlease(const char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
503 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
504 struct static_lease_s *sltmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
505 char *tkmac, *tkip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
506 int count;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
507
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
508 if (!*str) return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
509
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
510 if (!(tkmac = strtok((char*)str, " \t"))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
511 infomsg(infomode, "config : static lease : mac not found");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
512 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
513 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
514 if (!(tkip = strtok(NULL, " \t"))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
515 infomsg(infomode, "config : static lease : no ip bind to mac %s", tkmac);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
516 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
517 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
518 sltmp = xzalloc(sizeof(struct static_lease_s));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
519 for (count = 0; count < 6; count++, tkmac++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
520 errno = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
521 sltmp->mac[count] = strtol(tkmac, &tkmac, 16);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
522 if (sltmp->mac[count]>255 || sltmp->mac[count]<0 || (*tkmac && *tkmac!=':') || errno) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
523 infomsg(infomode, "config : static lease : mac address wrong format");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
524 free(sltmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
525 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
526 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
527 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
528 striptovar(tkip, &sltmp->nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
529 sltmp->next = gstate.sleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 gstate.sleases = sltmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
531
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
532 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
533 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
534
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
535 static struct config_keyword keywords[] = {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
536 // keyword handler variable address default
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
537 {"start" , striptovar , (void*)&gconfig.start_ip , "192.168.0.20"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
538 {"end" , striptovar , (void*)&gconfig.end_ip , "192.168.0.254"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
539 {"interface" , strinvar , (void*)&gconfig.interface , "eth0"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
540 {"port" , strtou32 , (void*)&gconfig.port , "67"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
541 {"min_lease" , strtou32 , (void*)&gconfig.min_lease_sec, "60"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
542 {"max_leases" , strtou32 , (void*)&gconfig.max_leases , "235"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
543 {"auto_time" , strtou32 , (void*)&gconfig.auto_time , "7200"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
544 {"decline_time" , strtou32 , (void*)&gconfig.decline_time , "3600"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
545 {"conflict_time", strtou32 , (void*)&gconfig.conflict_time, "3600"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
546 {"offer_time" , strtou32 , (void*)&gconfig.offer_time , "60"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
547 {"lease_file" , strinvar , (void*)&gconfig.lease_file , "/var/lib/misc/dhcpd.leases"}, //LEASES_FILE
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
548 {"pidfile" , strinvar , (void*)&gconfig.pidfile , "/var/run/dhcpd.pid"}, //DPID_FILE
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
549 {"siaddr" , striptovar , (void*)&gconfig.siaddr_nip , "0.0.0.0"},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
550 {"option" , strtoopt , (void*)&gconfig.options , ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
551 {"opt" , strtoopt , (void*)&gconfig.options , ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
552 {"notify_file" , strinvar , (void*)&gconfig.notify_file , ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
553 {"sname" , strinvar , (void*)&gconfig.sname , ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
554 {"boot_file" , strinvar , (void*)&gconfig.boot_file , ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
555 {"static_lease" , get_staticlease , (void*)&gconfig.static_leases, ""},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
556 };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
557
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
558 // Parses the server config file and updates the global server config accordingly.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
559 static int parse_server_config(char *config_file, struct config_keyword *confkey)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
560 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
561 FILE *fs = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
562 char *confline_temp = NULL,*confline = NULL, *tk = NULL, *tokens[2] = {NULL, NULL};
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
563 int len, linelen, tcount, count, size = ARRAY_LEN(keywords);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
564
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
565 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
566 if (confkey[count].handler) confkey[count].handler(confkey[count].def, confkey[count].var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
567
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
568 if (!(fs = fopen(config_file, "r"))) perror_msg("%s", config_file);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
569 for (len = 0, linelen = 0; fs;) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
570 len = getline(&confline_temp, (size_t*) &linelen, fs);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
571 confline = confline_temp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
572 if (len <= 0) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
573 for (; *confline == ' '; confline++, len--);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
574 if ((confline[0] == '#') || (confline[0] == '\n')) goto free_conf_continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
575 tk = strchr(confline, '#');
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
576 if (tk) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
577 for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
578 *tk = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
579 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
580 tk = strchr(confline, '\n');
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
581 if (tk) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
582 for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
583 *tk = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
584 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
585 for (tcount=0, tk=strtok(confline, " \t"); tk && (tcount < 2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
586 tcount++, tk=strtok(NULL,(tcount==1)?"":" \t")) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
587 while ((*tk == '\t') || (*tk == ' ')) tk++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
588 tokens[tcount] = xstrdup(tk);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
589 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
590 if (tcount<=1) goto free_tk0_continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
591 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
592 if (!strcmp(confkey[count].keyword,tokens[0])) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
593 dbg("got config : %15s : ", confkey[count].keyword);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
594 if (confkey[count].handler(tokens[1], confkey[count].var) == 0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
595 dbg("%s \n", tokens[1]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
596 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
597 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
598 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
599 if (tokens[1]) { free(tokens[1]); tokens[1] = NULL; }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
600 free_tk0_continue:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
601 if (tokens[0]) { free(tokens[0]); tokens[0] = NULL; }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
602 free_conf_continue:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
603 free(confline_temp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
604 confline_temp = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
605 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
606 if (fs) fclose(fs);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
607 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
608 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
609
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
610 // opens UDP socket for listen
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
611 static int open_listensock(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
612 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
613 struct sockaddr_in addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
614 struct ifreq ifr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
615
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
616 if (gstate.listensock > 0) close(gstate.listensock);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
617
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
618 dbg("Opening listen socket on *:%d %s\n", gconfig.port, gconfig.interface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
619 gstate.listensock = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
620 setsockopt(gstate.listensock, SOL_SOCKET, SO_REUSEADDR, &constone, sizeof(constone));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
621 if (setsockopt(gstate.listensock, SOL_SOCKET, SO_BROADCAST, &constone, sizeof(constone)) == -1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
622 dbg("OPEN : brodcast ioctl failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
623 close(gstate.listensock);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
624 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
625 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
626 memset(&ifr, 0, sizeof(ifr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
627 strncpy(ifr.ifr_name, gconfig.interface, IFNAMSIZ);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
628 ifr.ifr_name[IFNAMSIZ -1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
629 setsockopt(gstate.listensock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
630
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
631 memset(&addr, 0, sizeof(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
632 addr.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
633 addr.sin_port = (flag_chk(FLAG_P))?htons(TT.port):htons(67); //SERVER_PORT
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
634 addr.sin_addr.s_addr = INADDR_ANY ;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
635
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
636 if (bind(gstate.listensock, (struct sockaddr *) &addr, sizeof(addr))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
637 close(gstate.listensock);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
638 perror_exit("bind failed");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
639 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
640 dbg("OPEN : success\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
641 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
642 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
643
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
644 // Sends data through raw socket.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
645 static int send_packet(uint8_t broadcast)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
646 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
647 struct sockaddr_ll dest_sll;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
648 dhcp_raw_t packet;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
649 unsigned padding;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
650 int fd, result = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
651 uint8_t bmacaddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
652
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
653 memset(&packet, 0, sizeof(dhcp_raw_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
654 memcpy(&packet.dhcp, &gstate.send_pkt, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
655
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
656 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
657 dbg("SEND : socket failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
658 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
659 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
660 memset(&dest_sll, 0, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
661 dest_sll.sll_family = AF_PACKET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
662 dest_sll.sll_protocol = htons(ETH_P_IP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
663 dest_sll.sll_ifindex = gconfig.ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
664 dest_sll.sll_halen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
665 memcpy(dest_sll.sll_addr, (broadcast)?bmacaddr:gstate.rcvd_pkt.chaddr , 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
666
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
667 if (bind(fd, (struct sockaddr *) &dest_sll, sizeof(dest_sll)) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
668 dbg("SEND : bind failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
669 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
670 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
671 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
672 padding = 308 - 1 - dhcp_opt_size(gstate.send_pkt.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
673 packet.iph.protocol = IPPROTO_UDP;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
674 packet.iph.saddr = gconfig.server_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
675 packet.iph.daddr = (broadcast || (gstate.rcvd_pkt.ciaddr == 0))?INADDR_BROADCAST:gstate.rcvd_pkt.ciaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
676 packet.udph.source = htons(67);//SERVER_PORT
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
677 packet.udph.dest = htons(68); //CLIENT_PORT
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
678 packet.udph.len = htons(sizeof(dhcp_raw_t) - sizeof(struct iphdr) - padding);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
679 packet.iph.tot_len = packet.udph.len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
680 packet.udph.check = dhcp_checksum(&packet, sizeof(dhcp_raw_t) - padding);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
681 packet.iph.tot_len = htons(sizeof(dhcp_raw_t) - padding);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
682 packet.iph.ihl = sizeof(packet.iph) >> 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
683 packet.iph.version = IPVERSION;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
684 packet.iph.ttl = IPDEFTTL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
685 packet.iph.check = dhcp_checksum(&packet.iph, sizeof(packet.iph));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
686
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
687 result = sendto(fd, &packet, sizeof(dhcp_raw_t) - padding, 0,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
688 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
689
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
690 dbg("sendto %d\n", result);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
691 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
692 if (result < 0) dbg("PACKET send error\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
693 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
694 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
695
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
696 // Reads from UDP socket
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
697 static int read_packet(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
698 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
699 int ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
700
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
701 memset(&gstate.rcvd_pkt, 0, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
702 ret = read(gstate.listensock, &gstate.rcvd_pkt, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
703 if (ret < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
704 dbg("Packet read error, ignoring. \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
705 return ret; // returns -1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
706 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
707 if (gstate.rcvd_pkt.cookie != htonl(DHCP_MAGIC)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
708 dbg("Packet with bad magic, ignoring. \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
709 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
710 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
711 if (gstate.rcvd_pkt.op != 1) { //BOOTPREQUEST
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
712 dbg("Not a BOOT REQUEST ignoring. \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
713 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
714 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
715 if (gstate.rcvd_pkt.hlen != 6) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
716 dbg("hlen != 6 ignoring. \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
717 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
718 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
719 dbg("Received a packet. Size : %d \n", ret);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
720 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
721 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
722
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
723 // Preapres a dhcp packet with defaults and configs
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
724 static uint8_t* prepare_send_pkt(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
725 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
726 memset((void*)&gstate.send_pkt, 0, sizeof(gstate.send_pkt));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
727 gstate.send_pkt.op = 2; //BOOTPREPLY
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
728 gstate.send_pkt.htype = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
729 gstate.send_pkt.hlen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
730 gstate.send_pkt.xid = gstate.rcvd_pkt.xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
731 gstate.send_pkt.cookie = htonl(DHCP_MAGIC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
732 gstate.send_pkt.nsiaddr = gconfig.server_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
733 memcpy(gstate.send_pkt.chaddr, gstate.rcvd_pkt.chaddr, 16);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
734 gstate.send_pkt.options[0] = DHCP_OPT_END;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
735 return gstate.send_pkt.options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
736 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
737
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
738 // Sets a option value in dhcp packet's option field
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
739 static uint8_t* set_optval(uint8_t *optptr, uint16_t opt, void *var, size_t len)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
740 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
741 while (*optptr != DHCP_OPT_END) optptr++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
742 *optptr++ = (uint8_t)(opt & 0x00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
743 *optptr++ = (uint8_t) len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
744 memcpy(optptr, var, len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
745 optptr += len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
746 *optptr = DHCP_OPT_END;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
747 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
748 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
749
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
750 // Gets a option value from dhcp packet's option field
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
751 static uint8_t* get_optval(uint8_t *optptr, uint16_t opt, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
752 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
753 size_t len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
754 uint8_t overloaded = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
755
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
756 while (1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
757 while (*optptr == DHCP_OPT_PADDING) optptr++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
758 if ((*optptr & 0x00FF) == DHCP_OPT_END) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
759 if ((*optptr & 0x00FF) == DHCP_OPT_OPTION_OVERLOAD) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
760 overloaded = optptr[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
761 optptr += optptr[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
762 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
763 len = optptr[1];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
764 if (*optptr == (opt & 0x00FF))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
765 switch (opt & 0xFF00) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
766 case DHCP_NUM32: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
767 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
768 memcpy(var, optptr+2, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
769 optptr += len + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
770 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
771 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
772 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
773 memcpy(var, optptr+2, sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
774 optptr += len + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
775 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
776 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
777 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
778 memcpy(var, optptr+2, sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
779 optptr += len + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
780 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
781 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
782 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
783 var = xstrndup((char*) optptr, len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
784 optptr += len + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
785 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
786 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
787 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
788 optptr += len + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
789 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
790 if ((overloaded == 1) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.file, opt, var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
791 if ((overloaded == 2) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.sname, opt, var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
792 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
793 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
794
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
795 // Retrives Requested Parameter list from dhcp req packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
796 static uint8_t get_reqparam(uint8_t **list)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
797 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
798 uint8_t len, *optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
799 if(*list) free(*list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
800 for (optptr = gstate.rcvd_pkt.options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
801 *optptr && *optptr!=((DHCP_OPT_PARAM_REQ) & 0x00FF); optptr+=optptr[1]+2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
802 len = *++optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
803 *list = xzalloc(len+1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
804 memcpy(*list, ++optptr, len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
805 return len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
806 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
807
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
808 // Sets values of req param in dhcp offer packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
809 static uint8_t* set_reqparam(uint8_t *optptr, uint8_t *list)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
810 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
811 uint8_t reqcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
812 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
813
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
814 while (*list) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
815 reqcode = *list++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
816 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
817 if ((options_list[count].code & 0X00FF)==reqcode) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
818 if (!(options_list[count].len) || !(options_list[count].val)) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
819 for (; *optptr && *optptr!=DHCP_OPT_END; optptr+=optptr[1]+2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
820 *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
821 *optptr++ = (uint8_t) options_list[count].len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
822 memcpy(optptr, options_list[count].val, options_list[count].len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
823 optptr += options_list[count].len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
824 *optptr = DHCP_OPT_END;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
825 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
826 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
827 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
828 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
829 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
830 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
831
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
832 static void run_notify(char **argv)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
833 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
834 struct stat sts;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
835 volatile int error = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
836 pid_t pid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
837
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
838 if (stat(argv[0], &sts) == -1 && errno == ENOENT) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
839 infomsg(infomode, "notify file: %s : not exist.", argv[0]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
840 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
841 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
842 fflush(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
843
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
844 pid = vfork();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
845 if (pid < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
846 dbg("Fork failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
847 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
848 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
849 if (!pid) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
850 execvp(argv[0], argv);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
851 error = errno;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
852 _exit(111);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
853 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
854 if (error) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
855 waitpid(pid, NULL, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
856 errno = error;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
857 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
858 dbg("script complete.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
859 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
860
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
861 static int write_leasefile(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
862 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
863 int fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
864 uint32_t curr, tmp_time;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
865 int64_t timestamp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
866 struct arg_list *listdls = gstate.dleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
867 dyn_lease *dls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
868
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
869 if ((fd = open(gconfig.lease_file, O_WRONLY | O_CREAT | O_TRUNC)) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
870 perror_msg("can't open %s ", gconfig.lease_file);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
871 return fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
872 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
873
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
874 curr = timestamp = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
875 timestamp = SWAP_BE64(timestamp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
876 writeall(fd, &timestamp, sizeof(timestamp));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
877
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
878 while (listdls) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
879 dls = (dyn_lease*)listdls->arg;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
880 tmp_time = dls->expires;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
881 dls->expires -= curr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
882 if ((int32_t) dls->expires < 0) goto skip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
883 dls->expires = htonl(dls->expires);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
884 writeall(fd, dls, sizeof(dyn_lease));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
885 skip:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
886 dls->expires = tmp_time;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
887 listdls = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
888 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
889 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
890 if (gconfig.notify_file) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
891 char *argv[3];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
892 argv[0] = gconfig.notify_file;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
893 argv[1] = gconfig.lease_file;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
894 argv[2] = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
895 run_notify(argv);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
896 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
897 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
898 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
899
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
900 // Update max lease time from options.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
901 static void set_maxlease(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
902 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
903 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
904 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
905 if (options_list[count].val && options_list[count].code == (DHCP_OPT_LEASE_TIME)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
906 gconfig.max_lease_sec = *((uint32_t*)options_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
907 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
908 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
909 if (!gconfig.max_lease_sec) gconfig.max_lease_sec = (60*60*24*10);// DEFAULT_LEASE_TIME;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
910 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
911
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
912 // Returns lease time for client.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
913 static uint32_t get_lease(uint32_t req_exp)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
914 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
915 uint32_t now = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
916 req_exp = req_exp - now;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
917 if ((req_exp <= 0) || (req_exp > gconfig.max_lease_sec))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
918 return gconfig.max_lease_sec;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
919
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
920 if (req_exp < gconfig.min_lease_sec)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
921 return gconfig.min_lease_sec;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
922
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
923 return req_exp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
924 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
925
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
926 // Verify ip NIP in current leases ( assigned or not)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
927 static int verifyip_in_lease(uint32_t nip, uint8_t mac[6])
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
928 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
929 static_lease *sls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
930 struct arg_list *listdls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
931
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
932 for (listdls = gstate.dleases; listdls; listdls = listdls->next) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
933 if (((dyn_lease*) listdls->arg)->lease_nip == nip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
934 if (((int32_t)(((dyn_lease*) listdls->arg)->expires) - time(NULL)) < 0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
935 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
936 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
937 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
938 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
939 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
940 for (sls = gstate.sleases; sls; sls = sls->next)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
941 if (sls->nip == nip) return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
942
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
943 if ((ntohl(nip) < gconfig.start_ip) || (ntohl(nip) > gconfig.end_ip))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
944 return -3;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
945
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
946 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
947 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
948
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
949 // add ip assigned_nip to dynamic lease.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
950 static int addip_to_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname, uint8_t update)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
951 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
952 dyn_lease *dls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
953 struct arg_list *listdls = gstate.dleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
954 uint32_t now = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
955
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
956 while (listdls) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
957 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
958 if (update) *req_exp = get_lease(*req_exp + ((dyn_lease*) listdls->arg)->expires);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
959 ((dyn_lease*) listdls->arg)->expires = *req_exp + now;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
960 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
961 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
962 listdls = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
963 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
964
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
965 dls = xzalloc(sizeof(dyn_lease));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
966 memcpy(dls->lease_mac, mac, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
967 dls->lease_nip = assigned_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
968 if (hostname) memcpy(dls->hostname, hostname, 20);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
969
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
970 if (update) *req_exp = get_lease(*req_exp + now);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
971 dls->expires = *req_exp + now;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
972
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
973 listdls = xzalloc(sizeof(struct arg_list));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
974 listdls->next = gstate.dleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
975 listdls->arg = (char*)dls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
976 gstate.dleases = listdls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
977
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
978 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
979 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
980
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
981 // delete ip assigned_nip from dynamic lease.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
982 static int delip_from_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t del_time)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
983 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
984 struct arg_list *listdls = gstate.dleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
985
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
986 while (listdls) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
987 if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
988 ((dyn_lease*) listdls->arg)->expires = del_time + time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
989 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
990 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
991 listdls = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
992 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
993 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
994 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
995
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
996 // returns a IP from static, dynamic leases or free ip pool, 0 otherwise.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
997 static uint32_t getip_from_pool(uint32_t req_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
998 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
999 uint32_t nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1000 static_lease *sls = gstate.sleases;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1001 struct arg_list *listdls = gstate.dleases, *tmp = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1002
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1003 if (req_nip && (!verifyip_in_lease(req_nip, mac))) nip = req_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1004
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1005 if (!nip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1006 while (listdls) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1007 if (!memcmp(((dyn_lease*)listdls->arg)->lease_mac, mac, 6)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1008 nip = ((dyn_lease*)listdls->arg)->lease_nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1009 if (tmp) tmp->next = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1010 else gstate.dleases = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1011 free(listdls->arg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1012 free(listdls);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1013 if (verifyip_in_lease(nip, mac) < 0) nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1014 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1015 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1016 tmp = listdls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1017 listdls = listdls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1018 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1019 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1020 if (!nip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1021 while (sls) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1022 if (memcmp(sls->mac, mac, 6) == 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1023 nip = sls->nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1024 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1025 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1026 sls = sls->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1027 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1028 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1029 if (!nip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1030 for (nip = htonl(gconfig.start_ip); ntohl(nip) <= gconfig.end_ip; ) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1031 if (!verifyip_in_lease(nip, mac)) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1032 nip = ntohl(nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1033 nip = htonl(++nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1034 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1035 if (ntohl(nip) > gconfig.end_ip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1036 nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1037 infomsg(infomode, "can't find free IP in IP Pool.");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1038 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1039 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1040 if (nip) addip_to_lease(nip, mac, req_exp, hostname, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1041 return nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1042 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1043
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1044 static int read_leasefile(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1045 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1046 uint32_t passed, ip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1047 int32_t tmp_time;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1048 int64_t timestamp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1049 dyn_lease *dls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1050 int ret = -1, fd = open(gconfig.lease_file, O_RDONLY);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1051
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1052 if (fd < 0) return fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1053 dls = xzalloc(sizeof(dyn_lease));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1054
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1055 if (read(fd, &timestamp, sizeof(timestamp)) != sizeof(timestamp)) goto error_exit;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1056
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1057 timestamp = SWAP_BE64(timestamp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1058 passed = time(NULL) - timestamp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1059 if ((uint64_t)passed > 12 * 60 * 60) goto error_exit;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1060
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1061 while (read(fd, dls, sizeof(dyn_lease)) == sizeof(dyn_lease)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1062 ip = ntohl(dls->lease_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1063 if (ip >= gconfig.start_ip && ip <= gconfig.end_ip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1064 tmp_time = ntohl(dls->expires) - passed;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1065 if (tmp_time < 0) continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1066 addip_to_lease(dls->lease_nip, dls->lease_mac, (uint32_t*)&tmp_time, dls->hostname, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1067 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1068 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1069 ret = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1070 error_exit:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1071 free(dls);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1072 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1073 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1074 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1075
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1076 void dhcpd_main(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1077 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1078 struct timeval tv;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1079 int retval;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1080 uint8_t *optptr, msgtype = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1081 uint32_t waited = 0, serverid = 0, requested_nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1082 uint32_t reqested_lease = 0, ip_pool_size = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1083 char *hstname = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1084 fd_set rfds;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1085
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1086 infomode = LOG_CONSOLE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1087 if (!(flag_chk(FLAG_f))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1088 daemonize();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1089 infomode = LOG_SILENT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1090 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1091 if (flag_chk(FLAG_S)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1092 openlog("UDHCPD :", LOG_PID, LOG_DAEMON);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1093 infomode |= LOG_SYSTEM;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1094 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1095 setlinebuf(stdout);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1096 parse_server_config((toys.optc==1)?toys.optargs[0]:"/etc/dhcpd.conf", keywords); //DHCPD_CONF_FILE
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1097 infomsg(infomode, "toybox dhcpd started");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1098 gconfig.start_ip = ntohl(gconfig.start_ip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1099 gconfig.end_ip = ntohl(gconfig.end_ip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1100 ip_pool_size = gconfig.end_ip - gconfig.start_ip + 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1101 if (gconfig.max_leases > ip_pool_size) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1102 error_msg("max_leases=%u is too big, setting to %u", (unsigned) gconfig.max_leases, ip_pool_size);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1103 gconfig.max_leases = ip_pool_size;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1104 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1105 write_pid(gconfig.pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1106 set_maxlease();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1107 read_leasefile();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1108
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1109 if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1110 gconfig.server_mac)<0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1111 perror_exit("Failed to get interface %s", gconfig.interface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1112 gconfig.server_nip = htonl(gconfig.server_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1113
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1114 setup_signal();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1115 open_listensock();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1116 fcntl(gstate.listensock, F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1117
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1118 for (;;) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1119 uint32_t timestmp = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1120 FD_ZERO(&rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1121 FD_SET(gstate.listensock, &rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1122 FD_SET(sigfd.rd, &rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1123 tv.tv_sec = gconfig.auto_time - waited;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1124 tv.tv_usec = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1125 retval = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1126 serverid = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1127 msgtype = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1128
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1129 int maxfd = (sigfd.rd > gstate.listensock)? sigfd.rd : gstate.listensock;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1130 dbg("select waiting ....\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1131 retval = select(maxfd + 1, &rfds, NULL, NULL, (gconfig.auto_time?&tv:NULL));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1132 if (retval < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1133 if (errno == EINTR) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1134 waited += (unsigned) time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1135 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1136 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1137 dbg("Error in select wait again...\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1138 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1139 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1140 if (!retval) { // Timed out
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1141 dbg("select wait Timed Out...\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1142 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1143 write_leasefile();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1144 if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip, gconfig.server_mac)<0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1145 perror_exit("Interface lost %s\n", gconfig.interface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1146 gconfig.server_nip = htonl(gconfig.server_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1147 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1148 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1149 if (FD_ISSET(sigfd.rd, &rfds)) { // Some Activity on RDFDs : is signal
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1150 unsigned char sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1151 if (read(sigfd.rd, &sig, 1) != 1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1152 dbg("signal read failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1153 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1154 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1155 switch (sig) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1156 case SIGUSR1:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1157 infomsg(infomode, "Received SIGUSR1");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1158 write_leasefile();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1159 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1160 case SIGTERM:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1161 infomsg(infomode, "Received SIGTERM");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1162 write_leasefile();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1163 unlink(gconfig.pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1164 exit(0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1165 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1166 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1167 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1168 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1169 if (FD_ISSET(gstate.listensock, &rfds)) { // Some Activity on RDFDs : is socket
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1170 dbg("select listen sock read\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1171 if (read_packet() < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1172 open_listensock();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1173 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1174 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1175 waited += time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1176 get_optval((uint8_t*)&gstate.rcvd_pkt.options, DHCP_OPT_MESSAGE_TYPE, &gstate.rqcode);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1177 if (gstate.rqcode == 0 || gstate.rqcode < DHCPDISCOVER
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1178 || gstate.rqcode > DHCPINFORM) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1179 dbg("no or bad message type option, ignoring packet.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1180 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1181 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1182 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1183 if (serverid && (serverid != gconfig.server_nip)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1184 dbg("server ID doesn't match, ignoring packet.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1185 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1186 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1187 switch (gstate.rqcode) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1188 case DHCPDISCOVER:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1189 msgtype = DHCPOFFER;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1190 dbg("Message Type : DHCPDISCOVER\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1191 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1192 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1193 reqested_lease = gconfig.offer_time;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1194 get_reqparam(&gstate.rqopt);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1195 optptr = prepare_send_pkt();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1196 gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1197 if(!gstate.send_pkt.yiaddr){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1198 msgtype = DHCPNAK;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1199 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1200 send_packet(1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1201 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1202 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1203 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1204 reqested_lease = htonl(get_lease(reqested_lease + time(NULL)));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1205 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1206 optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1207 optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1208 optptr = set_reqparam(optptr, gstate.rqopt);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1209 send_packet(1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1210 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1211 case DHCPREQUEST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1212 msgtype = DHCPACK;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1213 dbg("Message Type : DHCPREQUEST\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1214 optptr = prepare_send_pkt();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1215 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1216 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1217 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1218 gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1219 if (!serverid) reqested_lease = gconfig.max_lease_sec;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1220 if (!gstate.send_pkt.yiaddr) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1221 msgtype = DHCPNAK;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1222 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1223 send_packet(1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1224 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1225 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1226 optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1227 optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1228 reqested_lease = htonl(reqested_lease);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1229 optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1230 send_packet(1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1231 write_leasefile();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1232 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1233 case DHCPDECLINE:// FALL THROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1234 case DHCPRELEASE:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1235 dbg("Message Type : DHCPDECLINE or DHCPRELEASE \n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1236 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1237 if (serverid != gconfig.server_nip) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1238 get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1239 delip_from_lease(requested_nip, gstate.rcvd_pkt.chaddr, (gstate.rqcode==DHCPRELEASE)?0:gconfig.decline_time);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1240 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1241 default:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1242 dbg("Message Type : %u\n", gstate.rqcode);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1243 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1244 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1245 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1246 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1247 }