annotate toys/pending/dhcp.c @ 1104:e11684e3bbc5 draft

Merge toynet.h into toys.h: musl supports it and micromanaging uClibc config options isn't very interesting anymore.
author Rob Landley <rob@landley.net>
date Sat, 02 Nov 2013 14:24:33 -0500
parents d3f9e55e350a
children 88abd5b68a17
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 pid_t pid = fork();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
322
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 if (pid < 0) perror_exit("DAEMON: failed to fork");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 if (pid) exit(EXIT_SUCCESS);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
325
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 setsid();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 dup2(fd, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 dup2(fd, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 dup2(fd, 2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 if (fd > 2) xclose(fd);
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 return 0;
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 *logs messeges to syslog or console
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 *opening the log is still left with applet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 *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
339 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 static void infomsg(uint8_t infomode, char *s, ...)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 int used;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 char *msg;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 va_list p, t;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
345
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 if (infomode == LOG_SILENT) return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 va_start(p, s);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 va_copy(t, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 used = vsnprintf(NULL, 0, s, t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 used++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 va_end(t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
352
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 msg = xmalloc(used);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 vsnprintf(msg, used, s, p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 va_end(p);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
356
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
357 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
358 if (infomode & LOG_CONSOLE) printf("%s\n", msg);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 free(msg);
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
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 * Writes self PID in file PATH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 * FIXME: libc implementation only writes in /var/run
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 * 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
366 * 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
367 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
368 static void write_pid(char *path)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 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
371 if (pidfile > 0) {
1041
d3f9e55e350a Convert dhcp/dhcpd from utoa() to sprintf().
Rob Landley <rob@landley.net>
parents: 1005
diff changeset
372 char pidbuf[12];
d3f9e55e350a Convert dhcp/dhcpd from utoa() to sprintf().
Rob Landley <rob@landley.net>
parents: 1005
diff changeset
373
d3f9e55e350a Convert dhcp/dhcpd from utoa() to sprintf().
Rob Landley <rob@landley.net>
parents: 1005
diff changeset
374 sprintf(pidbuf, "%u", (unsigned)getpid());
1005
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 write(pidfile, pidbuf, strlen(pidbuf));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 close(pidfile);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
379
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 // String STR to UINT32 conversion strored in VAR
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 static long strtou32( char *str)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 char *endptr = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 int base = 10;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
385 errno=0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
386 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
387 base = 16;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
388 str+=2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 long ret_val = strtol(str, &endptr, base);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 if (errno) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 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
393 return ret_val;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
394 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
395
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 // IP String STR to binary data.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 static int striptovar( char *str, void *var)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
398 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 in_addr_t addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 if(!str) error_exit("NULL address string.");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 addr = inet_addr(str);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 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
403 *((uint32_t*)(var)) = (uint32_t)addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
405 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
406
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
407 // String to dhcp option conversion
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 static int strtoopt( char *str, uint8_t optonly)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
409 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
410 char *option, *valstr, *grp, *tp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 long optcode = 0, convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 uint16_t flag = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 uint32_t mask, nip, router;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
415
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 if (!*str) return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
417 option = strtok((char*)str, ":");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 if (!option) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
419
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 dbg("-x option : %s ", option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 optcode = strtou32(option);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
422
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 if (optcode > 0 && optcode < 256) { // raw option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 if ((options_list[count].code & 0X00FF) == optcode) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
426 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
427 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 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
431 } else { // string option
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 if (!strcmp(options_list[count].key, option)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 flag = (options_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
435 optcode = (options_list[count].code & 0X00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 }
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 (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
440 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 if (!flag || !optcode) return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
442 if (optonly) return optcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
443
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
444 valstr = strtok(NULL, "\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 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
446 dbg(" value : %-20s \n ", valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
447 switch (flag) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 case DHCP_NUM32:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
450 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 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
453 convtmp = htonl(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
454 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
455 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
456 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
457 options_list[count].len = sizeof(uint16_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
458 options_list[count].val = xmalloc(sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
459 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
460 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
461 convtmp = htons(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
462 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
463 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
464 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
465 options_list[count].len = sizeof(uint8_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
466 options_list[count].val = xmalloc(sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
467 convtmp = strtou32(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
468 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
469 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
470 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
471 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
472 options_list[count].len = sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
473 options_list[count].val = xmalloc(sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
474 striptovar(valstr, options_list[count].val);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 options_list[count].len = strlen(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
478 options_list[count].val = strdup(valstr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
479 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 case DHCP_IPLIST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
481 while(valstr){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
482 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
483 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
484 options_list[count].len += sizeof(uint32_t);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 valstr = strtok(NULL," \t");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
486 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
487 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 case DHCP_STRLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 case DHCP_IPPLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
490 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
491 case DHCP_STCRTS:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 /* Option binary format:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
493 * mask [one byte, 0..32]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
494 * ip [0..4 bytes depending on mask]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
495 * router [4 bytes]
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
496 * may be repeated
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 * 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
498 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
499 grp = strtok(valstr, ",");;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
500 while(grp){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
501 while(*grp == ' ' || *grp == '\t') grp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
502 tp = strchr(grp, '/');
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
503 if (!tp) error_exit("malformed static route option");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
504 *tp = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
505 mask = strtol(++tp, &tp, 10);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
506 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
507 while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
508 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
509 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
510 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
511 options_list[count].len += 1;
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, &nip, mask/8);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
513 options_list[count].len += mask/8;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
514 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
515 options_list[count].len += 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
516 tp = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
517 grp = strtok(NULL, ",");
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 break;
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 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
522 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
523
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
524 // 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
525 static int fill_envp(dhcpc_result_t *res)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
526 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
527 struct in_addr temp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
528 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
529
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 ret = setenv("interface", state->iface, 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
531 if (!res) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
532 if (res->ipaddr.s_addr) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
533 temp.s_addr = htonl(res->ipaddr.s_addr);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
534 ret = setenv("ip", inet_ntoa(temp), 1);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
535 if (ret) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
536 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
537 if (msgopt_list) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
538 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
539 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
540 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
541 if (ret) return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
542 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
543 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
544 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
545 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
546
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
547 // Executes Script NAME.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
548 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
549 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
550 volatile int error = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
551 pid_t pid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
552 char *argv[3];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
553 struct stat sts;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
554 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
555
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
556 if (stat(script, &sts) == -1 && errno == ENOENT) return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
557 if (fill_envp(res)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
558 dbg("Failed to create environment variables.");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
559 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
560 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
561 dbg("Executing %s %s\n", script, name);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
562 argv[0] = (char*) script;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
563 argv[1] = (char*) name;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
564 argv[2] = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
565 fflush(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
566
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
567 pid = vfork();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
568 if (pid < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
569 dbg("Fork failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
570 return;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
571 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
572 if (!pid) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
573 execvp(argv[0], argv);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
574 error = errno;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
575 _exit(111);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
576 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
577 if (error) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
578 waitpid(pid, NULL,0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
579 errno = error;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
580 perror_msg("script exec failed");
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 dbg("script complete.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
583 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
584
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
585 // returns a randome ID
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
586 static uint32_t getxid(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
587 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
588 uint32_t randnum;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
589 int fd = xopen("/dev/urandom", O_RDONLY);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
590 xreadall(fd, &randnum, sizeof(randnum));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
591 xclose(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
592 return randnum;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
593 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
594
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
595 // opens socket in raw mode.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
596 static int mode_raw(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
597 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
598 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
599 struct sockaddr_ll sock;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
600
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
601 if (state->sockfd > 0) close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
602 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
603
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
604 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
605 if (state->sockfd < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
606 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
607 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
608 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
609 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
610 memset(&sock, 0, sizeof(sock));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
611 sock.sll_family = AF_PACKET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
612 sock.sll_protocol = htons(ETH_P_IP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
613 sock.sll_ifindex = state->ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
614
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
615 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
616 dbg("MODE RAW : bind fail.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
617 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
618 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
619 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
620 state->mode = MODE_RAW;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
621 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
622 dbg("MODE RAW : filter attach fail.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
623
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
624 dbg("MODE RAW : success\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
625 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
626 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
627
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
628 // opens UDP socket
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
629 static int mode_app(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
630 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
631 struct sockaddr_in addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
632 struct ifreq ifr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
633
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
634 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
635 if (state->sockfd > 0) close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
636
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
637 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
638 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
639 if (state->sockfd < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
640 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
641 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
642 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
643 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
644 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
645 dbg("MODE APP : brodcast failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
646 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
647 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
648 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
649 strncpy(ifr.ifr_name, state->iface, IFNAMSIZ);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
650 ifr.ifr_name[IFNAMSIZ -1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
651 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
652
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
653 memset(&addr, 0, sizeof(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
654 addr.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
655 addr.sin_port = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
656 addr.sin_addr.s_addr = INADDR_ANY ;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
657
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
658 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
659 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
660 dbg("MODE APP : bind failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
661 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
662 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
663 state->mode = MODE_APP;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
664 dbg("MODE APP : success\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
665 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
666 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
667
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
668 static int read_raw(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
669 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
670 dhcp_raw_t packet;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
671 uint16_t check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
672 int bytes = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
673
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
674 memset(&packet, 0, sizeof(packet));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
675 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
676 dbg("\tPacket read error, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
677 return bytes;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
678 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
679 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
680 dbg("\tPacket is too short, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
681 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
682 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
683 if (bytes < ntohs(packet.iph.tot_len)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
684 dbg("\tOversized packet, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
685 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
686 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
687 // ignore any extra garbage bytes
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
688 bytes = ntohs(packet.iph.tot_len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
689 // 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
690 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
691 || packet.iph.ihl != (sizeof(packet.iph) >> 2)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
692 || packet.udph.dest != htons(DHCPC_CLIENT_PORT)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
693 || 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
694 dbg("\tUnrelated/bogus packet, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
695 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
696 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
697 // verify IP checksum
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
698 check = packet.iph.check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
699 packet.iph.check = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
700 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
701 dbg("\tBad IP header checksum, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
702 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
703 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
704 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
705 packet.iph.tot_len = packet.udph.len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
706 check = packet.udph.check;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
707 packet.udph.check = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
708 if (check && check != dhcp_checksum(&packet, bytes)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
709 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
710 return -2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
711 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
712 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
713 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
714 dbg("\tPacket with bad magic, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
715 return -2;
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 return bytes - sizeof(packet.iph) - sizeof(packet.udph);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
718 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
719
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
720 static int read_app(void)
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 int ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
723
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
724 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
725 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
726 dbg("Packet read error, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
727 return ret; /* returns -1 */
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
728 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
729 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
730 dbg("Packet with bad magic, ignoring\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
731 return -2;
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 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
734 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
735
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
736 // Sends data through raw socket.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
737 static int send_raw(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
738 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
739 struct sockaddr_ll dest_sll;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
740 dhcp_raw_t packet;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
741 unsigned padding;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
742 int fd, result = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
743
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
744 memset(&packet, 0, sizeof(dhcp_raw_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
745 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
746
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
747 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
748 dbg("SEND RAW: socket failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
749 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
750 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
751 memset(&dest_sll, 0, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
752 dest_sll.sll_family = AF_PACKET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
753 dest_sll.sll_protocol = htons(ETH_P_IP);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
754 dest_sll.sll_ifindex = state->ifindex;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
755 dest_sll.sll_halen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
756 memcpy(dest_sll.sll_addr, bmacaddr , 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
757
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
758 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
759 dbg("SEND RAW: bind failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
760 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
761 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
762 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
763 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
764 packet.iph.protocol = IPPROTO_UDP;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
765 packet.iph.saddr = INADDR_ANY;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
766 packet.iph.daddr = INADDR_BROADCAST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
767 packet.udph.source = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
768 packet.udph.dest = htons(DHCPC_SERVER_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
769 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
770 packet.iph.tot_len = packet.udph.len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
771 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
772 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
773 packet.iph.ihl = sizeof(packet.iph) >> 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
774 packet.iph.version = IPVERSION;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
775 packet.iph.ttl = IPDEFTTL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
776 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
777
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
778 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
779 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
780
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
781 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
782 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
783 return result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
784 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
785
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
786 // Sends data through UDP socket.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
787 static int send_app(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
788 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
789 struct sockaddr_in cli;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
790 int fd, ret = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
791
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
792 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
793 dbg("SEND APP: sock failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
794 return ret;
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 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
797
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
798 memset(&cli, 0, sizeof(cli));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
799 cli.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
800 cli.sin_port = htons(DHCPC_CLIENT_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
801 cli.sin_addr.s_addr = state->pdhcp.ciaddr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
802 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
803 dbg("SEND APP: bind failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
804 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
805 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
806 memset(&cli, 0, sizeof(cli));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
807 cli.sin_family = AF_INET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
808 cli.sin_port = htons(DHCPC_SERVER_PORT);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
809 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
810 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
811 dbg("SEND APP: connect failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
812 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
813 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
814 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
815 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
816 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
817 goto error_fd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
818 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
819 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
820 error_fd:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
821 close(fd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
822 return ret;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
823 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
824
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
825 // 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
826 static void signal_handler(int sig)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
827 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
828 unsigned char ch = sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
829 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
830 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
831
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
832 // signal setup for SIGUSR1 SIGUSR2 SIGTERM
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
833 static int setup_signal()
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
834 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
835 if (pipe((int *)&sigfd) < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
836 dbg("signal pipe failed\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
837 return -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
838 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
839 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
840 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
841 int flags = fcntl(sigfd.wr, F_GETFL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
842 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
843 signal(SIGUSR1, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
844 signal(SIGUSR2, signal_handler);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
845 signal(SIGTERM, signal_handler);
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 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
848 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
849
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
850 // adds client id to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
851 static uint8_t *dhcpc_addclientid(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
852 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
853 *optptr++ = DHCP_OPTION_CLIENTID;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
854 *optptr++ = 7;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
855 *optptr++ = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
856 memcpy(optptr, &state->macaddr, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
857 return optptr + 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
858 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
859
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
860 // adds messege type to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
861 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
862 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
863 *optptr++ = DHCP_OPTION_MSG_TYPE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
864 *optptr++ = 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
865 *optptr++ = type;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
866 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
867 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
868
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
869 // adds max size to dhcp packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
870 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
871 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
872 *optptr++ = DHCP_OPTION_MAX_SIZE;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
873 *optptr++ = 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
874 memcpy(optptr, &size, 2);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
875 return optptr + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
876 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
877
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
878 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
879 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
880 *optptr++ = opcode;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
881 *optptr++ = len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
882 memcpy(optptr, str, len);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
883 return optptr + len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
884 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
885
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
886 // adds server id to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
887 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
888 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
889 *optptr++ = DHCP_OPTION_SERVER_ID;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
890 *optptr++ = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
891 memcpy(optptr, &serverid->s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
892 return optptr + 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
893 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
894
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
895 // adds requested ip address to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
896 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
897 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
898 *optptr++ = DHCP_OPTION_REQ_IPADDR;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
899 *optptr++ = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
900 memcpy(optptr, &ipaddr->s_addr, 4);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
901 return optptr + 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
902 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
903
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
904 // adds hostname to dhcp packet.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
905 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
906 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
907 int size = strlen(hname);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
908 *optptr++ = DHCP_OPTION_FQDN;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
909 *optptr++ = size + 3;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
910 *optptr++ = 0x1; //flags
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
911 optptr += 2; // two blank bytes
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
912 strncpy((char*)optptr, hname, size); // name
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
913 return optptr + size;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
914 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
915
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
916 // 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
917 static uint8_t *dhcpc_addreqoptions(uint8_t *optptr)
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 uint8_t *len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
920
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
921 *optptr++ = DHCP_OPTION_REQ_LIST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
922 len = optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
923 *len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
924 optptr++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
925
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
926 if (!flag_chk(FLAG_o)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
927 *len = 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
928 *optptr++ = DHCP_OPTION_SUBNET_MASK;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
929 *optptr++ = DHCP_OPTION_ROUTER;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
930 *optptr++ = DHCP_OPTION_DNS_SERVER;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
931 *optptr++ = DHCP_OPTION_BROADCAST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
932 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
933 if (flag_chk(FLAG_O)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
934 memcpy(optptr++, raw_opt, raw_optcount);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
935 *len += raw_optcount;
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 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
938 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
939
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
940 static uint8_t *dhcpc_addend(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
941 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
942 *optptr++ = DHCP_OPTION_END;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
943 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
944 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
945
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
946 // 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
947 static uint8_t* set_xopt(uint8_t *optptr)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
948 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
949 int count;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
950 int size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
951 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
952 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
953 *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
954 *optptr++ = (uint8_t) options_list[count].len;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
955 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
956 optptr += options_list[count].len;
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 return optptr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
959 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
960
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
961 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
962 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
963 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
964 while (*opt != DHCP_OPTION_SERVER_ID) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
965 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
966 opt += opt[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
967 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
968 memcpy(&var, opt+2, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
969 state->serverid.s_addr = var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
970 presult->serverid.s_addr = state->serverid.s_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
971 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
972 return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
973 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
974
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
975 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
976 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
977 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
978 while (*opt != DHCP_OPTION_MSG_TYPE) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
979 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
980 opt += opt[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
981 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
982 memcpy(&var, opt+2, sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
983 return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
984 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
985
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
986 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
987 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
988 uint32_t var = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
989 while (*opt != DHCP_OPTION_LEASE_TIME) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
990 if (*opt == DHCP_OPTION_END) return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
991 opt += opt[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
992 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
993 memcpy(&var, opt+2, sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
994 var = htonl(var);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
995 presult->lease_time = var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
996 return var;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
997 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
998
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
999
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1000 // sends dhcp msg of MSGTYPE
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1001 static int dhcpc_sendmsg(int msgtype)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1002 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1003 uint8_t *pend;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1004 struct in_addr rqsd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1005 char *vendor;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1006
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1007 // Create the common message header settings
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1008 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1009 state->pdhcp.op = DHCP_REQUEST;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1010 state->pdhcp.htype = DHCP_HTYPE_ETHERNET;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1011 state->pdhcp.hlen = 6;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1012 state->pdhcp.xid = xid;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1013 memcpy(state->pdhcp.chaddr, state->macaddr, 6);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1014 memset(&state->pdhcp.chaddr[6], 0, 10);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1015 state->pdhcp.cookie = htonl(DHCP_MAGIC);;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1016
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1017 // Add the common header options
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1018 pend = state->pdhcp.options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1019 pend = dhcpc_addmsgtype(pend, msgtype);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1020
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1021 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
1022 // Handle the message specific settings
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1023 switch (msgtype) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1024 case DHCPDISCOVER: // Broadcast DISCOVER message to all servers
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1025 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1026 if (flag_chk(FLAG_r)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1027 inet_aton(TT.req_ip, &rqsd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1028 pend = dhcpc_addreqipaddr(&rqsd, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1029 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1030 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
1031 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
1032 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
1033 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
1034 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
1035 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
1036 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
1037 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1038 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
1039 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1040 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
1041 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
1042 rqsd.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1043 pend = dhcpc_addserverid(&rqsd, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1044 pend = dhcpc_addreqipaddr(&state->ipaddr, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1045 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
1046 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
1047 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
1048 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
1049 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
1050 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
1051 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1052 case DHCPRELEASE: // Send RELEASE message to the server.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1053 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
1054 rqsd.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1055 pend = dhcpc_addserverid(&rqsd, pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1056 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1057 default:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1058 return -1;
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 pend = dhcpc_addend(pend);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1061
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1062 if (state->mode == MODE_APP) return send_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1063 return send_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1064 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1065
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1066 /*
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1067 * 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
1068 * stores result in PRESULT or MSGOPT_LIST
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 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
1071 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1072 uint8_t type = 0, *options, overloaded = 0;;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1073 uint16_t flag = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1074 uint32_t convtmp = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1075 char *dest, *pfx;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1076 struct in_addr addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1077 int count, optlen, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1078
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1079 if (flag_chk(FLAG_x)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1080 if(msgopt_list){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1081 for (count = 0; count < size; count++){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1082 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
1083 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1084 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1085 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1086 } else {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1087 msgopt_list = xmalloc(sizeof(options_list));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1088 memcpy(msgopt_list, options_list, sizeof(options_list));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1089 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1090 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1091 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1092 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1093 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1094 } else {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1095 msgopt_list = options_list;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1096 for (count = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1097 msgopt_list[count].len = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1098 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
1099 msgopt_list[count].val = NULL;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1100 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1101 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1102
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1103 while (*optptr != DHCP_OPTION_END) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1104 while (*optptr == DHCP_OPTION_PADDING) optptr++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1105 if (*optptr == DHCP_OPTION_OVERLOAD) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1106 overloaded = optptr[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1107 optptr += optptr[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1108 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1109 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1110 for (count = 0, flag = 0; count < size; count++) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1111 if ((msgopt_list[count].code & 0X00FF) == *optptr) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1112 flag = (msgopt_list[count].code & 0XFF00);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1113 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1114 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1115 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1116 switch (flag) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1117 case DHCP_NUM32:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1118 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1119 convtmp = htonl(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1120 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1121 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1122 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1123 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1124 case DHCP_NUM16:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1125 memcpy(&convtmp, &optptr[2], sizeof(uint16_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1126 convtmp = htons(convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1127 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1128 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1129 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1130 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1131 case DHCP_NUM8:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1132 memcpy(&convtmp, &optptr[2], sizeof(uint8_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1133 sprintf(toybuf, "%u", convtmp);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1134 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1135 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1136 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1137 case DHCP_IP:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1138 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1139 addr.s_addr = convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1140 sprintf(toybuf, "%s", inet_ntoa(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1141 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1142 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1143 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1144 case DHCP_STRING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1145 sprintf(toybuf, "%.*s", optptr[1], &optptr[2]);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1146 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1147 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1148 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1149 case DHCP_IPLIST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1150 optlen = optptr[1];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1151 dest = toybuf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1152 while (optlen) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1153 memcpy(&convtmp, &optptr[2], sizeof(uint32_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1154 addr.s_addr = convtmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1155 dest += sprintf(dest, "%s ", inet_ntoa(addr));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1156 optlen -= 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1157 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1158 *(dest - 1) = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1159 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1160 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1161 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1162 case DHCP_STRLST: //FIXME: do smthing.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1163 case DHCP_IPPLST:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1164 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1165 case DHCP_STCRTS:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1166 pfx = "";
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1167 dest = toybuf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1168 options = &optptr[2];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1169 optlen = optptr[1];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1170
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1171 while (optlen >= 1 + 4) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1172 uint32_t nip = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1173 int bytes;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1174 uint8_t *p_tmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1175 unsigned mask = *options;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1176
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1177 if (mask > 32) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1178 optlen--;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1179 p_tmp = (void*) &nip;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1180 bytes = (mask + 7) / 8;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1181 while (--bytes >= 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1182 *p_tmp++ = *options++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1183 optlen--;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1184 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1185 if (optlen < 4) break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1186 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
1187 ((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
1188 pfx = " ";
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1189 dest += sprintf(dest, "/%u ", mask);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1190 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
1191 options += 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1192 optlen -= 4;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1193 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1194 msgopt_list[count].val = strdup(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1195 msgopt_list[count].len = strlen(toybuf);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1196 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1197 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1198 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1199 optptr += optptr[1] + 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1200 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1201 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
1202 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
1203 return type;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1204 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1205
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1206 // 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
1207 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
1208 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1209 if (state->pdhcp.op == DHCP_REPLY
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1210 && !memcmp(state->pdhcp.chaddr, state->macaddr, 6)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1211 && !memcmp(&state->pdhcp.xid, &xid, sizeof(xid))) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1212 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
1213 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
1214 return get_option_msgtype(state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1215 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1216 return 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1217 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1218
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1219 // Sends a IP renew request.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1220 static void renew(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1221 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1222 infomsg(infomode, "Performing a DHCP renew");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1223 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1224 case STATE_INIT:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1225 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1226 case STATE_BOUND:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1227 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1228 case STATE_RENEWING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1229 case STATE_REBINDING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1230 state->status = STATE_RENEW_REQUESTED;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1231 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1232 case STATE_RENEW_REQUESTED:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1233 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1234 case STATE_REQUESTING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1235 case STATE_RELEASED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1236 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1237 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1238 break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1239 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1240 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1241 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1242
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1243 // Sends a IP release request.
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1244 static void release(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1245 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1246 int len = sizeof("255.255.255.255\0");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1247 char buffer[len];
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1248 struct in_addr temp_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1249
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1250 mode_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1251 // send release packet
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1252 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
1253 temp_addr.s_addr = htonl(server);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1254 strncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1255 buffer[len - 1] = '\0';
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1256 temp_addr.s_addr = state->ipaddr.s_addr;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1257 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
1258 dhcpc_sendmsg(DHCPRELEASE);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1259 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1260 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1261 infomsg(infomode, "Entering released state");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1262 close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1263 state->sockfd = -1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1264 state->mode = MODE_OFF;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1265 state->status = STATE_RELEASED;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1266 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1267
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1268 static void free_option_stores(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1269 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1270 int count, size = ARRAY_LEN(options_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1271 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1272 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
1273 if(flag_chk(FLAG_x)){
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1274 for (count = 0; count < size; count++)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1275 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
1276 free(msgopt_list);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1277 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1278 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1279
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1280 void dhcp_main(void)
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1281 {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1282 struct timeval tv;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1283 int retval, bufflen = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1284 dhcpc_result_t result;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1285 uint8_t packets = 0, retries = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1286 uint32_t timeout = 0, waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1287 fd_set rfds;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1288
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1289 xid = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1290 setlinebuf(stdout);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1291 dbg = dummy;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1292 if (flag_chk(FLAG_v)) dbg = xprintf;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1293 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
1294 retries = flag_get(FLAG_t, TT.retries, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1295 if (flag_chk(FLAG_S)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1296 openlog("UDHCPC :", LOG_PID, LOG_DAEMON);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1297 infomode |= LOG_SYSTEM;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1298 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1299 infomsg(infomode, "dhcp started");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1300 if (flag_chk(FLAG_O)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1301 while (TT.req_opt) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1302 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
1303 raw_optcount++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1304 TT.req_opt = TT.req_opt->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1305 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1306 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1307 if (flag_chk(FLAG_x)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1308 while (TT.pkt_opt) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1309 (void) strtoopt(TT.pkt_opt->arg, 0);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1310 TT.pkt_opt = TT.pkt_opt->next;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1311 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1312 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1313 memset(&result, 0, sizeof(dhcpc_result_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1314 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
1315 memset(state, 0, sizeof(dhcpc_state_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1316 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
1317
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1318 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
1319 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
1320
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1321 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1322 setup_signal();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1323 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1324 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1325 fcntl(state->sockfd, F_SETFD, FD_CLOEXEC);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1326
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1327 for (;;) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1328 FD_ZERO(&rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1329 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
1330 FD_SET(sigfd.rd, &rfds);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1331 tv.tv_sec = timeout - waited;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1332 tv.tv_usec = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1333 retval = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1334
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1335 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
1336 dbg("select wait ....\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1337 uint32_t timestmp = time(NULL);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1338 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
1339 if (errno == EINTR) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1340 waited += (unsigned) time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1341 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1342 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1343 perror_exit("Error in select");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1344 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1345 if (!retval) { // Timed out
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1346 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
1347 error_exit("Interface lost %s\n", state->iface);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1348
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1349 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1350 case STATE_INIT:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1351 if (packets < retries) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1352 if (!packets) xid = getxid();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1353 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1354 infomsg(infomode, "Sending discover...");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1355 dhcpc_sendmsg(DHCPDISCOVER);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1356 server = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1357 timeout = flag_get(FLAG_T, TT.timeout, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1358 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1359 packets++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1360 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1361 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1362 lease_fail:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1363 run_script(NULL,"leasefail");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1364 if (flag_chk(FLAG_n)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1365 infomsg(infomode, "Lease failed. Exiting");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1366 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1367 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1368 if (flag_chk(FLAG_b)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1369 infomsg(infomode, "Lease failed. Going Daemon mode");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1370 dhcp_daemon();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1371 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
1372 toys.optflags &= ~FLAG_b;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1373 toys.optflags |= FLAG_f;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1374 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1375 timeout = flag_get(FLAG_A, TT.tryagain, 20);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1376 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1377 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1378 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1379 case STATE_REQUESTING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1380 if (packets < retries) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1381 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
1382 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1383 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
1384 (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
1385 timeout = flag_get(FLAG_T, TT.timeout, 3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1386 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1387 packets++;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1388 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1389 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1390 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1391 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1392 goto lease_fail;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1393 case STATE_BOUND:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1394 state->status = STATE_RENEWING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1395 dbg("Entering renew state\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1396 // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1397 case STATE_RENEW_REQUESTED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1398 case STATE_RENEWING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1399 renew_requested:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1400 if (timeout > 60) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1401 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1402 timeout >>= 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1403 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1404 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1405 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1406 dbg("Entering rebinding state\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1407 state->status = STATE_REBINDING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1408 // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1409 case STATE_REBINDING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1410 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1411 if (timeout > 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1412 dhcpc_sendmsg(DHCPREQUEST);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1413 timeout >>= 1;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1414 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1415 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1416 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1417 infomsg(infomode, "Lease lost, entering INIT state");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1418 run_script(NULL, "deconfig");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1419 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1420 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1421 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1422 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1423 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1424 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1425 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1426 timeout = INT_MAX;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1427 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1428 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1429 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1430 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
1431 unsigned char sig;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1432 if (read(sigfd.rd, &sig, 1) != 1) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1433 dbg("signal read failed.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1434 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1435 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1436 switch (sig) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1437 case SIGUSR1:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1438 infomsg(infomode, "Received SIGUSR1");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1439 renew();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1440 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1441 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1442 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
1443 if (state->status == STATE_INIT) timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1444 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1445 case SIGUSR2:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1446 infomsg(infomode, "Received SIGUSR2");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1447 release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1448 timeout = INT_MAX;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1449 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1450 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1451 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1452 case SIGTERM:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1453 infomsg(infomode, "Received SIGTERM");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1454 if (flag_chk(FLAG_R)) release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1455 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1456 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1457 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1458 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1459 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
1460 dbg("main sock read\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1461 uint8_t msgType;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1462 if (state->mode == MODE_RAW) bufflen = read_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1463 if (state->mode == MODE_APP) bufflen = read_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1464 if (bufflen < 0) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1465 if (state->mode == MODE_RAW) mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1466 if (state->mode == MODE_APP) mode_app();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1467 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1468 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1469 waited += time(NULL) - timestmp;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1470 memset(&result, 0, sizeof(dhcpc_result_t));
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1471 msgType = dhcpc_parsemsg(&result);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1472 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
1473 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
1474 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
1475 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
1476 dhcpc_parseoptions(&result, state->pdhcp.options);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1477 get_option_lease(state->pdhcp.options, &result);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1478
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1479 switch (state->status) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1480 case STATE_INIT:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1481 if (msgType == DHCPOFFER) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1482 state->status = STATE_REQUESTING;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1483 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1484 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1485 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1486 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1487 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1488 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1489 case STATE_REQUESTING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1490 case STATE_RENEWING: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1491 case STATE_RENEW_REQUESTED: // FALLTHROUGH
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1492 case STATE_REBINDING:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1493 if (msgType == DHCPACK) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1494 timeout = result.lease_time / 2;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1495 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
1496 state->status = STATE_BOUND;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1497 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
1498 (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
1499 result.lease_time,
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1500 (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
1501 if (flag_chk(FLAG_q)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1502 if (flag_chk(FLAG_R)) release();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1503 goto ret_with_sockfd;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1504 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1505 toys.optflags &= ~FLAG_n;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1506 if (!flag_chk(FLAG_f)) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1507 dhcp_daemon();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1508 toys.optflags |= FLAG_f;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1509 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
1510 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1511 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1512 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1513 } else if (msgType == DHCPNAK) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1514 dbg("NACK received.\n");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1515 run_script(&result, "nak");
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1516 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
1517 mode_raw();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1518 sleep(3);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1519 state->status = STATE_INIT;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1520 state->ipaddr.s_addr = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1521 server = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1522 timeout = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1523 packets = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1524 waited = 0;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1525 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1526 continue;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1527 default: break;
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1528 }
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 ret_with_sockfd:
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1532 if (CFG_TOYBOX_FREE) {
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1533 free_option_stores();
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1534 if (state->sockfd > 0) close(state->sockfd);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1535 free(state);
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1536 }
03f72b57a092 DHCP client and server, from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1537 }