Iperf源代码分析(六)

本节主要介绍命令行参数处理函数及一些网络地址转换方面的函数:   

首先,介绍关于命令行参数处理函数相关源代码。

gnu_getopt.h文件源代码:

#ifndef _GETOPT_H
    #define _GETOPT_H 1

    #ifdef    __cplusplus
extern "C" {
#endif

/* For communication from `gnu_getopt' to the caller.
   When `gnu_getopt' finds an option that takes an argument,
   the argument value is returned here.
   Also, when `ordering' is RETURN_IN_ORDER,
   each non-option ARGV-element is returned here.  */

extern char *gnu_optarg;

/* Index in ARGV of the next element to be scanned.
   This is used for communication to and from the caller
   and for communication between successive calls to `gnu_getopt'.

   On entry to `gnu_getopt', zero means this is the first call; initialize.

   When `gnu_getopt' returns -1, this is the index of the first of the
   non-option elements that the caller should itself scan.

   Otherwise, `gnu_optind' communicates from one call to the next
   how much of ARGV has been scanned so far.  */

extern int gnu_optind;

/* Callers store zero here to inhibit the error message `gnu_getopt' prints
   for unrecognized options.  */

extern int gnu_opterr;

/* Set to an option character which was unrecognized.  */

extern int gnu_optopt;

/* Describe the long-named options requested by the application.
   The LONG_OPTIONS argument to gnu_getopt_long or getopt_long_only is a vector
   of `struct option' terminated by an element containing a name which is
   zero.

   The field `has_arg' is:
   no_argument        (or 0) if the option does not take an argument,
   required_argument    (or 1) if the option requires an argument,
   optional_argument     (or 2) if the option takes an optional argument.

   If the field `flag' is not NULL, it points to a variable that is set
   to the value given in the field `val' when the option is found, but
   left unchanged if the option is not found.

   To have a long-named option do something other than set an `int' to
   a compiled-in constant, such as set a value from `gnu_optarg', set the
   option's `flag' field to zero and its `val' field to a nonzero
   value (the equivalent single-letter option character, if there is
   one).  For long options that have a zero `flag' field, `gnu_getopt'
   returns the contents of the `val' field.  */

/* has_arg can't be an enum because some compilers complain about
   type mismatches in all the code that assumes it is an int.  */

struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
};

/* Names for the values of the `has_arg' field of `struct option'.  */

#define    no_argument            0
#define required_argument    1
#define optional_argument    2

int gnu_getopt( int argc,
                char *const *argv,
                const char *shortopts );

int gnu_getopt_long( int argc,
                     char *const *argv,
                     const char *shortopts,
                     const struct option *longopts,
                     int *longind );

int gnu_getopt_long_only( int argc,
                          char *const *argv,
                          const char *shortopts,
                          const struct option *longopts,
                          int *longind );

/* Internal only.  Users should not call this directly.  */
int _gnu_getopt_internal( int argc,
                          char *const *argv,
                          const char *shortopts,
                          const struct option *longopts,
                          int *longind,
                          int long_only );

#ifdef    __cplusplus
} /* end extern "C" */
    #endif

#endif /* _GETOPT_H */

    在这里,需要明确这样几个概念:

extern char *gnu_optarg;    
extern int gnu_optind;    
extern int gnu_opterr;    
extern int gnu_optopt;

====>>>>>

1、*gnu_optarg :当gnu_getopt函数查找到一个含参数的选项,对应参数就会返回到此变量中(gnu_optarg字符串)。对于“RETURN_IN_ORDER”的“ordering”时,每一个非选项参数均将参量返回到gnu_optarg字符串中。

2、gnu_optind :下一个被扫描到的元素中的元素序号,适用于caller与gnu_getopt之间的交互。在gnu_getopt函数入口,zero(0)表示这是第一个call,进行初始化工作。当“gnu_getopt”返回-1时,表示这是第一个非选项参量,这时,caller应该自我扫描。

3、gnu_opterr :caller此时存储0来防止错误信息"gnu_getopt"打印无法识别的选项(option)的错误。

4、gnu_optopt :用以标志无法识别的选项。这个值在很多系统中为了避免与系统自身的gnu_getopt实现相连接必须进行初始化。

====>>>>>

struct option :

struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
}

has_arg成员变量表示:

no_argument (0) -----此选项没有参量(自变量);

required_argument (1)-----此选项需要参量(一般需要设置,未设置应设立default选项);

optional_argument (2)-----此选项有可选参量。

flag成员变量:

当flag不为空时,当发现一个option时,其指向一个值被设置为给定val域中的值的变量;当没有option被发现时,此时flag保持不变。

在这里,我们可以结合具体实现时的赋值来对比学习:

在gnu_getopt.c中,这些变量的值的初始化为:

char *gnu_optarg = NULL;
/* 1003.2 says this must be 1 before any call.  */
int gnu_optind = 1;
int gnu_opterr = 1;
int gnu_optopt = '?';

下面对几个实现函数进行说明:

gnu_getopt(......)函数:实现对形如"-c"/"--c"之类的参数进行参数提取,得到'c'。

int gnu_getopt ( int argc,
             char *const *argv,
             const char *optstring ) {
    return _gnu_getopt_internal (argc, argv, optstring,
                                 (const struct option *) 0,
                                 (int *) 0,
                                 0);
}

gnu_getopt_long(......)函数:实现对形如"-client"/"--client"之类的参数进行参数提取,得到'c'。

int gnu_getopt_long( int argc,
                 char *const *argv,
                 const char *options,
                 const struct option *long_options,
                 int *opt_index ) {
    return _gnu_getopt_internal (argc, argv, options, long_options, opt_index, 0);
}

它们的核心实现为_gnu_getopt_internal(......)函数:

int _gnu_getopt_internal( int argc,char *const *argv,
                      const char *optstring,
                      const struct option *longopts,
                      int *longind,
                      int long_only );

再则,介绍一些关于 网络地址转换方面的函数:

这里主要有四个文件:endian.c、inet_aton.h、inet_ntop.c、inet_pton.c

在endian.c中,主要功能函数是:

void ntoh( void *buffer, int len, int inSizeof );

其完成的功能是将网络字节序转换为主机字节序。

在inet_aton.h,简述了相关功能:

/*
 * inet_pton is the new, better version of inet_aton.
 * inet_aton is not IP version agnostic.
 * inet_aton is the new, better version of inet_addr.
 * inet_addr is incorrect in that it returns -1 as an error value,
 * while -1 (0xFFFFFFFF) is a valid IP address (255.255.255.255).
 */

在inet_ntop.c中,主要功能函数是:

int inet_ntop(int af, const void *src, char *dst, size_t size);

其完成的功能是完成数值式套接字地址(IP地址-二进制形式)转换为表达式套接字地址(IP地址-点分十进制形式)。其余系统提供的相关函数不同的地方是,其可以同时实现IPv4与IPv6的地址转换工作。此函数的子功能函数分别为

//ipv4 version
static int
inet_ntop4(const unsigned char *src, char *dst, size_t size);
//ipv6 version
static int
inet_ntop6(const unsigned char *src, char *dst, size_t size);

同理,在inet_pton.c中,主要功能函数是:

int inet_pton(int af, const char *src, void *dst);

其完成的功能是完成表达式套接字地址(IP地址-点分十进制形式)转换为数值式套接字地址(IP地址-二进制形式)。其余系统提供的相关函数不同的地方是,其可以同时实现IPv4与IPv6的地址转换工作。此函数的子功能函数分别为

//ipv4 version
static int
inet_pton4(src, dst)
const char *src;
unsigned char *dst;
//ipv6 version
inet_pton6(src, dst)
const char *src;
unsigned char *dst;



转载于:https://my.oschina.net/kiterunner24/blog/269033

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值