Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71151
  • 博文数量: 16
  • 博客积分: 32
  • 博客等级: 民兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-14 16:25
文章分类
文章存档

2012年(13)

2011年(3)

分类: LINUX

2012-04-14 10:47:11


点击(此处)折叠或打开

  1. //主机(host), 网络(net), 协议(proto), 服务(serv),
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <net/if.h>
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<unistd.h>

  9. #include <sys/types.h>
  10. #include<netdb.h>
  11. #include<sys/socket.h>
  12. //uname
  13. #include <sys/utsname.h>

  14. // 主机信息
  15. #define IPPROTO_IPV4 4
  16. int prin_host(struct hostent *phost)
  17. {
  18.         char **pptmp;
  19.         char ip[100];
  20.         memset(ip,0,sizeof(ip));
  21.         if(phost==NULL)
  22.         {
  23.                 return -1;
  24.         }
  25.         //Canonical
  26.         printf("hostname = %s\n", phost->h_name);

  27.         // Aliases
  28.         for (pptmp = phost->h_aliases; *pptmp != NULL; pptmp++)
  29.         {
  30.                 printf("hostname aliase = %s\n", *pptmp);
  31.         }

  32.         // addrtype
  33.         switch (phost->h_addrtype)
  34.         {
  35.                 case AF_INET:
  36.                         printf("AF_INET\n");
  37.                         break;
  38.                 case AF_INET6:
  39.                         printf("AF_INET6\n");
  40.                         break;
  41.                 case AF_UNIX:
  42.                         printf("AF_UNIX\n");
  43.                         break;
  44.                 case AF_ROUTE:
  45.                         printf("AF_ROUTE\n");
  46.                         break;
  47.                 case PF_KEY:
  48.                         printf("PF_KEY\n");
  49.                         break;
  50.                 default:
  51.                         printf("UNKNOW\n");
  52.                         break;
  53.         }

  54.         printf("h_length = %d\n", phost->h_length);

  55.         for(pptmp = phost->h_addr_list; *pptmp != NULL; pptmp++)
  56.         {
  57.                 memset(ip,0,sizeof(ip));
  58.                 inet_ntop(phost->h_addrtype,*pptmp, ip, sizeof(ip));
  59.                 printf("ipaddr == %s\n", ip);
  60.         }

  61. }
  62. int host_info()
  63. {
  64.         //相关文件 /etc/hosts
  65.         struct hostent *phost=NULL;
  66.         printf("host info:\n");
  67.         sethostent(0);
  68.         while((phost=gethostent())!=NULL)
  69.         {
  70.                 prin_host(phost);
  71.         }
  72.         endhostent();

  73.         return 0;
  74. }
  75. // 打印网络名字和网络号查询:
  76. int prin_netent(struct netent *pnet)
  77. {

  78.         //相关文件 /etc/networks
  79.         struct in_addr addr;
  80.         char **pptmp;
  81.         char ip[100];
  82.         if(pnet==NULL)
  83.         {
  84.                 return 0;
  85.         }
  86.         //network name
  87.         printf("network name = %s\n", pnet->n_name);

  88.         // pointer to alternate network name array
  89.         for (pptmp = pnet->n_aliases; *pptmp != NULL; pptmp++)
  90.         {
  91.                 printf("network aliase = %s\n", *pptmp);
  92.         }

  93.         // addrtype
  94.         printf("always AF_INET\n");
  95.         memset(ip,0,sizeof(ip));
  96.         memset(&addr,0,sizeof(addr));
  97.         addr.s_addr=htonl(pnet->n_net);//网络号:主机序转换成网络字节序
  98.         inet_ntop(AF_INET,(void *)(&addr),ip,INET_ADDRSTRLEN);
  99.         printf("network num= %lu\n",pnet->n_net);
  100.         printf("network address= %s\n",ip);
  101.         return 0;

  102. }
  103. // 打印网络名字和网络号查询:
  104. int net_info()
  105. {
  106.         char * FILE_NAME= "/etc/networks";
  107.         //文件 /etc/networks
  108.         struct netent *pnet=NULL;
  109.         uint32_t net_num=2851995648UL;// 主机序网络号
  110.         char *name="loopback";
  111.         //c语言里面的常量默认是一个32位的有符号整型数。对于3951481745,由于无法用32位的有符号整型数表示,所以会报警告。解决方法 加上数字后边加上UL
  112.         /*
  113.          n_name
  114.          The official name of the network.
  115.          n_aliases
  116.          A NULL-terminated list of alternative names for the network.
  117.          n_addrtype
  118.          The type of the network number; always AF_INET.
  119.          n_net
  120.          The network number in host byte order.
  121.          */
  122.         printf("net info:\n");
  123.         setnetent(0);
  124.         while((pnet=getnetent())!=NULL)
  125.         {
  126.                 prin_netent(pnet);
  127.         }
  128.         endprotoent();
  129.         //struct netent *getnetbyaddr(uint32_t net, int type);
  130.         //struct netent *getnetbyname(const char *name);
  131.         pnet=NULL;
  132.         if((pnet=getnetbyaddr(net_num ,AF_INET))!=NULL)
  133.         {
  134.                 prin_netent(pnet);
  135.         }
  136.         else
  137.         {
  138.                 printf("getnetbyaddr not find net_num %lu from file %s\n",net_num,FILE_NAME);
  139.         }
  140.         pnet=NULL;
  141.         if((pnet=getnetbyname(name))!=NULL)
  142.         {
  143.                 prin_netent(pnet);
  144.         }
  145.         else
  146.         {
  147.                 printf("getnetbyname not find name %s from file %s\n",name,FILE_NAME);
  148.         }


  149.         return 0;
  150. }
  151. int prin_protoent(struct protoent *ppro)
  152. {
  153.         char **pptmp;
  154.         if(ppro==NULL)
  155.         {
  156.                 return 0;
  157.         }
  158.         //network name
  159.         printf("protocol name = %s\n", ppro->p_name);

  160.         // pointer to alternate network name array
  161.         for (pptmp = ppro->p_aliases; *pptmp != NULL; pptmp++)
  162.         {
  163.                 printf("protocol aliase = %s\n", *pptmp);
  164.         }
  165.         printf("protocol number = %d\n",ppro->p_proto);


  166. }
  167. // 协议名字和协议号查询:
  168. int protoent_info()
  169. {

  170.         char * FILE_NAME = "/etc/protocols";
  171.         //文件 /etc/networks
  172.         struct protoent *ppro=NULL;
  173.         /* Description of data base entry for a single service. */

  174.         //struct protoent
  175.         //{
  176.         //        char *p_name; /* Official protocol name. */
  177.         //        char **p_aliases; /* Alias list. */
  178.         //        int p_proto; /* Protocol number. */
  179.         //};
  180.         int proto_num=0;// 协议号
  181.         char *name="tcp";
  182.         printf("protocol info:\n");
  183.         setprotoent(0);
  184.         while((ppro=getprotoent())!=NULL)
  185.         {
  186.                 prin_protoent(ppro);
  187.         }
  188.         endprotoent();
  189.         //struct protoent *getprotobyname(const char *name);
  190.         //struct protoent *getprotobynumber(int proto);

  191.         ppro=NULL;
  192.         if((ppro=getprotobynumber(proto_num ))!=NULL)
  193.         {
  194.                 prin_protoent(ppro);
  195.         }
  196.         else
  197.         {
  198.                 printf("getprotobynumber not find proto_num %lu from file %s\n",proto_num,FILE_NAME);
  199.         }
  200.         ppro=NULL;
  201.         if((ppro=getprotobyname(name))!=NULL)
  202.         {
  203.                 prin_protoent(ppro);
  204.         }
  205.         else
  206.         {
  207.                 printf("getprotobyname not find name %s from file %s\n",name,FILE_NAME);
  208.         }


  209.         return 0;

  210. }
  211. int prin_servent(struct servent *pserv)
  212. {
  213.         char **pptmp;
  214.         if(pserv==NULL)
  215.         {
  216.                 return 0;
  217.         }
  218.         //server name
  219.         printf("server name = %s\n", pserv->s_name);

  220.         // pointer to alternate service name array
  221.         for (pptmp = pserv->s_aliases; *pptmp != NULL; pptmp++)
  222.         {
  223.                 printf("server aliase = %s\n", *pptmp);
  224.         }
  225.         printf("port number = %d\n",pserv->s_port);
  226.         printf("protocol name = %s\n",pserv->s_proto);


  227. }
  228. //服务查询
  229. int serve_info()
  230. {
  231.         /* Description of data base entry for a single service. */
  232.         //struct servent
  233.         //{
  234.         //        char *s_name; /* Official service name. */
  235.         //        char **s_aliases; /* Alias list. */
  236.         //        int s_port; /* Port number. */
  237.         //        char *s_proto; /* Protocol to use. */
  238.         //};
  239.         char * FILE_NAME = "/etc/services";
  240.         char *proto="tcp";
  241.         //文件 /etc/services
  242.         struct servent *pserv=NULL;
  243.         int serv_num=59668;// 协议号
  244.         char *name="mdns";
  245.         printf("server info:\n");

  246.         /*
  247.          setservent(0);
  248.          while((pserv=getservent())!=NULL)
  249.          {
  250.          prin_servent(pserv);
  251.          }
  252.          endservent();
  253.          */
  254.         //struct servent *getservbyname(const char *name, const char *proto);
  255.         //struct servent *getservbyport(int port, const char *proto);


  256.         pserv=NULL;
  257.         if((pserv=getservbyport(serv_num,proto))!=NULL)
  258.         {
  259.                 prin_servent(pserv);
  260.         }
  261.         else
  262.         {
  263.                 printf("getservbyport not find serv_num %lu proto %s from file %s\n",serv_num,proto,FILE_NAME);
  264.         }
  265.         pserv=NULL;
  266.         if((pserv=getservbyname(name,proto))!=NULL)
  267.         {
  268.                 prin_servent(pserv);
  269.         }
  270.         else
  271.         {
  272.                 printf("getservbyname not find name %s proto %s from file %s\n",name,proto,FILE_NAME);
  273.         }

  274. }
  275. int prin_addrinfo(struct addrinfo *aip)
  276. {
  277.         struct addrinfo * add_tmp=NULL;
  278.         if(aip==NULL)
  279.         {
  280.                 return 0;
  281.         }
  282.         for(add_tmp=aip;add_tmp!=NULL;add_tmp=add_tmp->ai_next)
  283.         {
  284.                 if(add_tmp->ai_flags==AI_PASSIVE)
  285.                 {
  286.                         printf("ai_flags:AI_PASSIVE\n");
  287.                 }else if(add_tmp->ai_flags==AI_CANONNAME)
  288.                 {
  289.                         printf("ai_flags:AI_CANONNAME\n");
  290.                 }
  291.                 switch(add_tmp->ai_family)
  292.                 {
  293.                         case AF_INET:
  294.                                 printf("ai_family:inet--ipv4\n");
  295.                                 break;
  296.                         case AF_INET6:
  297.                                 printf("ai_family:inet6--ipv6\n");
  298.                                 break;
  299.                         case AF_UNSPEC:
  300.                                 printf("ai_family:unspec--ipv4 ipv6\n");
  301.                                 break;
  302.                         default:
  303.                                 printf("ai_family:unknow\n");
  304.                                 break;
  305.                 }
  306.                 switch(add_tmp->ai_socktype)
  307.                 {
  308.                         case SOCK_STREAM:
  309.                                 printf("ai_socktype:SOCK_STREAM---tcp\n");
  310.                                 break;
  311.                         case SOCK_DGRAM:
  312.                                 printf("ai_socktype:SOCK_DGRAM---udp\n");
  313.                                 break;
  314.                         default:
  315.                                 printf("ai_socktype:unknow\n");
  316.                                 break;
  317.                 }
  318.                 switch(add_tmp->ai_protocol)
  319.                 {
  320.                         case IPPROTO_IP:
  321.                                 printf("ai_protocol:IPPROTO_IP----protocol num %d\n",IPPROTO_IP);
  322.                                 break;
  323.                         case IPPROTO_IPV4:
  324.                                 printf("ai_protocol:IPPROTO_IPV4----protocol num %d\n",IPPROTO_IPV4);
  325.                                 break;
  326.                         case IPPROTO_IPV6:
  327.                                 printf("ai_protocol:IPPROTO_IPV6----protocol num %d\n",IPPROTO_IPV6);
  328.                                 break;
  329.                         case IPPROTO_TCP:
  330.                                 printf("ai_protocol:IPPROTO_TCP----protocol num %d\n",IPPROTO_TCP);
  331.                                 break;
  332.                         case IPPROTO_UDP:
  333.                                 printf("ai_protocol:IPPROTO_UDP----protocol num %d\n",IPPROTO_UDP);
  334.                                 break;
  335.                         default:
  336.                                 printf("ai_protocol:unknow protocol----protocol num %d\n",add_tmp->ai_protocol);
  337.                                 break;
  338.                 }
  339.                 printf("ai_addrlen:%d\n",add_tmp->ai_addrlen);
  340.                 printf("ai_canonname:%s\n",add_tmp->ai_canonname);
  341.                 // 网络地址
  342.                 printf("ai_addr:%s\n",inet_ntoa(((struct sockaddr_in*)&(add_tmp->ai_addr))->sin_addr));

  343.         }

  344. }
  345. int addr_info()
  346. {
  347.         /*
  348.          int getaddrinfo(const char *restrict host,
  349.          const char *restrict service,
  350.          const struct addrinfo *restrict hint,
  351.          struct addrinfo **restrict res);
  352.          */
  353.         /*
  354.          struct addrinfo{
  355.          int ai_flags; // AI_PASSIVE,AI_CANONNAME
  356.          int ai_family;//地址族
  357.          int ai_socktype;//socket类型
  358.          int ai_protocol; //协议类型
  359.          socklen_t ai_addrlen;//地址字节长度
  360.          char *ai_canonname;//主机名
  361.          struct sockaddr *ai_addr;//socket结构体
  362.          struct addrinfo *ai_next;
  363.          };
  364.          */
  365.         char *host="";//是主机名 ip地址
  366.         //char *host="";
  367.         //char *host="96.197.49.9";
  368.         //char *host="61.135.169.105";
  369.         //char *host="localhost";
  370.         char *server="http";//server 一般是协议名称 或者协议号

  371.         struct addrinfo *ailist, *aip;
  372.         struct addrinfo hint;
  373.         const char *addr;
  374.         int err;
  375.         char abuf[INET_ADDRSTRLEN];
  376.         memset(&hint,0,sizeof(struct addrinfo));
  377.         hint.ai_flags = AI_CANONNAME;
  378.         //AI_CANONNAME:通知getaddrinfo函数返回主机的名字
  379.         //AI_PASSIVE:该套接口是用作被动地打开
  380.         hint.ai_family = AF_UNSPEC;
  381.         //AF_INET:IPv4协议 AF_INET6:IPv6协议
  382.         // AI_CANONNAME IPv4或IPv6均可 : AF_INET AF_INET6 都可以
  383.         hint.ai_socktype = SOCK_DGRAM;
  384.         //SOCK_STREAM:字节流套接字socket(TCP)
  385.         //SOCK_DGRAM:数据报套接字socket(UDP)
  386.         hint.ai_protocol = IPPROTO_UDP;//协议
  387.         //IPPROTO_IP:IP协议
  388.         //IPPROTO_IPV4:IPv4协议
  389.         //IPPROTO_IPV6:IPv6协议
  390.         //IPPROTO_UDP:UDP
  391.         //IPPROTO_TCP:TCP
  392.         hint.ai_addrlen = 0;
  393.         hint.ai_canonname = NULL;
  394.         hint.ai_addr = NULL;
  395.         hint.ai_next = NULL;
  396.         /*
  397.          (1)通常服务器端在调用getaddrinfo()之前,ai_flags设置AI_PASSIVE,
  398.          用于 bind()函数(用于端口和地址的绑定,后面会讲到),主机名nodename通常会设置为NULL。
  399.          (2)客户端调用getaddrinfo()时,ai_flags一般不设置AI_PASSIVE,但是主机名
  400.          nodename和服务名servname(端口)则应该不为空。
  401.          (3) 即使不设置ai_flags为AI_PASSIVE,取出的地址也可以被绑定,很多程序中ai_flags直接设置为0,
  402.          即3个标志位都不设置,这种情况 下只要hostname和servname设置的没有问题就可以正确绑定。
  403.          */
  404.         if ((err = getaddrinfo(host, server, &hint, &ailist)) != 0)
  405.                 printf("getaddrinfo error: %s", gai_strerror(err));
  406.         for (aip = ailist; aip != NULL;aip=aip->ai_next)
  407.         {
  408.                 prin_addrinfo(aip);
  409.                 //void freeaddrinfo(struct addrinfo *ai);

  410.         }
  411.         freeaddrinfo( ailist);



  412.         return 0;

  413. }
  414. int name_info()
  415. {
  416.         /*
  417.          int getnameinfo(const struct sockaddr *restrict addr, socklen_t alen,
  418.          char *restrict host, socklen_t hostlen,
  419.          char *restrict service, socklen_t servlen,
  420.          unsigned int flags);
  421.          */
  422.         /*
  423. addr: socket地址.
  424. alen: socket地址长度.
  425. host: 主机名.
  426. hostlen: 主机名长度.
  427. service: 服务名.
  428. servlen: 服务名长度.
  429. flags: 指定转换的控制方式. 可以有如下几种:
  430. NI_DGRAM: 服务基于数据报而非基于流.
  431. NI_NAMEREQD: 如果找不到主机名字, 将其作为一个错误对待.
  432. NI_NOFQDN: 对于本地主机, 仅返回完全限定域名的节点名部分.
  433. NI_NUMERICHOST: 以数字形式而非名字返回主机地址.
  434. NI_NUMERICSERV: 以数字形式而非名字返回服务地址(即端口号).
  435. 说明: 如果host或service非空,()将指向洋长度问该len字节的缓冲区用于存储返回的名. 如果为空则不返回该名.
  436. */
  437.         struct sockaddr_in addr;
  438.         char host[NI_MAXHOST];
  439.         char serv[NI_MAXSERV];

  440.         bzero( host, NI_MAXHOST );
  441.         bzero( serv, NI_MAXSERV );

  442.         addr.sin_family = AF_INET;
  443.         addr.sin_port = htons(53);//23 telnet 端口 domain 53 端口
  444.         addr.sin_addr.s_addr = inet_addr( "192.168.155.224" );//计算机的ip地址

  445.         //得到机器的主机名称 和 对应端口所用到的协议
  446.         getnameinfo( (struct sockaddr *)&addr, sizeof( struct sockaddr_in ),
  447.                         host, sizeof(host), serv, sizeof( serv ), NI_NOFQDN );

  448.         printf("host = %s \n", host );
  449.         printf("serv = %s \n", serv );

  450.         return 0;

  451. }
  452. //使用gethostname()得到主机名 .
  453. //gethostbyname 得到详细信息
  454. int hostname_info()
  455. {
  456.         struct hostent *hptr;
  457.         char **pptr;
  458.         char hostname[32];
  459.         char str[32];
  460.         memset(hostname,0,sizeof(hostname));
  461.         memset(str,0,sizeof(str));
  462.         printf("gethostname --------------\n");
  463.         if(gethostname(hostname,sizeof(hostname)) )
  464.         {
  465.                 printf("gethostname calling error\n");
  466.                 return 1;
  467.         }
  468.         printf("localhost name:%s\n",hostname);
  469.         memset(hostname,0,sizeof(hostname));
  470.         strcpy(hostname,"linurw");        
  471.         //临时设置计算机名 修改后使用 gethostbyname 后会出现错误,gethostbyname 无法得到相应的结构体

  472.         if( sethostname(hostname,sizeof(hostname))<0 )
  473.         {
  474.                 printf("sethostname calling error\n");
  475.                 return 1;
  476.         }
  477.         else
  478.         {
  479.                  printf("sethostname success : %s\n",hostname);
  480.         }

  481.         memset(hostname,0,sizeof(hostname));
  482.         if( gethostname(hostname,sizeof(hostname)) )
  483.         {
  484.                 printf("gethostname calling error\n");
  485.                 return 1;
  486.         }
  487.         printf("localhost name:%s\n",hostname);

  488.         if( (hptr = gethostbyname(hostname)) == NULL)
  489.         {
  490.                 printf("gethostbyname calling error\n");
  491.                 return 1;
  492.         }
  493.         prin_host(hptr);


  494. }
  495. int uname_info()
  496. {
  497.     //struct utsname{
  498.     // char sysname[_UTS_NAMESIZE]; /* name of this operating system */
  499.     // char nodename[_UTS_NODESIZE]; /* name of this node */
  500.     // char release[_UTS_NAMESIZE]; /* O.S release level */
  501.     // char version[_UTS_NAMESIZE]; /* O.S version level */
  502.     // char machine[_UTS_NAMESIZE]; /* hardware type */
  503.     // };
  504.         struct hostent *hptr=NULL;
  505.         struct utsname myname;
  506.         char **pptr;
  507.         char str[32];
  508.         bzero(&myname,sizeof(myname));
  509.         bzero(str,sizeof(str));
  510.         printf("uname info:-------------\n");
  511.         if(uname(&myname)<0)
  512.         {
  513.                 printf("uname calling error\n");
  514.                 return 1;
  515.         }
  516.         printf("utsname.sysname: %s\n",myname.sysname);
  517.         printf("utsname.nodename:%s\n",myname.nodename);
  518.         printf("utsname.release: %s\n",myname.release);
  519.         printf("utsname.version: %s\n",myname.version);
  520.         printf("utsname.machine: %s\n",myname.machine);
  521.         if( (hptr = gethostbyname(myname.nodename)) == NULL)
  522.         {
  523.                 printf("gethostbyname calling error\n");
  524.                 return 1;
  525.         }
  526.         prin_host(hptr);


  527. }
  528. int hostbyaddr_info()
  529. {
  530.         char *ptr="192.168.155.224";
  531.         char **pptr;
  532.         struct hostent *hptr=NULL;
  533.         char str[32];
  534.         char ipaddr[16];
  535.         bzero(str,sizeof(str));
  536.         bzero(ipaddr,sizeof(ipaddr));
  537.         struct in_addr hipaddr;
  538.         bzero(&hipaddr,sizeof(hipaddr));
  539.         /* 取得命令后第一个参数,即要解析的IP地址 */
  540.         /* 调用inet_aton(),ptr就是以字符串存放的地方的指针,hipaddr是in_addr形式的地址 */
  541.         printf("gethostbyaddr-------------\n");
  542.         if(!inet_aton(ptr,&hipaddr))
  543.         {
  544.                 printf("inet_aton error\n");
  545.                 return 1;
  546.         }
  547.         /* 调用gethostbyaddr()。调用结果都存在hptr中 */
  548.         if( (hptr = gethostbyaddr(&hipaddr,sizeof(hipaddr), AF_INET) ) == NULL )
  549.         {
  550.                 printf("gethostbyaddr error for addr:%s\n", ptr);
  551.                 return 1; /* 如果调用gethostbyaddr发生错误,返回1 */
  552.         }
  553.         prin_host(hptr);

  554. }
  555. //getdomainname(2)函数:可以允许程序获得程序正运行的主机的NIS域名。
  556. //这个函数的用法也gethostname相同。
  557. int domain_info()
  558. {
  559.         char domainname[32];
  560.         char str[32];
  561.         memset(domainname,0,sizeof(domainname));
  562.         memset(str,0,sizeof(str));
  563.         printf("getdomainname --------------\n");
  564.         //int getdomainname(char *name, size_t len);
  565.         //int setdomainname(const char *name, size_t len);
  566.         strcpy(domainname,"water.com");
  567.         setdomainname(domainname,sizeof(domainname));
  568.         memset(domainname,0,sizeof(domainname));
  569.         if(getdomainname(domainname,sizeof(domainname)) )
  570.         {
  571.                 printf("getdomainname calling error\n");
  572.                 return 1;
  573.         }
  574.         printf("getdomainname name:%s\n",domainname);

  575. }
  576. int main(int argc,char **argv)
  577. {

  578.         host_info();//gethostent()
  579.         net_info();//getnetent()
  580.         protoent_info();//getprotoent()
  581.         serve_info();//getserveinfo()
  582.         addr_info();//getaddrinfo()
  583.         name_info();//getnameinfo()
  584.         hostname_info();//gethostname()
  585.         uname_info();//uname()
  586.         hostbyaddr_info();//gethostbyaddr
  587.         domain_info();//getdomainname




  588. }

阅读(4102) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~