|         |      1 /* | 
|         |      2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). | 
|         |      3 * All rights reserved. | 
|         |      4 * This component and the accompanying materials are made available | 
|         |      5 * under the terms of "Eclipse Public License v1.0" | 
|         |      6 * which accompanies this distribution, and is available | 
|         |      7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". | 
|         |      8 * | 
|         |      9 * Initial Contributors: | 
|         |     10 * Nokia Corporation - initial contribution. | 
|         |     11 * | 
|         |     12 * Contributors: | 
|         |     13 * | 
|         |     14 * Description:  Builds the dhcp location information request buffer | 
|         |     15 * | 
|         |     16 */ | 
|         |     17  | 
|         |     18  | 
|         |     19 #include <e32math.h> | 
|         |     20 #include "dhcppsylogging.h"             // For logging | 
|         |     21 #include "dhcpconstants.h" | 
|         |     22 #include "dhcpdatalocationrequest.h" | 
|         |     23  | 
|         |     24 const TInt KDhcpFileBufferLength = 0x80;        // 128dec | 
|         |     25 const TInt KDhcpSnameLength = 0x40; | 
|         |     26  | 
|         |     27 const TInt KDhcpChAddressLength = 0x10;   // 16dec | 
|         |     28  | 
|         |     29 // Data positions constants. | 
|         |     30 const TInt KDhcpMessageOpCodePos = 0x00; | 
|         |     31 const TInt KDhcpHardwareAddressTypePos = 0x01; | 
|         |     32 const TInt KDhcpHardwareAddressLengthPos = 0x02; | 
|         |     33 const TInt KDhcpHopsRelayPos = 0x03; | 
|         |     34 const TInt KDhcpTransActionIdPos = 0x04;     // 4 byte | 
|         |     35  | 
|         |     36 const TInt KDhcpSecsPos = 0x08; | 
|         |     37 const TInt KDhcpFlagsPos = 0x0A; | 
|         |     38 const TInt KDhcpCiAddrPos = 0x0C; | 
|         |     39 const TInt KDhcpYiAddrPos = 0x10; | 
|         |     40 const TInt KDhcpSiAddrPos = 0x14; | 
|         |     41 const TInt KDhcpGiAddrPos = 0x18; | 
|         |     42  | 
|         |     43 // Operation code constans. | 
|         |     44 const TUint8    KDhcpMessageOperationCode = 0x01;       // 1 = BOOTREQUEST | 
|         |     45 const TUint8    KDhcpHardwareAddressType = 0x01;        // HType 1 = 10 mb ethernet | 
|         |     46 const TUint8    KDhcpHardwareAddressLength = 0x06;      // Hardware address length, | 
|         |     47                                                         // 6 bytes 10 mb ethernet | 
|         |     48 const TUint8    KDhcpHopsRelayInform = 0x00;            // Hops 0 in DHCPINFORM | 
|         |     49  | 
|         |     50  | 
|         |     51 // ======== MEMBER FUNCTIONS ======== | 
|         |     52  | 
|         |     53 // --------------------------------------------------------------------------- | 
|         |     54 // TDhcpLocationRequestPacket | 
|         |     55 // --------------------------------------------------------------------------- | 
|         |     56 // | 
|         |     57 TDhcpLocationRequestPacket::TDhcpLocationRequestPacket() | 
|         |     58     { | 
|         |     59     iXid = 0; | 
|         |     60     } | 
|         |     61  | 
|         |     62 // --------------------------------------------------------------------------- | 
|         |     63 // Return used transaction id. | 
|         |     64 // --------------------------------------------------------------------------- | 
|         |     65 // | 
|         |     66 TUint32 TDhcpLocationRequestPacket::TransactionId () | 
|         |     67     { | 
|         |     68     return iXid; | 
|         |     69     } | 
|         |     70  | 
|         |     71 // --------------------------------------------------------------------------- | 
|         |     72 // Constructs a DHCP Inform message | 
|         |     73 // --------------------------------------------------------------------------- | 
|         |     74 // | 
|         |     75 void TDhcpLocationRequestPacket::MakeDhcpInformMsg ( TUint32 aLocalIp, | 
|         |     76     const TDesC8& aMacAddr ) | 
|         |     77     { | 
|         |     78     TRACESTRING( "TDhcpLocationRequestPacket::MakeDhcpInform" ); | 
|         |     79     RandomiseXid (); | 
|         |     80  | 
|         |     81     // Inital part, we do byte work at here. | 
|         |     82     SetLength (0x1C); | 
|         |     83     FillZ(); | 
|         |     84  | 
|         |     85     iBuffer[KDhcpMessageOpCodePos] = KDhcpMessageOperationCode; | 
|         |     86     iBuffer[KDhcpHardwareAddressTypePos] = KDhcpHardwareAddressType; | 
|         |     87     // We could also get value in here from actual length of | 
|         |     88     // mac address buffer. | 
|         |     89     iBuffer[KDhcpHardwareAddressLengthPos] = KDhcpHardwareAddressLength; | 
|         |     90     iBuffer[KDhcpHopsRelayPos] = KDhcpHopsRelayInform; | 
|         |     91  | 
|         |     92     BigEndian::Put32(&(iBuffer[KDhcpTransActionIdPos]), TUint32 (iXid)); | 
|         |     93  | 
|         |     94     // Set secs and flags fields to the buffer. | 
|         |     95     TUint16 secsAndFlagsValue = 0;      // 0 in flags = no broadcast | 
|         |     96     BigEndian::Put16(&(iBuffer[KDhcpSecsPos]), TUint16 (secsAndFlagsValue)); | 
|         |     97     BigEndian::Put16(&(iBuffer[KDhcpFlagsPos]), TUint16 (secsAndFlagsValue)); | 
|         |     98  | 
|         |     99     // Local ip address | 
|         |    100     BigEndian::Put32(&(iBuffer[KDhcpCiAddrPos]), TUint32 (aLocalIp)); | 
|         |    101     TUint32 xiAddrValue = 0x00; | 
|         |    102     BigEndian::Put32(&(iBuffer[KDhcpYiAddrPos]), TUint32 (xiAddrValue)); | 
|         |    103     BigEndian::Put32(&(iBuffer[KDhcpSiAddrPos]), TUint32 (xiAddrValue)); | 
|         |    104     BigEndian::Put32(&(iBuffer[KDhcpGiAddrPos]), TUint32 (xiAddrValue)); | 
|         |    105  | 
|         |    106     // wlan mac address | 
|         |    107     TBuf8<KDhcpChAddressLength>  chAddress (KNullDesC8); | 
|         |    108     chAddress.Copy (aMacAddr.Left(KDhcpChAddressLength)); | 
|         |    109     if (chAddress.Length()<KDhcpChAddressLength) | 
|         |    110         { | 
|         |    111         // If given mac address is less max of field, | 
|         |    112         // we have make sure that rest of bytes | 
|         |    113         // are filled as zero-byte. | 
|         |    114         TInt oldLength = chAddress.Length(); | 
|         |    115         chAddress.SetLength(KDhcpChAddressLength); | 
|         |    116         for (TInt ii = oldLength; ii<KDhcpChAddressLength;ii++) | 
|         |    117             { | 
|         |    118             chAddress[ii] = 0x00; | 
|         |    119             } | 
|         |    120         } | 
|         |    121  | 
|         |    122     Append(chAddress); | 
|         |    123  | 
|         |    124     TBuf8<KDhcpFileBufferLength> fileBuffer(KNullDesC8); | 
|         |    125     // sname | 
|         |    126     fileBuffer.SetLength(KDhcpSnameLength); | 
|         |    127     fileBuffer.FillZ(); | 
|         |    128     Append(fileBuffer); | 
|         |    129  | 
|         |    130     // file | 
|         |    131     fileBuffer.SetLength(KDhcpFileBufferLength); | 
|         |    132     fileBuffer.FillZ(); | 
|         |    133     Append(fileBuffer); | 
|         |    134  | 
|         |    135     // magic cookie | 
|         |    136     TPtrC8 ptr (KDhcpMagicCookie, sizeof(KDhcpMagicCookie)); | 
|         |    137     Append(ptr); | 
|         |    138  | 
|         |    139     // And add all rest parts to the request message | 
|         |    140     TPtrC8 ptrInform (KDhcpDhcpMsgInform, sizeof(KDhcpDhcpMsgInform)); | 
|         |    141     Append(ptrInform); | 
|         |    142  | 
|         |    143     TPtrC8 ptrRequestList (KDhcpDhcpParameterRequestList, sizeof(KDhcpDhcpParameterRequestList)); | 
|         |    144     Append(ptrRequestList); | 
|         |    145  | 
|         |    146     TPtrC8 ptrGeoOptionParams (KDhcpGeoOptionParams, sizeof(KDhcpGeoOptionParams)); | 
|         |    147     Append(ptrGeoOptionParams); | 
|         |    148  | 
|         |    149     TPtrC8 ptrMessageSize (KDhcpMessageSize, sizeof(KDhcpMessageSize)); | 
|         |    150     Append(ptrMessageSize); | 
|         |    151  | 
|         |    152     TPtrC8 ptrMsgSize (KDhcpEndOfMessage, sizeof(KDhcpEndOfMessage)); | 
|         |    153     Append(ptrMsgSize); | 
|         |    154  | 
|         |    155     TInt length = Length(); | 
|         |    156     SetLength(KDhcpPacketLength); | 
|         |    157  | 
|         |    158     // Ok, rest of bytes shall be zero. | 
|         |    159     TPtr8 ptrMakeZeroBytes(&iBuffer[length],KDhcpPacketLength - length); | 
|         |    160     ptrMakeZeroBytes.FillZ(); | 
|         |    161     } | 
|         |    162  | 
|         |    163 // --------------------------------------------------------------------------- | 
|         |    164 // Generate a random transaction id | 
|         |    165 // --------------------------------------------------------------------------- | 
|         |    166 // | 
|         |    167 void TDhcpLocationRequestPacket::RandomiseXid () | 
|         |    168     { | 
|         |    169     TRACESTRING( "TDhcpLocationRequestPacket::RandomiseXid" ); | 
|         |    170     TTime time; | 
|         |    171     time.HomeTime(); | 
|         |    172     TInt64 seed = time.Int64(); | 
|         |    173     TUint random = Math::Rand( seed ); | 
|         |    174     iXid = random % KMaxTUint; | 
|         |    175     TRACESTRING2( "TDhcpLocationRequestPacket::RandomiseXid, iXid %u" ,iXid ); | 
|         |    176     } |