Linux Programming--Network Information (15th chapter)

Source: Internet
Author: User

Tag: Nat Print returns an HTTP list + + instead of a number

15.3 Network information as of now, the client and server programs have been the bar address and the port number compiled into their own internals.

For a more generic server and client program. It is possible to use network information functions to determine which address and port should be used.
Assume that you have sufficient permissions to add your own services to the list of known services in the/etc/services file. In this file, the port number is assigned a name, allowing the user to use the symbolic service name instead of the number of the port number.
In a similar way. Assume the name of a given computer. Can determine its IP address by invoking the host database function that resolves the address . These functions complete this work by querying the network configuration file, such as/etc/hosts files or network information services.
The Network information service that is used frequently has the NIS (Information services. Network information Services) and DNS (domain name service, name Services).
The host database function is declared in the interface header file Netdb.h. For example, see the following:

#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 includes 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) */};
Assuming there are no data items associated with the host or address of the query, these information functions will return a null pointer.


Similarly, information related to the service and its associated port number can also be obtained through some service information functions. For example, see the following:

#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. The former is used for sock_stream types of TCP connections, which are used for SOCK_DGRAM types of UDP datagrams.
The structure servent includes 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" */};
Suppose you want to get the host database information for a computer. Ability to 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 a function inet_ntoa, for example, is seen below:
#include <arpa/inet.h>char *inet_nto (struct in_addr in);
What this function does is. Converts an Internet host address into a string of four-tuple format.

It returns-1 on failure.

Other new functions that are available are gethostname, which are defined for example, as seen below:

#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, assuming that the host name returned is too long and it is truncated.

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 can use the GETHOSTBYADDR function to find out which host has the given IP address. This function can be used on the server to find 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.

When executing this program and specifying the hostname Google, the program gives the Ethernet and Demodulator two network interface information, when using hostname localhost, the program only gives the loop network information, such as the following see:

1

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.