~ubuntu-branches/ubuntu/trusty/dhcpcd/trusty-security

« back to all changes in this revision

Viewing changes to src/buildmsg.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Kelley
  • Date: 2005-09-30 02:21:51 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050930022151-vq3xlcazj0bdpyf4
Tags: 1:2.0.0-2
Clear out /etc/dhcpc/resolv.conf and /var/lib/dhcpc/* 
during purge. (closes: #330515)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * dhcpcd - DHCP client daemon -
 
3
 * Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
 
4
 * Copyright (C) January, 1998 Sergei Viznyuk <sv@phystech.com>
 
5
 * Copyright (C) 2005 Roy Marples <uberlord@gentoo.org>
 
6
 * Copyright (C) 2005 Simon Kelley <simon@thekelleys.org.uk>
 
7
 * 
 
8
 * dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
 
9
 *
 
10
 * This is free software; you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
18
 * See the GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
23
 */
 
24
 
 
25
#include <string.h>
 
26
#include <netinet/in.h>
 
27
#include <net/if_arp.h>
 
28
#include "client.h"
 
29
#include "udpipgen.h"
 
30
 
 
31
extern  dhcpMessage     *DhcpMsgSend;
 
32
extern  dhcpOptions     DhcpOptions;
 
33
extern  dhcpInterface   DhcpIface;
 
34
extern  char            *HostName;
 
35
extern  int             HostName_len;
 
36
extern  int             DebugFlag;
 
37
extern  int             BeRFC1541;
 
38
extern  unsigned        LeaseTime;
 
39
extern  int             TokenRingIf;
 
40
extern  unsigned char   ClientHwAddr[6];
 
41
extern  udpipMessage    UdpIpMsgSend;
 
42
extern  int             magic_cookie;
 
43
extern  unsigned short  dhcpMsgSize;
 
44
extern  unsigned        nleaseTime;
 
45
extern  int             BroadcastResp;
 
46
extern  struct in_addr  inform_ipaddr;
 
47
 
 
48
extern  int             SetFQDNHostName;
 
49
 
 
50
/*****************************************************************************/
 
51
void buildDhcpDiscover(xid)
 
52
unsigned xid;
 
53
{
 
54
  register unsigned char *p = DhcpMsgSend->options + 4;
 
55
 
 
56
/* build Ethernet header */
 
57
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
58
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
 
59
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
60
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
61
 
 
62
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
63
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
64
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
65
  DhcpMsgSend->xid      =       xid;
 
66
  DhcpMsgSend->secs     =       htons(10);
 
67
  if ( BroadcastResp )
 
68
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
69
#if 0
 
70
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
71
#else
 
72
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
73
#endif
 
74
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
75
  *p++ = dhcpMessageType;
 
76
  *p++ = 1;
 
77
  *p++ = DHCP_DISCOVER;
 
78
  *p++ = dhcpMaxMsgSize;
 
79
  *p++ = 2;
 
80
  memcpy(p,&dhcpMsgSize,2);
 
81
  p += 2;
 
82
  if ( DhcpIface.ciaddr )
 
83
    {
 
84
      if ( BeRFC1541 )
 
85
        DhcpMsgSend->ciaddr = DhcpIface.ciaddr;
 
86
      else
 
87
        {
 
88
          *p++ = dhcpRequestedIPaddr;
 
89
          *p++ = 4;
 
90
          memcpy(p,&DhcpIface.ciaddr,4);
 
91
          p += 4; 
 
92
        }
 
93
    }
 
94
  *p++ = dhcpIPaddrLeaseTime;
 
95
  *p++ = 4;
 
96
  memcpy(p,&nleaseTime,4);
 
97
  p += 4;
 
98
  *p++ = dhcpParamRequest;
 
99
  *p++ = 15;
 
100
  *p++ = subnetMask;
 
101
  *p++ = routersOnSubnet;
 
102
  *p++ = dns;
 
103
  *p++ = hostName;
 
104
  *p++ = domainName;
 
105
  *p++ = rootPath;
 
106
  *p++ = defaultIPTTL;
 
107
  *p++ = broadcastAddr;
 
108
  *p++ = performMaskDiscovery;
 
109
  *p++ = performRouterDiscovery;
 
110
  *p++ = staticRoute;
 
111
  *p++ = nisDomainName;
 
112
  *p++ = nisServers;
 
113
  *p++ = ntpServers;
 
114
  *p++ = dnsSearchPath;
 
115
  /* FQDN option (81) replaces HostName option (12) if requested */
 
116
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
117
    {
 
118
      *p++ = hostName;
 
119
      *p++ = HostName_len;
 
120
      memcpy(p,HostName,HostName_len);
 
121
      p += HostName_len;
 
122
    }
 
123
  *p++ = dhcpClassIdentifier;
 
124
  *p++ = DhcpIface.class_len;
 
125
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
126
  p += DhcpIface.class_len;
 
127
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
128
  p += DhcpIface.client_len;
 
129
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
130
    {
 
131
      /* Draft IETF DHC-FQDN option (81) */
 
132
      *p++ = dhcpFQDNHostName;
 
133
      *p++ = HostName_len + 3;
 
134
      /* Flags: 0000NEOS
 
135
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
136
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
137
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
138
       * N: 1 => Client requests Server to not update DNS
 
139
       */
 
140
      *p++ = SetFQDNHostName & 0x9;
 
141
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
142
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
143
      memcpy(p,HostName,HostName_len);
 
144
      p += HostName_len;
 
145
    }
 
146
  *p = endOption;
 
147
 
 
148
/* build UDP/IP header */
 
149
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,0,INADDR_BROADCAST,
 
150
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
151
}
 
152
/*****************************************************************************/
 
153
void buildDhcpRequest(xid)
 
154
unsigned xid;
 
155
{
 
156
  register unsigned char *p = DhcpMsgSend->options + 4;
 
157
 
 
158
/* build Ethernet header */
 
159
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
160
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
 
161
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
162
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
163
 
 
164
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
165
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
166
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
167
  DhcpMsgSend->xid      =       xid;
 
168
  DhcpMsgSend->secs     =       htons(10);
 
169
  if ( BroadcastResp )
 
170
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
171
#if 0
 
172
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
173
#else
 
174
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
175
#endif
 
176
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
177
  *p++ = dhcpMessageType;
 
178
  *p++ = 1;
 
179
  *p++ = DHCP_REQUEST;
 
180
  *p++ = dhcpMaxMsgSize;
 
181
  *p++ = 2;
 
182
  memcpy(p,&dhcpMsgSize,2);
 
183
  p += 2;
 
184
  *p++ = dhcpServerIdentifier;
 
185
  *p++ = 4;
 
186
  memcpy(p,DhcpOptions.val[dhcpServerIdentifier],4);
 
187
  p += 4;
 
188
  if ( BeRFC1541 )
 
189
    DhcpMsgSend->ciaddr = DhcpIface.ciaddr;
 
190
  else
 
191
    {
 
192
      *p++ = dhcpRequestedIPaddr;
 
193
      *p++ = 4;
 
194
      memcpy(p,&DhcpIface.ciaddr,4);
 
195
      p += 4;
 
196
    }
 
197
  if ( DhcpOptions.val[dhcpIPaddrLeaseTime] )
 
198
    {
 
199
      *p++ = dhcpIPaddrLeaseTime;
 
200
      *p++ = 4;
 
201
      memcpy(p,DhcpOptions.val[dhcpIPaddrLeaseTime],4);
 
202
      p += 4;
 
203
    }
 
204
  *p++ = dhcpParamRequest;
 
205
  *p++ = 15;
 
206
  *p++ = subnetMask;
 
207
  *p++ = routersOnSubnet;
 
208
  *p++ = dns;
 
209
  *p++ = hostName;
 
210
  *p++ = domainName;
 
211
  *p++ = rootPath;
 
212
  *p++ = defaultIPTTL;
 
213
  *p++ = broadcastAddr;
 
214
  *p++ = performMaskDiscovery;
 
215
  *p++ = performRouterDiscovery;
 
216
  *p++ = staticRoute;
 
217
  *p++ = nisDomainName;
 
218
  *p++ = nisServers;
 
219
  *p++ = ntpServers;
 
220
  *p++ = dnsSearchPath;
 
221
  /* FQDN option (81) replaces HostName option (12) if requested */
 
222
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
223
    {
 
224
      *p++ = hostName;
 
225
      *p++ = HostName_len;
 
226
      memcpy(p,HostName,HostName_len);
 
227
      p += HostName_len;
 
228
    }
 
229
  *p++ = dhcpClassIdentifier;
 
230
  *p++ = DhcpIface.class_len;
 
231
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
232
  p += DhcpIface.class_len;
 
233
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
234
  p += DhcpIface.client_len;
 
235
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
236
    {
 
237
      /* Draft IETF DHC-FQDN option (81) */
 
238
      *p++ = dhcpFQDNHostName;
 
239
      *p++ = HostName_len + 3;
 
240
      /* Flags: 0000NEOS
 
241
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
242
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
243
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
244
       * N: 1 => Client requests Server to not update DNS
 
245
       */
 
246
      *p++ = SetFQDNHostName & 0x9;
 
247
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
248
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
249
      memcpy(p,HostName,HostName_len);
 
250
      p += HostName_len;
 
251
    }
 
252
  *p = endOption;
 
253
 
 
254
/* build UDP/IP header */
 
255
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,0,INADDR_BROADCAST,
 
256
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
257
}
 
258
/*****************************************************************************/
 
259
void buildDhcpRenew(xid)
 
260
unsigned xid;
 
261
{
 
262
  register unsigned char *p = DhcpMsgSend->options + 4;
 
263
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
264
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,DhcpIface.shaddr,ETH_ALEN);
 
265
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
266
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
267
 
 
268
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
269
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
270
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
271
  DhcpMsgSend->xid      =       xid;
 
272
  DhcpMsgSend->secs     =       htons(10);
 
273
  if ( BroadcastResp )
 
274
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
275
  DhcpMsgSend->ciaddr   =       DhcpIface.ciaddr;
 
276
#if 0
 
277
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
278
#else
 
279
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
280
#endif
 
281
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
282
  *p++ = dhcpMessageType;
 
283
  *p++ = 1;
 
284
  *p++ = DHCP_REQUEST;
 
285
  *p++ = dhcpMaxMsgSize;
 
286
  *p++ = 2;
 
287
  memcpy(p,&dhcpMsgSize,2);
 
288
  p += 2;
 
289
#if 0
 
290
  if ( DhcpOptions.val[dhcpIPaddrLeaseTime] )
 
291
    {
 
292
      *p++ = dhcpIPaddrLeaseTime;
 
293
      *p++ = 4;
 
294
      memcpy(p,DhcpOptions.val[dhcpIPaddrLeaseTime],4);
 
295
      p += 4;
 
296
    }
 
297
#endif
 
298
  *p++ = dhcpParamRequest;
 
299
  *p++ = 15;
 
300
  *p++ = subnetMask;
 
301
  *p++ = routersOnSubnet;
 
302
  *p++ = dns;
 
303
  *p++ = hostName;
 
304
  *p++ = domainName;
 
305
  *p++ = rootPath;
 
306
  *p++ = defaultIPTTL;
 
307
  *p++ = broadcastAddr;
 
308
  *p++ = performMaskDiscovery;
 
309
  *p++ = performRouterDiscovery;
 
310
  *p++ = staticRoute;
 
311
  *p++ = nisDomainName;
 
312
  *p++ = nisServers;
 
313
  *p++ = ntpServers;
 
314
  *p++ = dnsSearchPath;
 
315
  /* FQDN option (81) replaces HostName option (12) if requested */
 
316
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
317
    {
 
318
      *p++ = hostName;
 
319
      *p++ = HostName_len;
 
320
      memcpy(p,HostName,HostName_len);
 
321
      p += HostName_len;
 
322
    }
 
323
  *p++ = dhcpClassIdentifier;
 
324
  *p++ = DhcpIface.class_len;
 
325
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
326
  p += DhcpIface.class_len;
 
327
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
328
  p += DhcpIface.client_len;
 
329
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
330
    {
 
331
      /* Draft IETF DHC-FQDN option (81) */
 
332
      *p++ = dhcpFQDNHostName;
 
333
      *p++ = HostName_len + 3;
 
334
      /* Flags: 0000NEOS
 
335
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
336
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
337
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
338
       * N: 1 => Client requests Server to not update DNS
 
339
       */
 
340
      *p++ = SetFQDNHostName & 0x9;
 
341
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
342
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
343
      memcpy(p,HostName,HostName_len);
 
344
      p += HostName_len;
 
345
    }
 
346
  *p = endOption;
 
347
 
 
348
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,
 
349
  DhcpIface.ciaddr,DhcpIface.siaddr,
 
350
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
351
}
 
352
/*****************************************************************************/
 
353
void buildDhcpRebind(xid)
 
354
unsigned xid;
 
355
{
 
356
  register unsigned char *p = DhcpMsgSend->options + 4;
 
357
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
358
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
 
359
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
360
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
361
 
 
362
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
363
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
364
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
365
  DhcpMsgSend->xid      =       xid;
 
366
  DhcpMsgSend->secs     =       htons(10);
 
367
  if ( BroadcastResp )
 
368
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
369
  DhcpMsgSend->ciaddr   =       DhcpIface.ciaddr;
 
370
#if 0
 
371
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
372
#else
 
373
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
374
#endif
 
375
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
376
  *p++ = dhcpMessageType;
 
377
  *p++ = 1;
 
378
  *p++ = DHCP_REQUEST;
 
379
  *p++ = dhcpMaxMsgSize;
 
380
  *p++ = 2;
 
381
  memcpy(p,&dhcpMsgSize,2);
 
382
  p += 2;
 
383
  if ( DhcpOptions.val[dhcpIPaddrLeaseTime] )
 
384
    {
 
385
      *p++ = dhcpIPaddrLeaseTime;
 
386
      *p++ = 4;
 
387
      memcpy(p,DhcpOptions.val[dhcpIPaddrLeaseTime],4);
 
388
      p += 4;
 
389
    }
 
390
  *p++ = dhcpParamRequest;
 
391
  *p++ = 15;
 
392
  *p++ = subnetMask;
 
393
  *p++ = routersOnSubnet;
 
394
  *p++ = dns;
 
395
  *p++ = hostName;
 
396
  *p++ = domainName;
 
397
  *p++ = rootPath;
 
398
  *p++ = defaultIPTTL;
 
399
  *p++ = broadcastAddr;
 
400
  *p++ = performMaskDiscovery;
 
401
  *p++ = performRouterDiscovery;
 
402
  *p++ = staticRoute;
 
403
  *p++ = nisDomainName;
 
404
  *p++ = nisServers;
 
405
  *p++ = ntpServers;
 
406
  *p++ = dnsSearchPath;
 
407
  /* FQDN option (81) replaces HostName option (12) if requested */
 
408
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
409
    {
 
410
      *p++ = hostName;
 
411
      *p++ = HostName_len;
 
412
      memcpy(p,HostName,HostName_len);
 
413
      p += HostName_len;
 
414
    }
 
415
  *p++ = dhcpClassIdentifier;
 
416
  *p++ = DhcpIface.class_len;
 
417
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
418
  p += DhcpIface.class_len;
 
419
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
420
  p += DhcpIface.client_len;
 
421
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
422
    {
 
423
      /* Draft IETF DHC-FQDN option (81) */
 
424
      *p++ = dhcpFQDNHostName;
 
425
      *p++ = HostName_len + 3;
 
426
      /* Flags: 0000NEOS
 
427
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
428
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
429
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
430
       * N: 1 => Client requests Server to not update DNS
 
431
       */
 
432
      *p++ = SetFQDNHostName & 0x9;
 
433
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
434
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
435
      memcpy(p,HostName,HostName_len);
 
436
      p += HostName_len;
 
437
    }
 
438
  *p = endOption;
 
439
 
 
440
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,
 
441
  DhcpIface.ciaddr,INADDR_BROADCAST,
 
442
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
443
}
 
444
/*****************************************************************************/
 
445
void buildDhcpReboot(xid)
 
446
unsigned xid;
 
447
{
 
448
  register unsigned char *p = DhcpMsgSend->options + 4;
 
449
 
 
450
/* build Ethernet header */
 
451
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
452
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
 
453
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
454
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
455
 
 
456
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
457
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
458
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
459
  DhcpMsgSend->xid      =       xid;
 
460
  DhcpMsgSend->secs     =       htons(10);
 
461
  if ( BroadcastResp )
 
462
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
463
#if 0
 
464
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
465
#else
 
466
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
467
#endif
 
468
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
469
 
 
470
  *p++ = dhcpMessageType;
 
471
  *p++ = 1;
 
472
  *p++ = DHCP_REQUEST;
 
473
  *p++ = dhcpMaxMsgSize;
 
474
  *p++ = 2;
 
475
  memcpy(p,&dhcpMsgSize,2);
 
476
  p += 2;
 
477
  if ( BeRFC1541 )
 
478
    DhcpMsgSend->ciaddr = DhcpIface.ciaddr;
 
479
  else
 
480
    {
 
481
      *p++ = dhcpRequestedIPaddr;
 
482
      *p++ = 4;
 
483
      memcpy(p,&DhcpIface.ciaddr,4);
 
484
      p += 4;
 
485
    }
 
486
  *p++ = dhcpIPaddrLeaseTime;
 
487
  *p++ = 4;
 
488
  memcpy(p,&nleaseTime,4);
 
489
  p += 4;
 
490
  *p++ = dhcpParamRequest;
 
491
  *p++ = 15;
 
492
  *p++ = subnetMask;
 
493
  *p++ = routersOnSubnet;
 
494
  *p++ = dns;
 
495
  *p++ = hostName;
 
496
  *p++ = domainName;
 
497
  *p++ = rootPath;
 
498
  *p++ = defaultIPTTL;
 
499
  *p++ = broadcastAddr;
 
500
  *p++ = performMaskDiscovery;
 
501
  *p++ = performRouterDiscovery;
 
502
  *p++ = staticRoute;
 
503
  *p++ = nisDomainName;
 
504
  *p++ = nisServers;
 
505
  *p++ = ntpServers;
 
506
  *p++ = dnsSearchPath;
 
507
  /* FQDN option (81) replaces HostName option (12) if requested */
 
508
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
509
    {
 
510
      *p++ = hostName;
 
511
      *p++ = HostName_len;
 
512
      memcpy(p,HostName,HostName_len);
 
513
      p += HostName_len;
 
514
    }
 
515
  *p++ = dhcpClassIdentifier;
 
516
  *p++ = DhcpIface.class_len;
 
517
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
518
  p += DhcpIface.class_len;
 
519
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
520
  p += DhcpIface.client_len;
 
521
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
522
    {
 
523
      /* Draft IETF DHC-FQDN option (81) */
 
524
      *p++ = dhcpFQDNHostName;
 
525
      *p++ = HostName_len + 3;
 
526
      /* Flags: 0000NEOS
 
527
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
528
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
529
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
530
       * N: 1 => Client requests Server to not update DNS
 
531
       */
 
532
      *p++ = SetFQDNHostName & 0x9;
 
533
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
534
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
535
      memcpy(p,HostName,HostName_len);
 
536
      p += HostName_len;
 
537
    }
 
538
  *p = endOption;
 
539
 
 
540
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,0,INADDR_BROADCAST,
 
541
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
542
}
 
543
/*****************************************************************************/
 
544
void buildDhcpRelease(xid)
 
545
unsigned xid;
 
546
{
 
547
  register unsigned char *p = DhcpMsgSend->options + 4;
 
548
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
549
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,DhcpIface.shaddr,ETH_ALEN);
 
550
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
551
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
552
 
 
553
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
554
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
555
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
556
  DhcpMsgSend->xid      =       xid;
 
557
  DhcpMsgSend->ciaddr   =       DhcpIface.ciaddr;
 
558
#if 0
 
559
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
560
#else
 
561
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
562
#endif
 
563
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
564
  *p++ = dhcpMessageType;
 
565
  *p++ = 1;
 
566
  *p++ = DHCP_RELEASE;
 
567
  *p++ = dhcpServerIdentifier;
 
568
  *p++ = 4;
 
569
  memcpy(p,DhcpOptions.val[dhcpServerIdentifier],4);
 
570
  p += 4;
 
571
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
572
  p += DhcpIface.client_len;
 
573
  *p = endOption;
 
574
 
 
575
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,DhcpIface.ciaddr,
 
576
  DhcpIface.siaddr,htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),
 
577
  sizeof(dhcpMessage));
 
578
}
 
579
/*****************************************************************************/
 
580
#ifdef ARPCHECK
 
581
void buildDhcpDecline(xid)
 
582
unsigned xid;
 
583
{
 
584
  register unsigned char *p = DhcpMsgSend->options + 4;
 
585
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
586
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,DhcpIface.shaddr,ETH_ALEN);
 
587
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
588
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
589
 
 
590
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
591
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
592
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
593
  DhcpMsgSend->xid      =       xid;
 
594
#if 0
 
595
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
596
#else
 
597
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
598
#endif
 
599
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
600
  *p++ = dhcpMessageType;
 
601
  *p++ = 1;
 
602
  *p++ = DHCP_DECLINE;
 
603
  *p++ = dhcpServerIdentifier;
 
604
  *p++ = 4;
 
605
  memcpy(p,DhcpOptions.val[dhcpServerIdentifier],4);
 
606
  p += 4;
 
607
  if ( BeRFC1541 )
 
608
    DhcpMsgSend->ciaddr = DhcpIface.ciaddr;
 
609
  else
 
610
    {
 
611
      *p++ = dhcpRequestedIPaddr;
 
612
      *p++ = 4;
 
613
      memcpy(p,&DhcpIface.ciaddr,4);
 
614
      p += 4;
 
615
    }
 
616
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
617
  p += DhcpIface.client_len;
 
618
  *p = endOption;
 
619
 
 
620
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,0,
 
621
  DhcpIface.siaddr,htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),
 
622
  sizeof(dhcpMessage));
 
623
}
 
624
#endif
 
625
/*****************************************************************************/
 
626
void buildDhcpInform(xid)
 
627
unsigned xid;
 
628
{
 
629
  register unsigned char *p = DhcpMsgSend->options + 4;
 
630
 
 
631
  memset(&UdpIpMsgSend,0,sizeof(udpipMessage));
 
632
  memcpy(UdpIpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
 
633
  memcpy(UdpIpMsgSend.ethhdr.ether_shost,ClientHwAddr,ETH_ALEN);
 
634
  UdpIpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_IP);
 
635
 
 
636
  DhcpMsgSend->op       =       DHCP_BOOTREQUEST;
 
637
  DhcpMsgSend->htype    =       (TokenRingIf) ? ARPHRD_IEEE802_TR : ARPHRD_ETHER;
 
638
  DhcpMsgSend->hlen     =       ETH_ALEN;
 
639
  DhcpMsgSend->xid      =       xid;
 
640
  DhcpMsgSend->secs     =       htons(10);
 
641
  if ( BroadcastResp )
 
642
    DhcpMsgSend->flags  =       htons(BROADCAST_FLAG);
 
643
#if 0
 
644
  memcpy(DhcpMsgSend->chaddr,DhcpIface.chaddr,ETH_ALEN);
 
645
#else
 
646
  memcpy(DhcpMsgSend->chaddr,ClientHwAddr,ETH_ALEN);
 
647
#endif
 
648
  DhcpMsgSend->ciaddr = inform_ipaddr.s_addr;
 
649
  memcpy(DhcpMsgSend->options,&magic_cookie,4);
 
650
  *p++ = dhcpMessageType;
 
651
  *p++ = 1;
 
652
  *p++ = DHCP_INFORM;
 
653
  *p++ = dhcpMaxMsgSize;
 
654
  *p++ = 2;
 
655
  memcpy(p,&dhcpMsgSize,2);
 
656
  p += 2;
 
657
  *p++ = dhcpParamRequest;
 
658
  *p++ = 15;
 
659
  *p++ = subnetMask;
 
660
  *p++ = routersOnSubnet;
 
661
  *p++ = dns;
 
662
  *p++ = hostName;
 
663
  *p++ = domainName;
 
664
  *p++ = rootPath;
 
665
  *p++ = defaultIPTTL;
 
666
  *p++ = broadcastAddr;
 
667
  *p++ = performMaskDiscovery;
 
668
  *p++ = performRouterDiscovery;
 
669
  *p++ = staticRoute;
 
670
  *p++ = nisDomainName;
 
671
  *p++ = nisServers;
 
672
  *p++ = ntpServers;
 
673
  *p++ = dnsSearchPath;
 
674
  /* FQDN option (81) replaces HostName option (12) if requested */
 
675
  if (( HostName ) && ( SetFQDNHostName == FQDNdisable ))
 
676
    {
 
677
      *p++ = hostName;
 
678
      *p++ = HostName_len;
 
679
      memcpy(p,HostName,HostName_len);
 
680
      p += HostName_len;
 
681
    }
 
682
  *p++ = dhcpClassIdentifier;
 
683
  *p++ = DhcpIface.class_len;
 
684
  memcpy(p,DhcpIface.class_id,DhcpIface.class_len);
 
685
  p += DhcpIface.class_len;
 
686
  memcpy(p,DhcpIface.client_id,DhcpIface.client_len);
 
687
  p += DhcpIface.client_len;
 
688
  if (( HostName ) && ( SetFQDNHostName != FQDNdisable ))
 
689
    {
 
690
      /* Draft IETF DHC-FQDN option (81) */
 
691
      *p++ = dhcpFQDNHostName;
 
692
      *p++ = HostName_len + 3;
 
693
      /* Flags: 0000NEOS
 
694
       * S: 1 => Client requests Server to update A RR in DNS as well as PTR
 
695
       * O: 1 => Server indicates to client that DNS has been updated regardless
 
696
       * E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
 
697
       * N: 1 => Client requests Server to not update DNS
 
698
       */
 
699
      *p++ = SetFQDNHostName & 0x9;
 
700
      *p++ = 0; /* rcode1, response from DNS server to DHCP for PTR RR */
 
701
      *p++ = 0; /* rcode2, response from DNS server to DHCP for A RR if S=1 */
 
702
      memcpy(p,HostName,HostName_len);
 
703
      p += HostName_len;
 
704
    }
 
705
  *p = endOption;
 
706
 
 
707
  udpipgen((udpiphdr *)UdpIpMsgSend.udpipmsg,0,INADDR_BROADCAST,
 
708
  htons(DHCP_CLIENT_PORT),htons(DHCP_SERVER_PORT),sizeof(dhcpMessage));
 
709
}