annotate toys/pending/dhcp.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 /* dhcp.c - DHCP client 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 2012 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@gmail.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 * Not in SUSv4.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0T#<0t#<0s:p:i:SBRCaovqnbf", 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 DHCP
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "dhcp"
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: dhcp [-fbnqvoCRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 [-H HOSTNAME] [-V VENDOR] [-x OPT:VAL] [-O OPT]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Configure network dynamicaly using DHCP.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 -i Interface to use (default eth0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -p Create pidfile
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -s Run PROG at DHCP events (default /usr/share/dhcp/default.script)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -B Request broadcast replies
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 -t Send up to N discover packets
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 -T Pause between packets (default 3 seconds)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -A Wait N seconds after failure (default 20)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 -f Run in foreground
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 -b Background if lease is not obtained
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 -n Exit if lease is not obtained
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 -q Exit after obtaining lease
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 -R Release IP on exit
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 -S Log to syslog too
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 -a Use arping to validate offered address
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 -O Request option OPT from server (cumulative)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 -o Don't request any options (unless -O is given)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 -r Request this IP address
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 -x OPT:VAL Include option OPT in sent packets (cumulative)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 -F Ask server to update DNS mapping for NAME
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 -H Send NAME as client hostname (default none)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 -V VENDOR Vendor identifier (default 'toybox VERSION')
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 -C Don't send MAC as client identifier
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 -v Verbose
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 Signals:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 USR1 Renew current lease
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 USR2 Release current lease
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 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 #define FOR_dhcp
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 #include "toys.h"
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 #include "toynet.h"
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 // TODO: headers not in posix:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 #include <netinet/ip.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 #include <netinet/udp.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 #include <netpacket/packet.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 #include <linux/filter.h> //FIXME: linux specific. fix for other OS ports
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 #include <linux/if_ether.h>
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 GLOBALS(
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 char *iface;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 char *pidfile;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 char *script;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 long retries;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 long timeout;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 long tryagain;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 struct arg_list *req_opt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 char *req_ip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 struct arg_list *pkt_opt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 char *fdn_name;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 char *hostname;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 char *vendor_cls;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 )
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 #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
76 #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
77
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 #define STATE_INIT 0
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 #define STATE_REQUESTING 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 #define STATE_BOUND 2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 #define STATE_RENEWING 3
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 #define STATE_REBINDING 4
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 #define STATE_RENEW_REQUESTED 5
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 #define STATE_RELEASED 6
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 #define BOOTP_BROADCAST 0x8000
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 #define DHCP_MAGIC 0x63825363
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 #define DHCP_REQUEST 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 #define DHCP_REPLY 2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 #define DHCP_HTYPE_ETHERNET 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 #define DHCPC_SERVER_PORT 67
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 #define DHCPC_CLIENT_PORT 68
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 #define DHCPDISCOVER 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 #define DHCPOFFER 2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 #define DHCPREQUEST 3
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 #define DHCPACK 5
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 #define DHCPNAK 6
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 #define DHCPRELEASE 7
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 #define DHCP_OPTION_PADDING 0x00
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 #define DHCP_OPTION_SUBNET_MASK 0x01
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 #define DHCP_OPTION_ROUTER 0x03
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 #define DHCP_OPTION_DNS_SERVER 0x06
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 #define DHCP_OPTION_HOST_NAME 0x0c
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 #define DHCP_OPTION_BROADCAST 0x1c
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 #define DHCP_OPTION_REQ_IPADDR 0x32
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 #define DHCP_OPTION_LEASE_TIME 0x33
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 #define DHCP_OPTION_OVERLOAD 0x34
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 #define DHCP_OPTION_MSG_TYPE 0x35
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 #define DHCP_OPTION_SERVER_ID 0x36
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 #define DHCP_OPTION_REQ_LIST 0x37
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 #define DHCP_OPTION_MAX_SIZE 0x39
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 #define DHCP_OPTION_CLIENTID 0x3D
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 #define DHCP_OPTION_VENDOR 0x3C
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 #define DHCP_OPTION_FQDN 0x51
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 #define DHCP_OPTION_END 0xFF
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
120
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 #define DHCP_NUM8 (1<<8)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 #define DHCP_NUM16 (1<<9)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 #define DHCP_NUM32 DHCP_NUM16 | DHCP_NUM8
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 #define DHCP_STRING (1<<10)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 #define DHCP_STRLST (1<<11)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 #define DHCP_IP (1<<12)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 #define DHCP_IPLIST (1<<13)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 #define DHCP_IPPLST (1<<14)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 #define DHCP_STCRTS (1<<15)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
130
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 #define LOG_SILENT 0x0
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 #define LOG_CONSOLE 0x1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 #define LOG_SYSTEM 0x2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
134
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 #define MODE_OFF 0
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 #define MODE_RAW 1
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 #define MODE_APP 2
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
138
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 static void (*dbg)(char *format, ...);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 static void dummy(char *format, ...){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
143
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 typedef struct dhcpc_result_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 struct in_addr serverid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 struct in_addr ipaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 struct in_addr netmask;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 struct in_addr dnsaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 struct in_addr default_router;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 uint32_t lease_time;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 } dhcpc_result_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 typedef struct __attribute__((packed)) dhcp_msg_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 uint8_t op;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 uint8_t htype;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 uint8_t hlen;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 uint8_t hops;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 uint32_t xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 uint16_t secs;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 uint16_t flags;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 uint32_t ciaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 uint32_t yiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 uint32_t nsiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 uint32_t ngiaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 uint8_t chaddr[16];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 uint8_t sname[64];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 uint8_t file[128];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 uint32_t cookie;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 uint8_t options[308];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 } dhcp_msg_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 typedef struct __attribute__((packed)) dhcp_raw_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 struct iphdr iph;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 struct udphdr udph;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 dhcp_msg_t dhcp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 } dhcp_raw_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 typedef struct dhcpc_state_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 uint8_t macaddr[6];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 char *iface;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 int ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 int sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 int status;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 int mode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 uint32_t mask;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 struct in_addr ipaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 struct in_addr serverid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 dhcp_msg_t pdhcp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 } dhcpc_state_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
190
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 typedef struct option_val_s {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 char *key;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 uint16_t code;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 void *val;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 size_t len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 } option_val_t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
197
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 struct fd_pair { int rd; int wr; };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 static uint32_t xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 static dhcpc_state_t *state;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 static struct fd_pair sigfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 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
203 int set = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 uint8_t infomode = LOG_CONSOLE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 uint8_t raw_opt[29];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 int raw_optcount = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 struct arg_list *x_opt;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 in_addr_t server = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
209
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 static option_val_t *msgopt_list = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 static option_val_t options_list[] = {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 {"lease" , DHCP_NUM32 | 0x33, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 {"subnet" , DHCP_IP | 0x01, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 {"broadcast" , DHCP_IP | 0x1c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 {"router" , DHCP_IP | 0x03, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 {"ipttl" , DHCP_NUM8 | 0x17, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 {"mtu" , DHCP_NUM16 | 0x1a, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 {"hostname" , DHCP_STRING | 0x0c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 {"domain" , DHCP_STRING | 0x0f, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 {"search" , DHCP_STRLST | 0x77, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 {"nisdomain" , DHCP_STRING | 0x28, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 {"timezone" , DHCP_NUM32 | 0x02, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 {"tftp" , DHCP_STRING | 0x42, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 {"bootfile" , DHCP_STRING | 0x43, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 {"bootsize" , DHCP_NUM16 | 0x0d, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 {"rootpath" , DHCP_STRING | 0x11, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 {"wpad" , DHCP_STRING | 0xfc, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 {"serverid" , DHCP_IP | 0x36, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 {"message" , DHCP_STRING | 0x38, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 {"vlanid" , DHCP_NUM32 | 0x84, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 {"vlanpriority" , DHCP_NUM32 | 0x85, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 {"dns" , DHCP_IPLIST | 0x06, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 {"wins" , DHCP_IPLIST | 0x2c, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 {"nissrv" , DHCP_IPLIST | 0x29, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
235 {"ntpsrv" , DHCP_IPLIST | 0x2a, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 {"lprsrv" , DHCP_IPLIST | 0x09, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 {"swapsrv" , DHCP_IP | 0x10, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 {"routes" , DHCP_STCRTS | 0x21, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 {"staticroutes" , DHCP_STCRTS | 0x79, NULL, 0},
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 static struct sock_filter filter_instr[] = {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 BPF_STMT(BPF_RET|BPF_K, 0xffffffff), BPF_STMT(BPF_RET|BPF_K, 0),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 };
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 static struct sock_fprog filter_prog = {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 .len = ARRAY_LEN(filter_instr),
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 .filter = (struct sock_filter *) filter_instr,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 };
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
257
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 // calculate options size.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 static int dhcp_opt_size(uint8_t *optionptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 int i = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 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
263 return i;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
265
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 // calculates checksum for dhcp messages.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 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
268 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 int32_t sum = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 uint16_t tmp = 0, *source = (uint16_t *)addr;
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 while (count > 1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 sum += *source++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 count -= 2;
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 if (count > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 *(uint8_t*)&tmp = *(uint8_t*)source;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 sum += tmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 return ~sum;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 // 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
285 static int get_interface( 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
286 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 struct ifreq req;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 struct sockaddr_in *ip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 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
290
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 req.ifr_addr.sa_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 strncpy(req.ifr_name, interface, IFNAMSIZ);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
293 req.ifr_name[IFNAMSIZ-1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
294
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
295 xioctl(fd, SIOCGIFFLAGS, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 if (!(req.ifr_flags & IFF_UP)) return -1;
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 (oip) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 xioctl(fd, SIOCGIFADDR, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 ip = (struct sockaddr_in*) &req.ifr_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 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
302 *oip = ntohl(ip->sin_addr.s_addr);
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 if (ifindex) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 xioctl(fd, SIOCGIFINDEX, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 dbg("Adapter index %d\n", req.ifr_ifindex);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 *ifindex = req.ifr_ifindex;
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 if (mac) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 xioctl(fd, SIOCGIFHWADDR, &req);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 memcpy(mac, req.ifr_hwaddr.sa_data, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 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
313 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 return 0;
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 static int dhcp_daemon(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 int fd = open("/dev/null", O_RDWR);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 pid_t pid = fork();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
323
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 if (pid < 0) perror_exit("DAEMON: failed to fork");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 if (pid) exit(EXIT_SUCCESS);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
326
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 setsid();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 dup2(fd, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 dup2(fd, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 dup2(fd, 2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 if (fd > 2) xclose(fd);
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 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
335
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 *logs messeges to syslog or console
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 *opening the log is still left with applet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 *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
340 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 static void infomsg(uint8_t infomode, char *s, ...)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 int used;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 char *msg;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 va_list p, t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
346
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 if (infomode == LOG_SILENT) return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 va_start(p, s);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 va_copy(t, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 used = vsnprintf(NULL, 0, s, t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 used++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
352 va_end(t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
353
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 msg = xmalloc(used);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 vsnprintf(msg, used, s, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
356 va_end(p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
357
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 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
359 if (infomode & LOG_CONSOLE) printf("%s\n", msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
360 free(msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
362
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 * Writes self PID in file PATH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 * FIXME: libc implementation only writes in /var/run
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
366 * 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
367 * 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
368 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 static void write_pid(char *path)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 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
372 if (pidfile > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 char *pidbuf = utoa(getpid());
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
374 write(pidfile, pidbuf, strlen(pidbuf));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 close(pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 }
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 // String STR to UINT32 conversion strored in VAR
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 static long strtou32( char *str)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 char *endptr = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 int base = 10;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 errno=0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
385 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
386 base = 16;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 str+=2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
388 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 long ret_val = strtol(str, &endptr, base);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 if (errno) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 else if (endptr && (*endptr!='\0'||endptr == str)) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 return ret_val;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
393 }
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 // IP String STR to binary data.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 static int striptovar( char *str, void *var)
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 in_addr_t addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 if(!str) error_exit("NULL address string.");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 addr = inet_addr(str);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 if(addr == -1) error_exit("Wrong address %s.",str );
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 *((uint32_t*)(var)) = (uint32_t)addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
403 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
405
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 // String to dhcp option conversion
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 static int strtoopt( char *str, uint8_t optonly)
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 char *option, *valstr, *grp, *tp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
410 long optcode = 0, convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 uint16_t flag = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 uint32_t mask, nip, router;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
414
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
415 if (!*str) return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 option = strtok((char*)str, ":");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
417 if (!option) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
418
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 dbg("-x option : %s ", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 optcode = strtou32(option);
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 if (optcode > 0 && optcode < 256) { // raw option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 if ((options_list[count].code & 0X00FF) == optcode) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
426 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
427 }
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 if (count == size) error_exit("Obsolete OR Unknown Option : %s", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 } else { // string option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
431 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 if (!strcmp(options_list[count].key, option)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 optcode = (options_list[count].code & 0X00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
435 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
438 if (count == size) error_exit("Obsolete OR Unknown Option : %s", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
439 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 if (!flag || !optcode) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 if (optonly) return optcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
442
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
443 valstr = strtok(NULL, "\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
444 if (!valstr) error_exit("option %s has no value defined.\n", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 dbg(" value : %-20s \n ", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
446 switch (flag) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
447 case DHCP_NUM32:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
450 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 if (convtmp < 0) error_exit("Invalid/wrong formated number %s", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 convtmp = htonl(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
453 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
454 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
455 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
456 options_list[count].len = sizeof(uint16_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
457 options_list[count].val = xmalloc(sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
458 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
459 if (convtmp < 0) error_exit("Invalid/malformed number %s", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
460 convtmp = htons(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
461 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
462 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
463 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
464 options_list[count].len = sizeof(uint8_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
465 options_list[count].val = xmalloc(sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 if (convtmp < 0) error_exit("Invalid/malformed number %s", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 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
469 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
471 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
472 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
473 striptovar(valstr, options_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
474 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 options_list[count].len = strlen(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 options_list[count].val = strdup(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
478 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
479 case DHCP_IPLIST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 while(valstr){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
481 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
482 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
483 options_list[count].len += sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 valstr = strtok(NULL," \t");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
486 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 case DHCP_STRLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 case DHCP_IPPLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
490 case DHCP_STCRTS:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
491 /* Option binary format:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 * mask [one byte, 0..32]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
493 * ip [0..4 bytes depending on mask]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
494 * router [4 bytes]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 * may be repeated
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 * 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
497 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
498 grp = strtok(valstr, ",");;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
499 while(grp){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
500 while(*grp == ' ' || *grp == '\t') grp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
501 tp = strchr(grp, '/');
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
502 if (!tp) error_exit("malformed static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
503 *tp = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
504 mask = strtol(++tp, &tp, 10);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
505 if (striptovar(grp, (uint8_t*)&nip) < 0) error_exit("malformed static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
506 while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
507 if (striptovar(tp, (uint8_t*)&router) < 0) error_exit("malformed static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
508 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
509 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
510 options_list[count].len += 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
511 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
512 options_list[count].len += mask/8;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
513 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
514 options_list[count].len += 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
515 tp = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
516 grp = strtok(NULL, ",");
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 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
519 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
520 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
521 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
522
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
523 // Creates environment pointers from RES to use in script
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
524 static int fill_envp(dhcpc_result_t *res)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
525 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
526 struct in_addr temp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
527 int size = ARRAY_LEN(options_list), count, ret = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
528
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
529 ret = setenv("interface", state->iface, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 if (!res) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
531 if (res->ipaddr.s_addr) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
532 temp.s_addr = htonl(res->ipaddr.s_addr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
533 ret = setenv("ip", inet_ntoa(temp), 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
534 if (ret) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
535 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
536 if (msgopt_list) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
537 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
538 if ((msgopt_list[count].len == 0) || (msgopt_list[count].val == NULL)) continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
539 ret = setenv(msgopt_list[count].key, (char*)msgopt_list[count].val, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
540 if (ret) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
541 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
542 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
543 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
544 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
545
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
546 // Executes Script NAME.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
547 static void run_script(dhcpc_result_t *res, char *name)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
548 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
549 volatile int error = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
550 pid_t pid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
551 char *argv[3];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
552 struct stat sts;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
553 char *script = flag_get(FLAG_s, TT.script, "/usr/share/dhcp/default.script");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
554
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
555 if (stat(script, &sts) == -1 && errno == ENOENT) return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
556 if (fill_envp(res)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
557 dbg("Failed to create environment variables.");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
558 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
559 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
560 dbg("Executing %s %s\n", script, name);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
561 argv[0] = (char*) script;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
562 argv[1] = (char*) name;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
563 argv[2] = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
564 fflush(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
565
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
566 pid = vfork();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
567 if (pid < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
568 dbg("Fork failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
569 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
570 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
571 if (!pid) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
572 execvp(argv[0], argv);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
573 error = errno;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
574 _exit(111);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
575 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
576 if (error) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
577 waitpid(pid, NULL,0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
578 errno = error;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
579 perror_msg("script exec failed");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
580 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
581 dbg("script complete.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
582 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
583
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
584 // returns a randome ID
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
585 static uint32_t getxid(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
586 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
587 uint32_t randnum;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
588 int fd = xopen("/dev/urandom", O_RDONLY);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
589 xreadall(fd, &randnum, sizeof(randnum));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
590 xclose(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
591 return randnum;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
592 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
593
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
594 // opens socket in raw mode.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
595 static int mode_raw(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
596 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
597 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
598 struct sockaddr_ll sock;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
599
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
600 if (state->sockfd > 0) close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
601 dbg("Opening raw socket on ifindex %d\n", state->ifindex);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
602
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
603 state->sockfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
604 if (state->sockfd < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
605 dbg("MODE RAW : socket fail ERROR : %d\n", state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
606 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
607 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
608 dbg("Got raw socket fd %d\n", state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
609 memset(&sock, 0, sizeof(sock));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
610 sock.sll_family = AF_PACKET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
611 sock.sll_protocol = htons(ETH_P_IP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
612 sock.sll_ifindex = state->ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
613
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
614 if (bind(state->sockfd, (struct sockaddr *) &sock, sizeof(sock))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
615 dbg("MODE RAW : bind fail.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
616 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
617 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
618 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
619 state->mode = MODE_RAW;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
620 if (setsockopt(state->sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) < 0)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
621 dbg("MODE RAW : filter attach fail.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
622
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
623 dbg("MODE RAW : success\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
624 return 0;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
627 // opens UDP socket
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
628 static int mode_app(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
629 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
630 struct sockaddr_in addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
631 struct ifreq ifr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
632
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
633 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
634 if (state->sockfd > 0) close(state->sockfd);
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 dbg("Opening listen socket on *:%d %s\n", DHCPC_CLIENT_PORT, state->iface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
637 state->sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
638 if (state->sockfd < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
639 dbg("MODE APP : socket fail ERROR: %d\n", state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
640 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
641 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
642 setsockopt(state->sockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
643 if (setsockopt(state->sockfd, SOL_SOCKET, SO_BROADCAST, &set, sizeof(set)) == -1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
644 dbg("MODE APP : brodcast failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
645 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
646 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
647 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
648 strncpy(ifr.ifr_name, state->iface, IFNAMSIZ);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
649 ifr.ifr_name[IFNAMSIZ -1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
650 setsockopt(state->sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
651
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
652 memset(&addr, 0, sizeof(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
653 addr.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
654 addr.sin_port = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
655 addr.sin_addr.s_addr = INADDR_ANY ;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
656
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
657 if (bind(state->sockfd, (struct sockaddr *) &addr, sizeof(addr))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
658 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
659 dbg("MODE APP : bind failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
660 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
661 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
662 state->mode = MODE_APP;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
663 dbg("MODE APP : success\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
664 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
665 }
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 static int read_raw(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
668 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
669 dhcp_raw_t packet;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
670 uint16_t check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
671 int bytes = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
672
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
673 memset(&packet, 0, sizeof(packet));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
674 if ((bytes = read(state->sockfd, &packet, sizeof(packet))) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
675 dbg("\tPacket read error, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
676 return bytes;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
677 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
678 if (bytes < (int) (sizeof(packet.iph) + sizeof(packet.udph))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
679 dbg("\tPacket is too short, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
680 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
681 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
682 if (bytes < ntohs(packet.iph.tot_len)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
683 dbg("\tOversized packet, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
684 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
685 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
686 // ignore any extra garbage bytes
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
687 bytes = ntohs(packet.iph.tot_len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
688 // make sure its the right packet for us, and that it passes sanity checks
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
689 if (packet.iph.protocol != IPPROTO_UDP || packet.iph.version != IPVERSION
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
690 || packet.iph.ihl != (sizeof(packet.iph) >> 2)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
691 || packet.udph.dest != htons(DHCPC_CLIENT_PORT)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
692 || ntohs(packet.udph.len) != (uint16_t)(bytes - sizeof(packet.iph))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
693 dbg("\tUnrelated/bogus packet, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
694 return -2;
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 // verify IP checksum
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
697 check = packet.iph.check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
698 packet.iph.check = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
699 if (check != dhcp_checksum(&packet.iph, sizeof(packet.iph))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
700 dbg("\tBad IP header checksum, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
701 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
702 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
703 memset(&packet.iph, 0, ((size_t) &((struct iphdr *)0)->protocol));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
704 packet.iph.tot_len = packet.udph.len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
705 check = packet.udph.check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
706 packet.udph.check = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
707 if (check && check != dhcp_checksum(&packet, bytes)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
708 dbg("\tPacket with bad UDP checksum received, 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 memcpy(&state->pdhcp, &packet.dhcp, bytes - (sizeof(packet.iph) + sizeof(packet.udph)));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
712 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
713 dbg("\tPacket with bad magic, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
714 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
715 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
716 return bytes - sizeof(packet.iph) - sizeof(packet.udph);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
717 }
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 static int read_app(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
720 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
721 int ret;
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 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
724 if ((ret = read(state->sockfd, &state->pdhcp, sizeof(dhcp_msg_t))) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
725 dbg("Packet read error, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
726 return ret; /* returns -1 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
727 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
728 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
729 dbg("Packet with bad magic, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
730 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
731 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
732 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
733 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
734
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
735 // Sends data through raw socket.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
736 static int send_raw(void)
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 struct sockaddr_ll dest_sll;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
739 dhcp_raw_t packet;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
740 unsigned padding;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
741 int fd, result = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
742
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
743 memset(&packet, 0, sizeof(dhcp_raw_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
744 memcpy(&packet.dhcp, &state->pdhcp, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
745
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
746 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
747 dbg("SEND RAW: socket failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
748 return result;
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 memset(&dest_sll, 0, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
751 dest_sll.sll_family = AF_PACKET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
752 dest_sll.sll_protocol = htons(ETH_P_IP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
753 dest_sll.sll_ifindex = state->ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
754 dest_sll.sll_halen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
755 memcpy(dest_sll.sll_addr, bmacaddr , 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
756
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
757 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
758 dbg("SEND RAW: bind failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
759 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
760 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
761 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
762 padding = 308 - 1 - dhcp_opt_size(state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
763 packet.iph.protocol = IPPROTO_UDP;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
764 packet.iph.saddr = INADDR_ANY;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
765 packet.iph.daddr = INADDR_BROADCAST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
766 packet.udph.source = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
767 packet.udph.dest = htons(DHCPC_SERVER_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
768 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
769 packet.iph.tot_len = packet.udph.len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
770 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
771 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
772 packet.iph.ihl = sizeof(packet.iph) >> 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
773 packet.iph.version = IPVERSION;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
774 packet.iph.ttl = IPDEFTTL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
775 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
776
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
777 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
778 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
779
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
780 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
781 if (result < 0) dbg("SEND RAW: PACKET send error\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
782 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
783 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
784
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
785 // Sends data through UDP socket.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
786 static int send_app(void)
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 struct sockaddr_in cli;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
789 int fd, ret = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
790
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
791 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
792 dbg("SEND APP: sock failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
793 return ret;
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 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
796
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
797 memset(&cli, 0, sizeof(cli));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
798 cli.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
799 cli.sin_port = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
800 cli.sin_addr.s_addr = state->pdhcp.ciaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
801 if (bind(fd, (struct sockaddr *)&cli, sizeof(cli)) == -1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
802 dbg("SEND APP: bind failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
803 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
804 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
805 memset(&cli, 0, sizeof(cli));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
806 cli.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
807 cli.sin_port = htons(DHCPC_SERVER_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
808 cli.sin_addr.s_addr = state->serverid.s_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
809 if (connect(fd, (struct sockaddr *)&cli, sizeof(cli)) == -1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
810 dbg("SEND APP: connect failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
811 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
812 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
813 int padding = 308 - 1 - dhcp_opt_size(state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
814 if((ret = write(fd, &state->pdhcp, sizeof(dhcp_msg_t) - padding)) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
815 dbg("SEND APP: write failed error %d\n", ret);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
816 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
817 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
818 dbg("SEND APP: write success wrote %d\n", ret);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
819 error_fd:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
820 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
821 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
822 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
823
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
824 // 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
825 static void signal_handler(int sig)
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 unsigned char ch = sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
828 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
829 }
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 // signal setup for SIGUSR1 SIGUSR2 SIGTERM
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
832 static int setup_signal()
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 if (pipe((int *)&sigfd) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
835 dbg("signal pipe failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
836 return -1;
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 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
839 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
840 int flags = fcntl(sigfd.wr, F_GETFL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
841 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
842 signal(SIGUSR1, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
843 signal(SIGUSR2, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
844 signal(SIGTERM, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
845
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
846 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
847 }
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 // adds client id to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
850 static uint8_t *dhcpc_addclientid(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
851 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
852 *optptr++ = DHCP_OPTION_CLIENTID;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
853 *optptr++ = 7;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
854 *optptr++ = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
855 memcpy(optptr, &state->macaddr, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
856 return optptr + 6;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
859 // adds messege type to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
860 static uint8_t *dhcpc_addmsgtype(uint8_t *optptr, uint8_t type)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
861 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
862 *optptr++ = DHCP_OPTION_MSG_TYPE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
863 *optptr++ = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
864 *optptr++ = type;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
865 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
866 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
867
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
868 // adds max size to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
869 static uint8_t *dhcpc_addmaxsize(uint8_t *optptr, uint16_t size)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
870 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
871 *optptr++ = DHCP_OPTION_MAX_SIZE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
872 *optptr++ = 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
873 memcpy(optptr, &size, 2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
874 return optptr + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
875 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
876
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
877 static uint8_t *dhcpc_addstropt(uint8_t *optptr, uint8_t opcode, char* str, int len)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
878 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
879 *optptr++ = opcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
880 *optptr++ = len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
881 memcpy(optptr, str, len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
882 return optptr + len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
883 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
884
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
885 // adds server id to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
886 static uint8_t *dhcpc_addserverid(struct in_addr *serverid, uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
887 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
888 *optptr++ = DHCP_OPTION_SERVER_ID;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
889 *optptr++ = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
890 memcpy(optptr, &serverid->s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
891 return optptr + 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
892 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
893
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
894 // adds requested ip address to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
895 static uint8_t *dhcpc_addreqipaddr(struct in_addr *ipaddr, uint8_t *optptr)
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 *optptr++ = DHCP_OPTION_REQ_IPADDR;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
898 *optptr++ = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
899 memcpy(optptr, &ipaddr->s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
900 return optptr + 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
901 }
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 // adds hostname to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
904 static uint8_t *dhcpc_addfdnname(uint8_t *optptr, char *hname)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
905 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
906 int size = strlen(hname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
907 *optptr++ = DHCP_OPTION_FQDN;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
908 *optptr++ = size + 3;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
909 *optptr++ = 0x1; //flags
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
910 optptr += 2; // two blank bytes
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
911 strncpy((char*)optptr, hname, size); // name
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
912 return optptr + size;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
913 }
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 // adds request options using -o,-O flag to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
916 static uint8_t *dhcpc_addreqoptions(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
917 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
918 uint8_t *len;
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 *optptr++ = DHCP_OPTION_REQ_LIST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
921 len = optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
922 *len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
923 optptr++;
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 if (!flag_chk(FLAG_o)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
926 *len = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
927 *optptr++ = DHCP_OPTION_SUBNET_MASK;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
928 *optptr++ = DHCP_OPTION_ROUTER;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
929 *optptr++ = DHCP_OPTION_DNS_SERVER;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
930 *optptr++ = DHCP_OPTION_BROADCAST;
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 if (flag_chk(FLAG_O)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
933 memcpy(optptr++, raw_opt, raw_optcount);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
934 *len += raw_optcount;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
935 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
936 return optptr;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
939 static uint8_t *dhcpc_addend(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
940 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
941 *optptr++ = DHCP_OPTION_END;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
942 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
943 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
944
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
945 // Sets values of -x options in dhcp discover and request packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
946 static uint8_t* set_xopt(uint8_t *optptr)
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 int count;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
949 int size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
950 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
951 if ((options_list[count].len == 0) || (options_list[count].val == NULL)) continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
952 *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
953 *optptr++ = (uint8_t) options_list[count].len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
954 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
955 optptr += options_list[count].len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
956 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
957 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
958 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
959
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
960 static uint32_t get_option_serverid (uint8_t *opt, dhcpc_result_t *presult)
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 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
963 while (*opt != DHCP_OPTION_SERVER_ID) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
964 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
965 opt += opt[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
966 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
967 memcpy(&var, opt+2, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
968 state->serverid.s_addr = var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
969 presult->serverid.s_addr = state->serverid.s_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
970 presult->serverid.s_addr = ntohl(presult->serverid.s_addr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
971 return var;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
974 static uint8_t get_option_msgtype(uint8_t *opt)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
975 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
976 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
977 while (*opt != DHCP_OPTION_MSG_TYPE) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
978 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
979 opt += opt[1] + 2;
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 memcpy(&var, opt+2, sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
982 return var;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
985 static uint8_t get_option_lease(uint8_t *opt, dhcpc_result_t *presult)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
986 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
987 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
988 while (*opt != DHCP_OPTION_LEASE_TIME) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
989 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
990 opt += opt[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
991 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
992 memcpy(&var, opt+2, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
993 var = htonl(var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
994 presult->lease_time = var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
995 return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
996 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
997
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 // sends dhcp msg of MSGTYPE
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1000 static int dhcpc_sendmsg(int msgtype)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1001 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1002 uint8_t *pend;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1003 struct in_addr rqsd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1004 char *vendor;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1005
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1006 // Create the common message header settings
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1007 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1008 state->pdhcp.op = DHCP_REQUEST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1009 state->pdhcp.htype = DHCP_HTYPE_ETHERNET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1010 state->pdhcp.hlen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1011 state->pdhcp.xid = xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1012 memcpy(state->pdhcp.chaddr, state->macaddr, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1013 memset(&state->pdhcp.chaddr[6], 0, 10);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1014 state->pdhcp.cookie = htonl(DHCP_MAGIC);;
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 // Add the common header options
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1017 pend = state->pdhcp.options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1018 pend = dhcpc_addmsgtype(pend, msgtype);
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 (!flag_chk(FLAG_C)) pend = dhcpc_addclientid(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1021 // Handle the message specific settings
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1022 switch (msgtype) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1023 case DHCPDISCOVER: // Broadcast DISCOVER message to all servers
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1024 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1025 if (flag_chk(FLAG_r)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1026 inet_aton(TT.req_ip, &rqsd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1027 pend = dhcpc_addreqipaddr(&rqsd, pend);
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 pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t)));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1030 vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1031 pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1032 if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1033 if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1034 if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1035 if (flag_chk(FLAG_x)) pend = set_xopt(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1036 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1037 case DHCPREQUEST: // Send REQUEST message to the server that sent the *first* OFFER
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1038 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1039 if (state->status == STATE_RENEWING) memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1040 pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t)));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1041 rqsd.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1042 pend = dhcpc_addserverid(&rqsd, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1043 pend = dhcpc_addreqipaddr(&state->ipaddr, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1044 vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1045 pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1046 if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1047 if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1048 if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1049 if (flag_chk(FLAG_x)) pend = set_xopt(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1050 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1051 case DHCPRELEASE: // Send RELEASE message to the server.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1052 memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1053 rqsd.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1054 pend = dhcpc_addserverid(&rqsd, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1055 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1056 default:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1057 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1058 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1059 pend = dhcpc_addend(pend);
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 if (state->mode == MODE_APP) return send_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1062 return send_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1063 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1064
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1065 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1066 * parses options from received dhcp packet at OPTPTR and
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1067 * stores result in PRESULT or MSGOPT_LIST
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 static uint8_t dhcpc_parseoptions(dhcpc_result_t *presult, uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1070 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1071 uint8_t type = 0, *options, overloaded = 0;;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1072 uint16_t flag = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1073 uint32_t convtmp = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1074 char *dest, *pfx;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1075 struct in_addr addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1076 int count, optlen, size = ARRAY_LEN(options_list);
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 if (flag_chk(FLAG_x)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1079 if(msgopt_list){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1080 for (count = 0; count < size; count++){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1081 if(msgopt_list[count].val) free(msgopt_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1082 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1083 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1084 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1085 } else {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1086 msgopt_list = xmalloc(sizeof(options_list));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1087 memcpy(msgopt_list, options_list, sizeof(options_list));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1088 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1089 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1090 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1091 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1092 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1093 } else {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1094 msgopt_list = options_list;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1095 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1096 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1097 if(msgopt_list[count].val) free(msgopt_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1098 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1099 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1100 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1101
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1102 while (*optptr != DHCP_OPTION_END) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1103 while (*optptr == DHCP_OPTION_PADDING) optptr++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1104 if (*optptr == DHCP_OPTION_OVERLOAD) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1105 overloaded = optptr[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1106 optptr += optptr[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1107 continue;
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 for (count = 0, flag = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1110 if ((msgopt_list[count].code & 0X00FF) == *optptr) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1111 flag = (msgopt_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1112 break;
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 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1115 switch (flag) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1116 case DHCP_NUM32:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1117 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1118 convtmp = htonl(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1119 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1120 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1121 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1122 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1123 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1124 memcpy(&convtmp, &optptr[2], sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1125 convtmp = htons(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1126 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1127 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1128 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1129 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1130 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1131 memcpy(&convtmp, &optptr[2], sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1132 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1133 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1134 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1135 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1136 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1137 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1138 addr.s_addr = convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1139 sprintf(toybuf, "%s", inet_ntoa(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1140 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1141 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1142 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1143 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1144 sprintf(toybuf, "%.*s", optptr[1], &optptr[2]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1145 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1146 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1147 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1148 case DHCP_IPLIST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1149 optlen = optptr[1];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1150 dest = toybuf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1151 while (optlen) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1152 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1153 addr.s_addr = convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1154 dest += sprintf(dest, "%s ", inet_ntoa(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1155 optlen -= 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1156 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1157 *(dest - 1) = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1158 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1159 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1160 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1161 case DHCP_STRLST: //FIXME: do smthing.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1162 case DHCP_IPPLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1163 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1164 case DHCP_STCRTS:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1165 pfx = "";
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1166 dest = toybuf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1167 options = &optptr[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1168 optlen = optptr[1];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1169
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1170 while (optlen >= 1 + 4) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1171 uint32_t nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1172 int bytes;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1173 uint8_t *p_tmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1174 unsigned mask = *options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1175
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1176 if (mask > 32) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1177 optlen--;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1178 p_tmp = (void*) &nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1179 bytes = (mask + 7) / 8;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1180 while (--bytes >= 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1181 *p_tmp++ = *options++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1182 optlen--;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1183 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1184 if (optlen < 4) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1185 dest += sprintf(dest, "%s%u.%u.%u.%u", pfx, ((uint8_t*) &nip)[0],
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1186 ((uint8_t*) &nip)[1], ((uint8_t*) &nip)[2], ((uint8_t*) &nip)[3]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1187 pfx = " ";
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1188 dest += sprintf(dest, "/%u ", mask);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1189 dest += sprintf(dest, "%u.%u.%u.%u", options[0], options[1], options[2], options[3]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1190 options += 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1191 optlen -= 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1192 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1193 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1194 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1195 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1196 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1197 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1198 optptr += optptr[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1199 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1200 if ((overloaded == 1) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1201 if ((overloaded == 2) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1202 return type;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1203 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1204
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1205 // parses recvd messege to check that it was for us.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1206 static uint8_t dhcpc_parsemsg(dhcpc_result_t *presult)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1207 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1208 if (state->pdhcp.op == DHCP_REPLY
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1209 && !memcmp(state->pdhcp.chaddr, state->macaddr, 6)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1210 && !memcmp(&state->pdhcp.xid, &xid, sizeof(xid))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1211 memcpy(&presult->ipaddr.s_addr, &state->pdhcp.yiaddr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1212 presult->ipaddr.s_addr = ntohl(presult->ipaddr.s_addr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1213 return get_option_msgtype(state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1214 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1215 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1216 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1217
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1218 // Sends a IP renew request.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1219 static void renew(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1220 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1221 infomsg(infomode, "Performing a DHCP renew");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1222 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1223 case STATE_INIT:
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 case STATE_BOUND:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1226 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1227 case STATE_RENEWING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1228 case STATE_REBINDING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1229 state->status = STATE_RENEW_REQUESTED;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1230 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1231 case STATE_RENEW_REQUESTED:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1232 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1233 case STATE_REQUESTING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1234 case STATE_RELEASED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1235 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1236 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1237 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1238 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1239 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1240 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1241
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1242 // Sends a IP release request.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1243 static void release(void)
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 int len = sizeof("255.255.255.255\0");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1246 char buffer[len];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1247 struct in_addr temp_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1248
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1249 mode_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1250 // send release packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1251 if (state->status == STATE_BOUND || state->status == STATE_RENEWING || state->status == STATE_REBINDING) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1252 temp_addr.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1253 strncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1254 buffer[len - 1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1255 temp_addr.s_addr = state->ipaddr.s_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1256 infomsg( infomode, "Unicasting a release of %s to %s", inet_ntoa(temp_addr), buffer);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1257 dhcpc_sendmsg(DHCPRELEASE);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1258 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1259 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1260 infomsg(infomode, "Entering released state");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1261 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1262 state->sockfd = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1263 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1264 state->status = STATE_RELEASED;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1265 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1266
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1267 static void free_option_stores(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1268 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1269 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1270 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1271 if (options_list[count].val) free(options_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1272 if(flag_chk(FLAG_x)){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1273 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1274 if (msgopt_list[count].val) free(msgopt_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1275 free(msgopt_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1276 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1277 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1278
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1279 void dhcp_main(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1280 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1281 struct timeval tv;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1282 int retval, bufflen = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1283 dhcpc_result_t result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1284 uint8_t packets = 0, retries = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1285 uint32_t timeout = 0, waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1286 fd_set rfds;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1287
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1288 xid = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1289 setlinebuf(stdout);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1290 dbg = dummy;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1291 if (flag_chk(FLAG_v)) dbg = xprintf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1292 if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1293 retries = flag_get(FLAG_t, TT.retries, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1294 if (flag_chk(FLAG_S)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1295 openlog("UDHCPC :", LOG_PID, LOG_DAEMON);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1296 infomode |= LOG_SYSTEM;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1297 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1298 infomsg(infomode, "dhcp started");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1299 if (flag_chk(FLAG_O)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1300 while (TT.req_opt) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1301 raw_opt[raw_optcount] = (uint8_t) strtoopt(TT.req_opt->arg, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1302 raw_optcount++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1303 TT.req_opt = TT.req_opt->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1304 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1305 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1306 if (flag_chk(FLAG_x)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1307 while (TT.pkt_opt) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1308 (void) strtoopt(TT.pkt_opt->arg, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1309 TT.pkt_opt = TT.pkt_opt->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1310 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1311 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1312 memset(&result, 0, sizeof(dhcpc_result_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1313 state = (dhcpc_state_t*) xmalloc(sizeof(dhcpc_state_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1314 memset(state, 0, sizeof(dhcpc_state_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1315 state->iface = flag_get(FLAG_i, TT.iface, "eth0");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1316
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1317 if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1318 perror_exit("Failed to get interface %s", state->iface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1319
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1320 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1321 setup_signal();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1322 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1323 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1324 fcntl(state->sockfd, F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1325
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1326 for (;;) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1327 FD_ZERO(&rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1328 if (state->sockfd >= 0) FD_SET(state->sockfd, &rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1329 FD_SET(sigfd.rd, &rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1330 tv.tv_sec = timeout - waited;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1331 tv.tv_usec = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1332 retval = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1333
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1334 int maxfd = (sigfd.rd > state->sockfd)? sigfd.rd : state->sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1335 dbg("select wait ....\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1336 uint32_t timestmp = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1337 if((retval = select(maxfd + 1, &rfds, NULL, NULL, &tv)) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1338 if (errno == EINTR) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1339 waited += (unsigned) time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1340 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1341 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1342 perror_exit("Error in select");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1343 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1344 if (!retval) { // Timed out
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1345 if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr))
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1346 error_exit("Interface lost %s\n", state->iface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1347
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1348 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1349 case STATE_INIT:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1350 if (packets < retries) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1351 if (!packets) xid = getxid();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1352 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1353 infomsg(infomode, "Sending discover...");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1354 dhcpc_sendmsg(DHCPDISCOVER);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1355 server = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1356 timeout = flag_get(FLAG_T, TT.timeout, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1357 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1358 packets++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1359 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1360 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1361 lease_fail:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1362 run_script(NULL,"leasefail");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1363 if (flag_chk(FLAG_n)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1364 infomsg(infomode, "Lease failed. Exiting");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1365 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1366 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1367 if (flag_chk(FLAG_b)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1368 infomsg(infomode, "Lease failed. Going Daemon mode");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1369 dhcp_daemon();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1370 if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1371 toys.optflags &= ~FLAG_b;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1372 toys.optflags |= FLAG_f;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1373 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1374 timeout = flag_get(FLAG_A, TT.tryagain, 20);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1375 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1376 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1377 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1378 case STATE_REQUESTING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1379 if (packets < retries) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1380 memcpy(&state->ipaddr.s_addr,&state->pdhcp.yiaddr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1381 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1382 infomsg(infomode, "Sending select for %d.%d.%d.%d...",
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1383 (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1384 timeout = flag_get(FLAG_T, TT.timeout, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1385 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1386 packets++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1387 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1388 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1389 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1390 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1391 goto lease_fail;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1392 case STATE_BOUND:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1393 state->status = STATE_RENEWING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1394 dbg("Entering renew state\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1395 // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1396 case STATE_RENEW_REQUESTED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1397 case STATE_RENEWING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1398 renew_requested:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1399 if (timeout > 60) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1400 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1401 timeout >>= 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1402 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1403 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1404 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1405 dbg("Entering rebinding state\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1406 state->status = STATE_REBINDING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1407 // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1408 case STATE_REBINDING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1409 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1410 if (timeout > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1411 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1412 timeout >>= 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1413 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1414 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1415 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1416 infomsg(infomode, "Lease lost, entering INIT state");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1417 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1418 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1419 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1420 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1421 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1422 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1423 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1424 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1425 timeout = INT_MAX;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1426 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1427 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1428 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1429 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
1430 unsigned char sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1431 if (read(sigfd.rd, &sig, 1) != 1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1432 dbg("signal read failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1433 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1434 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1435 switch (sig) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1436 case SIGUSR1:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1437 infomsg(infomode, "Received SIGUSR1");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1438 renew();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1439 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1440 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1441 if (state->status == STATE_RENEW_REQUESTED) goto renew_requested;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1442 if (state->status == STATE_INIT) timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1443 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1444 case SIGUSR2:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1445 infomsg(infomode, "Received SIGUSR2");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1446 release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1447 timeout = INT_MAX;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1448 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1449 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1450 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1451 case SIGTERM:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1452 infomsg(infomode, "Received SIGTERM");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1453 if (flag_chk(FLAG_R)) release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1454 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1455 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1456 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1457 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1458 if (FD_ISSET(state->sockfd, &rfds)) { // Some Activity on RDFDs : is socket
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1459 dbg("main sock read\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1460 uint8_t msgType;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1461 if (state->mode == MODE_RAW) bufflen = read_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1462 if (state->mode == MODE_APP) bufflen = read_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1463 if (bufflen < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1464 if (state->mode == MODE_RAW) mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1465 if (state->mode == MODE_APP) mode_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1466 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1467 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1468 waited += time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1469 memset(&result, 0, sizeof(dhcpc_result_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1470 msgType = dhcpc_parsemsg(&result);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1471 if (msgType != DHCPNAK && result.ipaddr.s_addr == 0 ) continue; // no ip for me ignore
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1472 if (!msgType || !get_option_serverid(state->pdhcp.options, &result)) continue; //no server id ignore
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1473 if (msgType == DHCPOFFER && server == 0) server = result.serverid.s_addr; // select the server
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1474 if (result.serverid.s_addr != server) continue; // not from the server we requested ignore
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1475 dhcpc_parseoptions(&result, state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1476 get_option_lease(state->pdhcp.options, &result);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1477
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1478 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1479 case STATE_INIT:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1480 if (msgType == DHCPOFFER) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1481 state->status = STATE_REQUESTING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1482 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1483 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1484 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1485 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1486 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1487 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1488 case STATE_REQUESTING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1489 case STATE_RENEWING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1490 case STATE_RENEW_REQUESTED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1491 case STATE_REBINDING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1492 if (msgType == DHCPACK) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1493 timeout = result.lease_time / 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1494 run_script(&result, state->status == STATE_REQUESTING ? "bound" : "renew");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1495 state->status = STATE_BOUND;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1496 infomsg(infomode, "Lease of %d.%d.%d.%d obtained, lease time %d from server %d.%d.%d.%d",
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1497 (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1498 result.lease_time,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1499 (result.serverid.s_addr >> 24) & 0xff, (result.serverid.s_addr >> 16) & 0xff, (result.serverid.s_addr >> 8) & 0xff, (result.serverid.s_addr) & 0xff);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1500 if (flag_chk(FLAG_q)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1501 if (flag_chk(FLAG_R)) release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1502 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1503 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1504 toys.optflags &= ~FLAG_n;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1505 if (!flag_chk(FLAG_f)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1506 dhcp_daemon();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1507 toys.optflags |= FLAG_f;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1508 if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1509 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1510 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1511 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1512 } else if (msgType == DHCPNAK) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1513 dbg("NACK received.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1514 run_script(&result, "nak");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1515 if (state->status != STATE_REQUESTING) run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1516 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1517 sleep(3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1518 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1519 state->ipaddr.s_addr = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1520 server = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1521 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1522 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1523 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1524 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1525 continue;