How to get DHCP server’s IP.

Posted on Updated on

  1. include SystemConfiguration.framework in your project
  2. sample codes
  3. #include <iostream>
    #include <sys/cdefs.h>
    #include <CoreFoundation/CoreFoundation.h>
    #include <SystemConfiguration/SCDynamicStore.h>
    #include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>

    int main (int argc, char * const argv[]) {
    CFDictionaryRef dhcpInfo = SCDynamicStoreCopyDHCPInfo(NULL, NULL);
    CFDataRef dhcpServerIP = NULL;
    UInt8 dhcpCode = 54 ; // server identification

    if ( dhcpInfo == NULL )
    {
    std::cout << "SCDynamicStoreCopyDHCPInfo return null\n" ;
    return 0;
    }

    dhcpServerIP = DHCPInfoGetOptionData( dhcpInfo, dhcpCode) ;

    if ( dhcpServerIP != NULL )
    {
    const UInt8 * ptr = CFDataGetBytePtr( dhcpServerIP) ;
    printf("ServerIP:%d.%d.%d.%d\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3));
    }

    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
    }

Reference:

廣告

One thought on “How to get DHCP server’s IP.

    cotton5415 responded:
    二月 12, 2007 at 7:55 上午

    the parameter service id should be know first. or you have to enumerate all the services belongs to airport/ethernet and then get the dhcp serviceID by giving some filters, eg. the keyword dhcp.
    you can use console command scutil to get the service id

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s