- //主机(host), 网络(net), 协议(proto), 服务(serv),
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <net/if.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #include<unistd.h>
- #include <sys/types.h>
- #include<netdb.h>
- #include<sys/socket.h>
- //uname
- #include <sys/utsname.h>
- // 主机信息
- #define IPPROTO_IPV4 4
- int prin_host(struct hostent *phost)
- {
- char **pptmp;
- char ip[100];
- memset(ip,0,sizeof(ip));
- if(phost==NULL)
- {
- return -1;
- }
- //Canonical
- printf("hostname = %s\n", phost->h_name);
- // Aliases
- for (pptmp = phost->h_aliases; *pptmp != NULL; pptmp++)
- {
- printf("hostname aliase = %s\n", *pptmp);
- }
- // addrtype
- switch (phost->h_addrtype)
- {
- case AF_INET:
- printf("AF_INET\n");
- break;
- case AF_INET6:
- printf("AF_INET6\n");
- break;
- case AF_UNIX:
- printf("AF_UNIX\n");
- break;
- case AF_ROUTE:
- printf("AF_ROUTE\n");
- break;
- case PF_KEY:
- printf("PF_KEY\n");
- break;
- default:
- printf("UNKNOW\n");
- break;
- }
- printf("h_length = %d\n", phost->h_length);
- for(pptmp = phost->h_addr_list; *pptmp != NULL; pptmp++)
- {
- memset(ip,0,sizeof(ip));
- inet_ntop(phost->h_addrtype,*pptmp, ip, sizeof(ip));
- printf("ipaddr == %s\n", ip);
- }
- }
- int host_info()
- {
- //相关文件 /etc/hosts
- struct hostent *phost=NULL;
- printf("host info:\n");
- sethostent(0);
- while((phost=gethostent())!=NULL)
- {
- prin_host(phost);
- }
- endhostent();
- return 0;
- }
- // 打印网络名字和网络号查询:
- int prin_netent(struct netent *pnet)
- {
- //相关文件 /etc/networks
- struct in_addr addr;
- char **pptmp;
- char ip[100];
- if(pnet==NULL)
- {
- return 0;
- }
- //network name
- printf("network name = %s\n", pnet->n_name);
- // pointer to alternate network name array
- for (pptmp = pnet->n_aliases; *pptmp != NULL; pptmp++)
- {
- printf("network aliase = %s\n", *pptmp);
- }
- // addrtype
- printf("always AF_INET\n");
- memset(ip,0,sizeof(ip));
- memset(&addr,0,sizeof(addr));
- addr.s_addr=htonl(pnet->n_net);//网络号:主机序转换成网络字节序
- inet_ntop(AF_INET,(void *)(&addr),ip,INET_ADDRSTRLEN);
- printf("network num= %lu\n",pnet->n_net);
- printf("network address= %s\n",ip);
- return 0;
- }
- // 打印网络名字和网络号查询:
- int net_info()
- {
- char * FILE_NAME= "/etc/networks";
- //文件 /etc/networks
- struct netent *pnet=NULL;
- uint32_t net_num=2851995648UL;// 主机序网络号
- char *name="loopback";
- //c语言里面的常量默认是一个32位的有符号整型数。对于3951481745,由于无法用32位的有符号整型数表示,所以会报警告。解决方法 加上数字后边加上UL
- /*
- n_name
- The official name of the network.
- n_aliases
- A NULL-terminated list of alternative names for the network.
- n_addrtype
- The type of the network number; always AF_INET.
- n_net
- The network number in host byte order.
- */
- printf("net info:\n");
- setnetent(0);
- while((pnet=getnetent())!=NULL)
- {
- prin_netent(pnet);
- }
- endprotoent();
- //struct netent *getnetbyaddr(uint32_t net, int type);
- //struct netent *getnetbyname(const char *name);
- pnet=NULL;
- if((pnet=getnetbyaddr(net_num ,AF_INET))!=NULL)
- {
- prin_netent(pnet);
- }
- else
- {
- printf("getnetbyaddr not find net_num %lu from file %s\n",net_num,FILE_NAME);
- }
- pnet=NULL;
- if((pnet=getnetbyname(name))!=NULL)
- {
- prin_netent(pnet);
- }
- else
- {
- printf("getnetbyname not find name %s from file %s\n",name,FILE_NAME);
- }
- return 0;
- }
- int prin_protoent(struct protoent *ppro)
- {
- char **pptmp;
- if(ppro==NULL)
- {
- return 0;
- }
- //network name
- printf("protocol name = %s\n", ppro->p_name);
- // pointer to alternate network name array
- for (pptmp = ppro->p_aliases; *pptmp != NULL; pptmp++)
- {
- printf("protocol aliase = %s\n", *pptmp);
- }
- printf("protocol number = %d\n",ppro->p_proto);
- }
- // 协议名字和协议号查询:
- int protoent_info()
- {
- char * FILE_NAME = "/etc/protocols";
- //文件 /etc/networks
- struct protoent *ppro=NULL;
- /* Description of data base entry for a single service. */
- //struct protoent
- //{
- // char *p_name; /* Official protocol name. */
- // char **p_aliases; /* Alias list. */
- // int p_proto; /* Protocol number. */
- //};
- int proto_num=0;// 协议号
- char *name="tcp";
- printf("protocol info:\n");
- setprotoent(0);
- while((ppro=getprotoent())!=NULL)
- {
- prin_protoent(ppro);
- }
- endprotoent();
- //struct protoent *getprotobyname(const char *name);
- //struct protoent *getprotobynumber(int proto);
- ppro=NULL;
- if((ppro=getprotobynumber(proto_num ))!=NULL)
- {
- prin_protoent(ppro);
- }
- else
- {
- printf("getprotobynumber not find proto_num %lu from file %s\n",proto_num,FILE_NAME);
- }
- ppro=NULL;
- if((ppro=getprotobyname(name))!=NULL)
- {
- prin_protoent(ppro);
- }
- else
- {
- printf("getprotobyname not find name %s from file %s\n",name,FILE_NAME);
- }
- return 0;
- }
- int prin_servent(struct servent *pserv)
- {
- char **pptmp;
- if(pserv==NULL)
- {
- return 0;
- }
- //server name
- printf("server name = %s\n", pserv->s_name);
- // pointer to alternate service name array
- for (pptmp = pserv->s_aliases; *pptmp != NULL; pptmp++)
- {
- printf("server aliase = %s\n", *pptmp);
- }
- printf("port number = %d\n",pserv->s_port);
- printf("protocol name = %s\n",pserv->s_proto);
- }
- //服务查询
- int serve_info()
- {
- /* Description of data base entry for a single service. */
- //struct servent
- //{
- // char *s_name; /* Official service name. */
- // char **s_aliases; /* Alias list. */
- // int s_port; /* Port number. */
- // char *s_proto; /* Protocol to use. */
- //};
- char * FILE_NAME = "/etc/services";
- char *proto="tcp";
- //文件 /etc/services
- struct servent *pserv=NULL;
- int serv_num=59668;// 协议号
- char *name="mdns";
- printf("server info:\n");
- /*
- setservent(0);
- while((pserv=getservent())!=NULL)
- {
- prin_servent(pserv);
- }
- endservent();
- */
- //struct servent *getservbyname(const char *name, const char *proto);
- //struct servent *getservbyport(int port, const char *proto);
- pserv=NULL;
- if((pserv=getservbyport(serv_num,proto))!=NULL)
- {
- prin_servent(pserv);
- }
- else
- {
- printf("getservbyport not find serv_num %lu proto %s from file %s\n",serv_num,proto,FILE_NAME);
- }
- pserv=NULL;
- if((pserv=getservbyname(name,proto))!=NULL)
- {
- prin_servent(pserv);
- }
- else
- {
- printf("getservbyname not find name %s proto %s from file %s\n",name,proto,FILE_NAME);
- }
- }
- int prin_addrinfo(struct addrinfo *aip)
- {
- struct addrinfo * add_tmp=NULL;
- if(aip==NULL)
- {
- return 0;
- }
- for(add_tmp=aip;add_tmp!=NULL;add_tmp=add_tmp->ai_next)
- {
- if(add_tmp->ai_flags==AI_PASSIVE)
- {
- printf("ai_flags:AI_PASSIVE\n");
- }else if(add_tmp->ai_flags==AI_CANONNAME)
- {
- printf("ai_flags:AI_CANONNAME\n");
- }
- switch(add_tmp->ai_family)
- {
- case AF_INET:
- printf("ai_family:inet--ipv4\n");
- break;
- case AF_INET6:
- printf("ai_family:inet6--ipv6\n");
- break;
- case AF_UNSPEC:
- printf("ai_family:unspec--ipv4 ipv6\n");
- break;
- default:
- printf("ai_family:unknow\n");
- break;
- }
- switch(add_tmp->ai_socktype)
- {
- case SOCK_STREAM:
- printf("ai_socktype:SOCK_STREAM---tcp\n");
- break;
- case SOCK_DGRAM:
- printf("ai_socktype:SOCK_DGRAM---udp\n");
- break;
- default:
- printf("ai_socktype:unknow\n");
- break;
- }
- switch(add_tmp->ai_protocol)
- {
- case IPPROTO_IP:
- printf("ai_protocol:IPPROTO_IP----protocol num %d\n",IPPROTO_IP);
- break;
- case IPPROTO_IPV4:
- printf("ai_protocol:IPPROTO_IPV4----protocol num %d\n",IPPROTO_IPV4);
- break;
- case IPPROTO_IPV6:
- printf("ai_protocol:IPPROTO_IPV6----protocol num %d\n",IPPROTO_IPV6);
- break;
- case IPPROTO_TCP:
- printf("ai_protocol:IPPROTO_TCP----protocol num %d\n",IPPROTO_TCP);
- break;
- case IPPROTO_UDP:
- printf("ai_protocol:IPPROTO_UDP----protocol num %d\n",IPPROTO_UDP);
- break;
- default:
- printf("ai_protocol:unknow protocol----protocol num %d\n",add_tmp->ai_protocol);
- break;
- }
- printf("ai_addrlen:%d\n",add_tmp->ai_addrlen);
- printf("ai_canonname:%s\n",add_tmp->ai_canonname);
- // 网络地址
- printf("ai_addr:%s\n",inet_ntoa(((struct sockaddr_in*)&(add_tmp->ai_addr))->sin_addr));
- }
- }
- int addr_info()
- {
- /*
- int getaddrinfo(const char *restrict host,
- const char *restrict service,
- const struct addrinfo *restrict hint,
- struct addrinfo **restrict res);
- */
- /*
- struct addrinfo{
- int ai_flags; // AI_PASSIVE,AI_CANONNAME
- int ai_family;//地址族
- int ai_socktype;//socket类型
- int ai_protocol; //协议类型
- socklen_t ai_addrlen;//地址字节长度
- char *ai_canonname;//主机名
- struct sockaddr *ai_addr;//socket结构体
- struct addrinfo *ai_next;
- };
- */
- char *host="";//是主机名 ip地址
- //char *host="";
- //char *host="96.197.49.9";
- //char *host="61.135.169.105";
- //char *host="localhost";
- char *server="http";//server 一般是协议名称 或者协议号
- struct addrinfo *ailist, *aip;
- struct addrinfo hint;
- const char *addr;
- int err;
- char abuf[INET_ADDRSTRLEN];
- memset(&hint,0,sizeof(struct addrinfo));
- hint.ai_flags = AI_CANONNAME;
- //AI_CANONNAME:通知getaddrinfo函数返回主机的名字
- //AI_PASSIVE:该套接口是用作被动地打开
- hint.ai_family = AF_UNSPEC;
- //AF_INET:IPv4协议 AF_INET6:IPv6协议
- // AI_CANONNAME IPv4或IPv6均可 : AF_INET AF_INET6 都可以
- hint.ai_socktype = SOCK_DGRAM;
- //SOCK_STREAM:字节流套接字socket(TCP)
- //SOCK_DGRAM:数据报套接字socket(UDP)
- hint.ai_protocol = IPPROTO_UDP;//协议
- //IPPROTO_IP:IP协议
- //IPPROTO_IPV4:IPv4协议
- //IPPROTO_IPV6:IPv6协议
- //IPPROTO_UDP:UDP
- //IPPROTO_TCP:TCP
- hint.ai_addrlen = 0;
- hint.ai_canonname = NULL;
- hint.ai_addr = NULL;
- hint.ai_next = NULL;
- /*
- (1)通常服务器端在调用getaddrinfo()之前,ai_flags设置AI_PASSIVE,
- 用于 bind()函数(用于端口和地址的绑定,后面会讲到),主机名nodename通常会设置为NULL。
- (2)客户端调用getaddrinfo()时,ai_flags一般不设置AI_PASSIVE,但是主机名
- nodename和服务名servname(端口)则应该不为空。
- (3) 即使不设置ai_flags为AI_PASSIVE,取出的地址也可以被绑定,很多程序中ai_flags直接设置为0,
- 即3个标志位都不设置,这种情况 下只要hostname和servname设置的没有问题就可以正确绑定。
- */
- if ((err = getaddrinfo(host, server, &hint, &ailist)) != 0)
- printf("getaddrinfo error: %s", gai_strerror(err));
- for (aip = ailist; aip != NULL;aip=aip->ai_next)
- {
- prin_addrinfo(aip);
- //void freeaddrinfo(struct addrinfo *ai);
- }
- freeaddrinfo( ailist);
- return 0;
- }
- int name_info()
- {
- /*
- int getnameinfo(const struct sockaddr *restrict addr, socklen_t alen,
- char *restrict host, socklen_t hostlen,
- char *restrict service, socklen_t servlen,
- unsigned int flags);
- */
- /*
- addr: socket地址.
- alen: socket地址长度.
- host: 主机名.
- hostlen: 主机名长度.
- service: 服务名.
- servlen: 服务名长度.
- flags: 指定转换的控制方式. 可以有如下几种:
- NI_DGRAM: 服务基于数据报而非基于流.
- NI_NAMEREQD: 如果找不到主机名字, 将其作为一个错误对待.
- NI_NOFQDN: 对于本地主机, 仅返回完全限定域名的节点名部分.
- NI_NUMERICHOST: 以数字形式而非名字返回主机地址.
- NI_NUMERICSERV: 以数字形式而非名字返回服务地址(即端口号).
- 说明: 如果host或service非空, 它(们)将指向洋长度问该len字节的缓冲区用于存储返回的名. 如果为空则不返回该名.
- */
- struct sockaddr_in addr;
- char host[NI_MAXHOST];
- char serv[NI_MAXSERV];
- bzero( host, NI_MAXHOST );
- bzero( serv, NI_MAXSERV );
- addr.sin_family = AF_INET;
- addr.sin_port = htons(53);//23 telnet 端口 domain 53 端口
- addr.sin_addr.s_addr = inet_addr( "192.168.155.224" );//计算机的ip地址
- //得到机器的主机名称 和 对应端口所用到的协议
- getnameinfo( (struct sockaddr *)&addr, sizeof( struct sockaddr_in ),
- host, sizeof(host), serv, sizeof( serv ), NI_NOFQDN );
- printf("host = %s \n", host );
- printf("serv = %s \n", serv );
- return 0;
- }
- //使用gethostname()得到主机名 .
- //gethostbyname 得到详细信息
- int hostname_info()
- {
- struct hostent *hptr;
- char **pptr;
- char hostname[32];
- char str[32];
- memset(hostname,0,sizeof(hostname));
- memset(str,0,sizeof(str));
- printf("gethostname --------------\n");
- if(gethostname(hostname,sizeof(hostname)) )
- {
- printf("gethostname calling error\n");
- return 1;
- }
- printf("localhost name:%s\n",hostname);
- memset(hostname,0,sizeof(hostname));
- strcpy(hostname,"linurw");
- //临时设置计算机名 修改后使用 gethostbyname 后会出现错误,gethostbyname 无法得到相应的结构体
- if( sethostname(hostname,sizeof(hostname))<0 )
- {
- printf("sethostname calling error\n");
- return 1;
- }
- else
- {
- printf("sethostname success : %s\n",hostname);
- }
- memset(hostname,0,sizeof(hostname));
- if( gethostname(hostname,sizeof(hostname)) )
- {
- printf("gethostname calling error\n");
- return 1;
- }
- printf("localhost name:%s\n",hostname);
- if( (hptr = gethostbyname(hostname)) == NULL)
- {
- printf("gethostbyname calling error\n");
- return 1;
- }
- prin_host(hptr);
- }
- int uname_info()
- {
- //struct utsname{
- // char sysname[_UTS_NAMESIZE]; /* name of this operating system */
- // char nodename[_UTS_NODESIZE]; /* name of this node */
- // char release[_UTS_NAMESIZE]; /* O.S release level */
- // char version[_UTS_NAMESIZE]; /* O.S version level */
- // char machine[_UTS_NAMESIZE]; /* hardware type */
- // };
- struct hostent *hptr=NULL;
- struct utsname myname;
- char **pptr;
- char str[32];
- bzero(&myname,sizeof(myname));
- bzero(str,sizeof(str));
- printf("uname info:-------------\n");
- if(uname(&myname)<0)
- {
- printf("uname calling error\n");
- return 1;
- }
- printf("utsname.sysname: %s\n",myname.sysname);
- printf("utsname.nodename:%s\n",myname.nodename);
- printf("utsname.release: %s\n",myname.release);
- printf("utsname.version: %s\n",myname.version);
- printf("utsname.machine: %s\n",myname.machine);
- if( (hptr = gethostbyname(myname.nodename)) == NULL)
- {
- printf("gethostbyname calling error\n");
- return 1;
- }
- prin_host(hptr);
- }
- int hostbyaddr_info()
- {
- char *ptr="192.168.155.224";
- char **pptr;
- struct hostent *hptr=NULL;
- char str[32];
- char ipaddr[16];
- bzero(str,sizeof(str));
- bzero(ipaddr,sizeof(ipaddr));
- struct in_addr hipaddr;
- bzero(&hipaddr,sizeof(hipaddr));
- /* 取得命令后第一个参数,即要解析的IP地址 */
- /* 调用inet_aton(),ptr就是以字符串存放的地方的指针,hipaddr是in_addr形式的地址 */
- printf("gethostbyaddr-------------\n");
- if(!inet_aton(ptr,&hipaddr))
- {
- printf("inet_aton error\n");
- return 1;
- }
- /* 调用gethostbyaddr()。调用结果都存在hptr中 */
- if( (hptr = gethostbyaddr(&hipaddr,sizeof(hipaddr), AF_INET) ) == NULL )
- {
- printf("gethostbyaddr error for addr:%s\n", ptr);
- return 1; /* 如果调用gethostbyaddr发生错误,返回1 */
- }
- prin_host(hptr);
- }
- //getdomainname(2)函数:可以允许程序获得程序正运行的主机的NIS域名。
- //这个函数的用法也gethostname相同。
- int domain_info()
- {
- char domainname[32];
- char str[32];
- memset(domainname,0,sizeof(domainname));
- memset(str,0,sizeof(str));
- printf("getdomainname --------------\n");
- //int getdomainname(char *name, size_t len);
- //int setdomainname(const char *name, size_t len);
- strcpy(domainname,"water.com");
- setdomainname(domainname,sizeof(domainname));
- memset(domainname,0,sizeof(domainname));
- if(getdomainname(domainname,sizeof(domainname)) )
- {
- printf("getdomainname calling error\n");
- return 1;
- }
- printf("getdomainname name:%s\n",domainname);
- }
- int main(int argc,char **argv)
- {
- host_info();//gethostent()
- net_info();//getnetent()
- protoent_info();//getprotoent()
- serve_info();//getserveinfo()
- addr_info();//getaddrinfo()
- name_info();//getnameinfo()
- hostname_info();//gethostname()
- uname_info();//uname()
- hostbyaddr_info();//gethostbyaddr
- domain_info();//getdomainname
- }
阅读(4091) | 评论(0) | 转发(0) |