udhcp源碼剖析(五)——DHCP客戶端重要的數據結構和初始化配置

數據結構

和服務器類似,客戶端也維護着一個全局的數據結構client_config,用於所有的DHCP服務。

struct client_config_t client_config = {
    /* Default options. */
    abort_if_no_lease: 0,
    foreground: 0,
    quit_after_lease: 0,
    background_if_no_lease: 0,
    interface: "eth0",
    pidfile: NULL,
    script: DEFAULT_SCRIPT,
    clientid: NULL,
    hostname: NULL,
    ifindex: 0,
    arp: "\0\0\0\0\0\0",        /* appease gcc-3.0 */
};

該結構在dhcpc.h中定義,並在dhcpc.c中設置默認值

struct client_config_t client_config = {
    /* Default options. */
    abort_if_no_lease: 0,
    foreground: 0,
    quit_after_lease: 0,
    background_if_no_lease: 0,
    interface: "eth0",
    pidfile: NULL,
    script: DEFAULT_SCRIPT,
    clientid: NULL,
    hostname: NULL,
    ifindex: 0,
    arp: "\0\0\0\0\0\0",        /* appease gcc-3.0 */
};

其它全局變量

其餘所需要的重要的全局變量都以簡單數據結構給出,並沒有專門設計複雜結構。

static int state;
static unsigned long requested_ip; /* = 0,請求的IP */
static unsigned long server_addr;       //服務器地址
static unsigned long timeout;       //租約超時時刻    
static int packet_num; /* = 0 */        //可認爲是連續重複發送數據包的次數
static int fd;                  //本地socket監聽的fd
static int signal_pipe[2];          //本地接收signal消息的pipe fd

udhcpc_main的初始化和配置

dhcpc的初始化和dhcpd類似,但是簡化了很多操作,並且添加了對傳入參數的解析和處理。

解析arg選項

對啓動時傳遞的arg選項的解析,通過這些選項,DHCP可以選擇多種模式運行,比如傳遞參數q,即設置quit_after_lease=1,客戶端將在成功獲得lease之後退出。

enum {
        OPT_c = 1 << 0,
        OPT_C = 1 << 1,
        OPT_V = 1 << 2,
        OPT_f = 1 << 3,
        OPT_b = 1 << 4,
        OPT_H = 1 << 5,
        OPT_h = 1 << 6,
        OPT_F = 1 << 7,
        OPT_i = 1 << 8,
        OPT_n = 1 << 9,
        OPT_p = 1 << 10,
        OPT_q = 1 << 11,
        OPT_R = 1 << 12,
        OPT_r = 1 << 13,
        OPT_s = 1 << 14,
        OPT_T = 1 << 15,
        OPT_t = 1 << 16,
        OPT_v = 1 << 17,
        OPT_S = 1 << 18,
    };
#if ENABLE_GETOPT_LONG
    static const char udhcpc_longopts[] ALIGN1 =
        "clientid\0"      Required_argument "c"
        "clientid-none\0" No_argument       "C"
        "vendorclass\0"   Required_argument "V"
        "foreground\0"    No_argument       "f"
        "background\0"    No_argument       "b"
        "hostname\0"      Required_argument "H"
        "hostname\0"      Required_argument "h"
        "fqdn\0"          Required_argument "F"
        "interface\0"     Required_argument "i"
        "now\0"           No_argument       "n"
        "pidfile\0"       Required_argument "p"
        "quit\0"          No_argument       "q"
        "release\0"       No_argument       "R"
        "request\0"       Required_argument "r"
        "script\0"        Required_argument "s"
        "timeout\0"       Required_argument "T"
        "version\0"       No_argument       "v"
        "retries\0"       Required_argument "t"
        "syslog\0"        No_argument       "S"
        ;
#endif
    /* Default options. */
    client_config.interface = "eth0";
    client_config.script = DEFAULT_SCRIPT;
    client_config.retries = 3;
    client_config.timeout = 3;

    /* Parse command line */
    opt_complementary = "c--C:C--c" // mutually exclusive
                        ":hH:Hh"; // -h and -H are the same
#if ENABLE_GETOPT_LONG
    applet_long_options = udhcpc_longopts;
#endif
    opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vS",
        &str_c, &str_V, &str_h, &str_h, &str_F,
        &client_config.interface, &client_config.pidfile, &str_r,
        &client_config.script, &str_T, &str_t
        );

    if (opt & OPT_c)
        client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
    //if (opt & OPT_C)
    if (opt & OPT_V)
        client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
    if (opt & OPT_f)
        client_config.foreground = 1;
    if (opt & OPT_b)
        client_config.background_if_no_lease = 1;
    if (opt & OPT_h)
        client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
    if (opt & OPT_F) {
        client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
        /* Flags: 0000NEOS
        S: 1 => Client requests Server to update A RR in DNS as well as PTR
        O: 1 => Server indicates to client that DNS has been updated regardless
        E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
        N: 1 => Client requests Server to not update DNS
        */
        client_config.fqdn[OPT_DATA + 0] = 0x1;
        /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
        /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
    }
    // if (opt & OPT_i) client_config.interface = ...
    if (opt & OPT_n)
        client_config.abort_if_no_lease = 1;
    // if (opt & OPT_p) client_config.pidfile = ...
    if (opt & OPT_q)
        client_config.quit_after_lease = 1;
    if (opt & OPT_R)
        client_config.release_on_quit = 1;
    if (opt & OPT_r)
        requested_ip = inet_addr(str_r);
    // if (opt & OPT_s) client_config.script = ...
    if (opt & OPT_T)
        client_config.timeout = xatoi_u(str_T);
    if (opt & OPT_t)
        client_config.retries = xatoi_u(str_t);
    if (opt & OPT_v) {
        printf("version %s\n", BB_VER);
        return 0;
    }

    if (opt & OPT_S) {
        openlog(applet_name, LOG_PID, LOG_LOCAL0);
        logmode |= LOGMODE_SYSLOG;
    }

讀取接口信息和設置信號處理器

這一步和服務器的基本一致,注意這裏的不同是對clientid的初始化,以及run_script。

    if (read_interface(client_config.interface, &client_config.ifindex,
               NULL, client_config.arp))
        return 1;

    /* Make sure fd 0,1,2 are open */
    bb_sanitize_stdio();
    /* Equivalent of doing a fflush after every \n */
    setlinebuf(stdout);

    /* Create pidfile */
    write_pidfile(client_config.pidfile);
    /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */

    /* Goes to stdout and possibly syslog */
    bb_info_msg("%s (v%s) started", applet_name, BB_VER);

    /* if not set, and not suppressed, setup the default client ID */
    if (!client_config.clientid && !(opt & OPT_C)) {
        client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
        client_config.clientid[OPT_DATA] = 1;
        memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
    }

    if (!client_config.vendorclass)
        client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);

    /* setup the signal pipe */
    udhcp_sp_setup();

    state = INIT_SELECTING;
    udhcp_run_script(NULL, "deconfig");
    change_mode(LISTEN_RAW);
    tv.tv_sec = 0;
    goto jump_in;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章