Stan Chesnutt | 7da02ad | 2010-12-14 09:49:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.dhcp; |
| 18 | |
| 19 | import java.net.InetAddress; |
| 20 | import java.net.Inet4Address; |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * This class implements the DHCP-OFFER packet. |
| 26 | */ |
| 27 | class DhcpOfferPacket extends DhcpPacket { |
| 28 | /** |
| 29 | * The IP address of the server which sent this packet. |
| 30 | */ |
| 31 | private final InetAddress mSrcIp; |
| 32 | |
| 33 | /** |
| 34 | * Generates a OFFER packet with the specified parameters. |
| 35 | */ |
| 36 | DhcpOfferPacket(int transId, boolean broadcast, InetAddress serverAddress, |
| 37 | InetAddress clientIp, byte[] clientMac) { |
| 38 | super(transId, Inet4Address.ANY, clientIp, Inet4Address.ANY, |
| 39 | Inet4Address.ANY, clientMac, broadcast); |
| 40 | mSrcIp = serverAddress; |
| 41 | } |
| 42 | |
| 43 | public String toString() { |
| 44 | String s = super.toString(); |
| 45 | String dnsServers = ", DNS servers: "; |
| 46 | |
| 47 | if (mDnsServers != null) { |
| 48 | for (InetAddress dnsServer: mDnsServers) { |
| 49 | dnsServers += dnsServer + " "; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return s + " OFFER, ip " + mYourIp + ", mask " + mSubnetMask + |
| 54 | dnsServers + ", gateway " + mGateway + |
| 55 | " lease time " + mLeaseTime + ", domain " + mDomainName; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Fills in a packet with the specified OFFER attributes. |
| 60 | */ |
| 61 | public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { |
| 62 | ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); |
| 63 | InetAddress destIp = mBroadcast ? Inet4Address.ALL : mYourIp; |
| 64 | InetAddress srcIp = mBroadcast ? Inet4Address.ANY : mSrcIp; |
| 65 | |
| 66 | fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result, |
| 67 | DHCP_BOOTREPLY, mBroadcast); |
| 68 | result.flip(); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Adds the optional parameters to the server-generated OFFER packet. |
| 74 | */ |
| 75 | void finishPacket(ByteBuffer buffer) { |
| 76 | addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_OFFER); |
| 77 | addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); |
| 78 | addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime); |
| 79 | |
| 80 | // the client should renew at 1/2 the lease-expiry interval |
| 81 | if (mLeaseTime != null) { |
| 82 | addTlv(buffer, DHCP_RENEWAL_TIME, |
| 83 | Integer.valueOf(mLeaseTime.intValue() / 2)); |
| 84 | } |
| 85 | |
| 86 | addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask); |
| 87 | addTlv(buffer, DHCP_ROUTER, mGateway); |
| 88 | addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName); |
| 89 | addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress); |
| 90 | addTlv(buffer, DHCP_DNS_SERVER, mDnsServers); |
| 91 | addTlvEnd(buffer); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Notifies the state machine of the OFFER packet parameters. |
| 96 | */ |
| 97 | public void doNextOp(DhcpStateMachine machine) { |
| 98 | machine.onOfferReceived(mBroadcast, mTransId, mClientMac, mYourIp, |
| 99 | mServerIdentifier); |
| 100 | } |
| 101 | } |