网络编程基础4-获得网络信息getnetent

来源:互联网 发布:淘宝店招分层素材 编辑:程序博客网 时间:2024/05/22 12:38
/*1.获得网络名,如下文件/etc/networks2.struct netent结构体成员使用*/#include <netdb.h>#include <stdlib.h>#include <stdio.h>int main(){    struct netent *net;    int8_t decdotip[16] = {0};    char **p;    uint32_t netaddrtype;    while((net = getnetent() )!= NULL)    {        printf("network name : %s\n",net->n_name);        if( net->n_aliases[0] == NULL) printf("no alternate network name.\n");        else        {            for(p=net->n_aliases; *p != NULL; p++ )            {                printf("alternate network name: %s\n",*p);            }        }        if(net->n_addrtype == AF_INET)            printf("address type AF_INET:%d\n",net->n_addrtype);        else            printf("address type UNIX_INET:%d\n",net->n_addrtype);        /*  得到的是32bit本地主机字节数据 */        netaddrtype = htonl(net->n_net);        printf("net number:0x%08x, ip:%s\n",netaddrtype,(char *)inet_ntop(net->n_addrtype, (struct in_addr *)&netaddrtype, decdotip,sizeof(decdotip)));    }    endnetent();    return 0;}

这里写图片描述

#if 0/* Description of data base entry for a single network.  NOTE: here a   poor assumption is made.  The network number is expected to fit   into an unsigned long int variable.  /usr/include/i386-linux-gnu/bits/netdb.h*/struct netent{  char *n_name;         /* Official name of network.  */  char **n_aliases;     /* Alias list.  */  int n_addrtype;       /* Net address type.  */  uint32_t n_net;       /* Network number.  */};/* Open network data base files and mark them as staying open even   after a later search if STAY_OPEN is non-zero.   This function is a possible cancellation point and therefore not   marked with __THROW.  */extern void setnetent (int __stay_open);/* Close network data base files and clear `stay open' flag.   This function is a possible cancellation point and therefore not   marked with __THROW.  */extern void endnetent (void);/* Get next entry from network data base file.  Open data base if   necessary.   This function is a possible cancellation point and therefore not   marked with __THROW.  */extern struct netent *getnetent (void);/* Return entry from network data base which address match NET and   type TYPE.   This function is a possible cancellation point and therefore not   marked with __THROW.  */extern struct netent *getnetbyaddr (uint32_t __net, int __type);/* Return entry from network data base for network with NAME.   This function is a possible cancellation point and therefore not   marked with __THROW.  */extern struct netent *getnetbyname (__const char *__name);#endif
0 0
原创粉丝点击