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

/*
1.获得网络名,如下文件/etc/networks
2.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值