annotate toys/pending/dhcp.c @ 1396:e0c9c5424864 draft

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