annotate toys/pending/dhcp.c @ 1327:85f297591693 draft

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