Retrieving an IP Network Parameters Tutorial

How to retrieve IP network parameters from a DHCP server configured network connection.

Context

The high level steps to retrieve an IP network parameters are as follows:

Steps

  1. Open a socket using RSocketServer object.

  2. Create a connection using RConnection function.

  3. Start the connection using RConnection with necessary preferences.

    For example, enable DHCP server. The active connection is configured by the DHCP server.
  4. To retrieve the active network connection parameters, Call the RConnection::Ioctl() function by passing the relevant arguments.

  5. The RConnection::Ioctl() function returns the necessary parameters.

    Note: Multiple option codes can be queried to a DHCP server using the <api-item>TDhcpRawOptionMultipleDataPckg</api-item> object.

    The advantages of retrieving multiple parameters are:

    • It reduces IPC calls between application and the DHCP daemon.

    • It reduces the messages exchanged between the DHCP daemon and the DHCP server.

Retrieve IP Network Parameters example

The following is an example code for how to retrieve IP network parameters:


//create a socket connection  

RSocketServ esock; 
TInt err = eSock.Connect();
...

//handle the error
...
RConnection aConnection;   //create a RConnection 
err = aConnection.Open(esock);
...

//start the connection with necessary preferences
...
err = aConnection.Start(iConnPrefs);


//Retrieve the TFTP server address

TRequestStatus status1;
TTftpServerAddrBuf tftpserveraddr; // Buffer to store TFTP server address
tftpserveraddr().index = 0;
aConnection.Ioctl(KCOLConfiguration, KConnGetTftpServerAddr, status1, &tftpserveraddress);
User::WaitForRequest(status1);
...

//Retrieve the host name of the TFTP server

TRequestStatus status2;
TBuf<256> tftpname; //buffer to store TFTP server name
aConnection.Ioctl(KCOLConfiguration, KConnGetTftpServerName, status2, &tftpname);
User::WaitForRequest(status2);
...

//Retrieve SiAddr field from a DHCP header

TRequestStatus status3;
TConnectionAddrBuf addr;
addr().iAddressFamily = IpAddressFamily();
aConnection.Ioctl(KCOLConfiguration, KConnGetDhcpHdrSiaddr, status3, &addr);
...
//Retrieve SName field from a DHCP header

TRequestStatus status4;
TBuf<64> snamebuf;
aConnection.Ioctl(KCOLConfiguration, KConnGetDhcpHdrSname, status4, &snamebuf);
...
//Retrieve the active IP address

TRequestStatus status5;
TConnectionAddress actaddr;
actaddr().iAddressFamily = IpAddressFamily();
aConnection.Ioctl(KCOLConfiguration, KConnGetCurrentAddr, status5, &actaddr); 
...

//Retrieve multiple parameters

Multiple opcodes can be queried to a DHCP server using the 
TDhcpRawOptionMultipleDataPckg object. 

TBuf8<300> rawbuf;
rawbuf.FillZ(300);   //buffer must be large enough for all returned options values
TDhcpRawOptionMultipleDataPckg pckg1(rawbuf);
pckg1.AddRawOptionCodeL(KTFtpServerAddress);    //option 150 
pckg1.AddRawOptionCodeL(KGeoConfCivicOption);   //option 99
pckg1.AddRawOptionCodeL(KTFtpServerName);  // option 66
TRequestStatus status6;    

// Get raw option data for ip4

aConnection.Ioctl(KCOLConfiguration, KConnDhcpGetMultipleParams, status6, &rawbuf);
User::WaitForRequest(status);
...

 

Related concepts