Linux Programming--Network Information (15th chapter)

Source: Internet
Author: User
Tags aliases

15.3 Network information so far, clients and server programs have been the bar addresses and port numbers compiled into their own internals. for a more general purpose server and client, you can use network information functions to determine which address and port should be used.
If you have sufficient permissions, you can add your own service to the list of known services in the/etc/services file and assign a name to the port number in this file, allowing the user to use the symbolic service name instead of the number of the port number.
Similarly, if given the name of a computer, you can determine its IP address by calling the host database function that resolves the address。 These functions do this by querying a network configuration file, such as/etc/hosts files or network information services.
The commonly used Network information Services are NIS (Network Information Service, Web Information Services) and DNS (domain name service, name Services).
The host database functions are declared in the interface header file Netdb.h as follows:
#include <netdb.h>struct hostent *gethostbyaddr (const void *addr, size_t len, int type); struct hostent *gethostbyna Me (const char *name);
These functions return a structure that contains at least the following members:
struct Hostent {    char *h_name;            /* Name of the host */    char **h_aliases;        /* List of aliases (nicknames) */    int h_addrtype;            /* Address type */    int h_length;                /* length in bytes of the address */    char **h_addr_list;        /* List of Address (network order) */};
If there are no data items associated with the host or address of the query, these information functions return a null pointer.
Similarly, information related to the service and its associated port number can also be obtained through some service information functions. As shown below:
#include <netdb.h>struct servent *getservbyname (const char *name, const char *proto); struct servent *getservbyport (int port, const char *proto);
The proto parameter specifies the protocol used to connect to the service, and its two values are TCP and UDP, which are used for TCP connections of type Sock_stream, which are used for UDP datagrams of type Sock_dgram.
The structure servent contains at least the following members:
struct Servent {    char *s_name;        /* Name of the service */    char **s_aliases;    /* List of aliases (alternative names) */    int s_port;            /* The IP port number */    char *s_proto;        /* The service type, usually "TCP" or "UDP" */};
If you want to obtain the host database information for a computer, you can call the GetHostByName function and print the results. Note that to convert the returned address list to the correct address type, use the function Inet_ntoa to convert them from the network byte order to the printed string. The definition of the function Inet_ntoa is as follows:
#include <arpa/inet.h>char *inet_nto (struct in_addr in);
The purpose of this function is to convert an Internet host address into a string of four-tuple-formatted strings. It returns-1 on failure. Other new functions available are gethostname, which are defined as follows:
#include <unistd.h>int gethostname (char *name, int namelength);
The purpose of this function is to write the name of the current host to the string that the name points to. The host name will be null-terminated. The parameter namelength specifies the length of the string, which is truncated if the host name returned is too long. GetHostName returns 0 on success and 1 on failure.
The following program getname.c is used to obtain information about a host computer.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <netdb.h> #include <    Netinet/in.h> #include <arpa/inet.h>int main (int agrc, char *argv[]) {char *host, **name, **addrs;        struct Hostent *hostinfo;        if (argc = = 1) {char myname[256];        GetHostName (myname, 255);    host = MyName;    } else host = argv[1];    Hostinfo = gethostbyname (host);        if (hostinfo) {fprintf (stderr, "Cannot get info for host:%s\n", host);    Exit (1);    } printf ("Results for host%s:\n", host);    printf ("Name:%s\n", hostinfo->h_name);    printf ("Aliases:");    Name = hostinfo->h_aliases;        while (*names) {printf ("%s", *names);    names++;        } printf ("\ n");        if (hostinfo->h_addrtype! = af_inet) {fprintf (stderr, "not an IP host!\n");    Exit (1);    } Addrs = hostinfo->h_addr_list;      while (*addrs) {printf ("%s", Inet_ntoa (* (struct in_addr *) *addrs));  addrs++;    } printf ("\ n"); Exit (0);}
In addition, you can use the GETHOSTBYADDR function to find out which host has a given IP address, and you can use this function on the server to locate the source of the connecting customer.
Program parsing
The getname program extracts the host's information from the host database by calling GetHostByName. It prints out the host name, its alias, and the IP address that the host uses on its network interface. Run this program and specify the hostname of Google, the program gives the Ethernet and Demodulator two network interface information, when using host name localhost, the program only gives the loop network information, as follows:

1

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Programming--Network Information (15th chapter)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.