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; |
Stan Chesnutt | 7da02ad | 2010-12-14 09:49:26 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * This class implements the DHCP-ACK packet. |
| 25 | */ |
| 26 | class DhcpAckPacket extends DhcpPacket { |
| 27 | |
| 28 | /** |
| 29 | * The address of the server which sent this packet. |
| 30 | */ |
| 31 | private final InetAddress mSrcIp; |
| 32 | |
| 33 | DhcpAckPacket(int transId, boolean broadcast, InetAddress serverAddress, |
| 34 | InetAddress clientIp, byte[] clientMac) { |
Stan Chesnutt | eafccc0 | 2011-02-03 17:32:39 -0800 | [diff] [blame] | 35 | super(transId, Inet4Address.ANY, clientIp, serverAddress, |
Stan Chesnutt | 7da02ad | 2010-12-14 09:49:26 -0800 | [diff] [blame] | 36 | Inet4Address.ANY, clientMac, broadcast); |
| 37 | mBroadcast = broadcast; |
| 38 | mSrcIp = serverAddress; |
| 39 | } |
| 40 | |
| 41 | public String toString() { |
| 42 | String s = super.toString(); |
| 43 | String dnsServers = " DNS servers: "; |
| 44 | |
| 45 | for (InetAddress dnsServer: mDnsServers) { |
| 46 | dnsServers += dnsServer.toString() + " "; |
| 47 | } |
| 48 | |
| 49 | return s + " ACK: your new IP " + mYourIp + |
| 50 | ", netmask " + mSubnetMask + |
| 51 | ", gateway " + mGateway + dnsServers + |
| 52 | ", lease time " + mLeaseTime; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Fills in a packet with the requested ACK parameters. |
| 57 | */ |
| 58 | public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { |
| 59 | ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); |
| 60 | InetAddress destIp = mBroadcast ? Inet4Address.ALL : mYourIp; |
| 61 | InetAddress srcIp = mBroadcast ? Inet4Address.ANY : mSrcIp; |
| 62 | |
| 63 | fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result, |
| 64 | DHCP_BOOTREPLY, mBroadcast); |
| 65 | result.flip(); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Adds the optional parameters to the client-generated ACK packet. |
| 71 | */ |
| 72 | void finishPacket(ByteBuffer buffer) { |
| 73 | addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_ACK); |
| 74 | addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); |
| 75 | addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime); |
| 76 | |
| 77 | // the client should renew at 1/2 the lease-expiry interval |
| 78 | if (mLeaseTime != null) { |
| 79 | addTlv(buffer, DHCP_RENEWAL_TIME, |
| 80 | Integer.valueOf(mLeaseTime.intValue() / 2)); |
| 81 | } |
| 82 | |
| 83 | addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask); |
| 84 | addTlv(buffer, DHCP_ROUTER, mGateway); |
| 85 | addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName); |
| 86 | addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress); |
| 87 | addTlv(buffer, DHCP_DNS_SERVER, mDnsServers); |
| 88 | addTlvEnd(buffer); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Un-boxes an Integer, returning 0 if a null reference is supplied. |
| 93 | */ |
| 94 | private static final int getInt(Integer v) { |
| 95 | if (v == null) { |
| 96 | return 0; |
| 97 | } else { |
| 98 | return v.intValue(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Notifies the specified state machine of the ACK packet parameters. |
| 104 | */ |
| 105 | public void doNextOp(DhcpStateMachine machine) { |
| 106 | machine.onAckReceived(mYourIp, mSubnetMask, mGateway, mDnsServers, |
| 107 | mServerIdentifier, getInt(mLeaseTime)); |
| 108 | } |
| 109 | } |