Implement Inet_aton and Inet_ntoa function functions in C language

Source: Internet
Author: User
Tags manual

Previously encountered to achieve their own Inet_aton and inet_ntoa function function of the problem, summed up here.

1 Basic knowledge

The network byte order is the big big pattern, then the low memory address is storing the data high, in the memory high address is storing the data low. The Inet_aton converts the IPV4 address (Point division) to the corresponding decimal integer, and Inet_ntoa converts the decimal integer to the corresponding IPv4 address (dot-notation).

2 full Code

#include <stdio.h>
#include <ctype.h>
#include "Print.h"
#define Ip_adress "192.168.1.177"

/*
* Convert the IPV4 address (Point division) to the corresponding decimal integer
* Original function declaration:
* int Inet_aton (const char *CP, struct in_addr *inp);
*/
unsigned int __inet_aton (const char *C_IPADDR)
{
unsigned int u_ipaddr = 0;
unsigned int u_tmp = 0;
char c;
int i_base = 10;
int i_shift = 0;
int i_recycle = 0;

c = *c_ipaddr;
while (1)
{
u_tmp = 0;
while (1)
{
If it's a number,
if (IsDigit (c))
{
U_tmp = (U_TMP * i_base) + (c-0x30);
c = *++c_ipaddr;
}
Else
{
If it's not a number but a. Symbol, description. The data before the symbol has been processed
Break
}
}

BYTE shift, note network byte order is big-endian mode
I_shift = 8*i_recycle++;
U_tmp <<= I_shift;
U_ipaddr + = u_tmp;

to point (.) Processing of symbols
if (c = = '. ')
{
c = *++c_ipaddr;
}
Else
{
If other symbols (such as a terminator, etc.) jump out of the loop
Break
}
}

Check for symbols that contain a non-terminator, space, and then return 0
if (c!= '!isspace ' && (c))
Goto RET_0;

return u_ipaddr;
RET_0:
return (0);
}

/**
* Converts a decimal integer to the corresponding IPV4 address (point division), where the first byte corresponds to the low byte[0 of the integer because the network byte order is a big-endian representation]
* Original function declaration:
* int Inet_aton (const char *CP, struct in_addr *inp);
*/

static Char buffer[16];
char * __INET_NTOA (unsigned int in)
{
unsigned char *bytes = (unsigned char *) &in;
snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d", Bytes[0], bytes[1], bytes[2], bytes[3]);
return buffer;
}

int main (int argc, char **argv)
{
PRINT ("ip_adress[%s] ==> [%lu].", Ip_adress, __inet_aton (ip_adress));
PRINT ("[%lu]=[0x%x].", __inet_aton (ip_adress), __inet_aton (ip_adress));
PRINT ("Network Orders[%lu] ==> [%s].", __inet_aton (ip_adress), __inet_ntoa (__inet_aton (ip_adress)));
return 0;
}


3 Compilation Execution

Use the makefile file in the article "Generic Makefile for generating elf, dynamic/static library files in Linux C + + project" To compile the program (you can, of course, compile GCC network_selfdefine.c-o with commands) Network_ Selfdefine), then executes the program and gets the result as shown in the following figure:

Implement Inet_aton and Inet_ntoa functions yourself

What needs to be explained is [2969675968]=[0xb101a8c0] output from line 87th. As you can see, the low byte 0xc0 (ie 192) is stored in memory as a result of the relationship of the network byte sequence big-endian mode, and the high byte 0xb1 (i.e. 177) is stored in the low memory.

4 Appendix

About Inet_aton's man description:

[Vfhky@typecodes xlei]$ Mans Inet_aton
INET (3) Linux Programmer ' s Manual INET (3)

NAME
Inet_aton, inet_addr, Inet_network, Inet_ntoa, Inet_makeaddr, Inet_lnaof,
Inet_netof-internet Address manipulation Routines

Synopsis
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int Inet_aton (const char *CP, struct in_addr *inp);

in_addr_t inet_addr (const char *CP);

in_addr_t inet_network (const char *CP);

Char *inet_ntoa (struct in_addr in);

struct IN_ADDR inet_makeaddr (int net, int host);

in_addr_t inet_lnaof (struct in_addr in);

in_addr_t inet_netof (struct in_addr in);

Feature Test Macro Requirements for glibc (in the following Feature_test_macros (7)):

Inet_aton (), Inet_ntoa (): _bsd_source | | _svid_source

DESCRIPTION
Inet_aton () Converts the Internet host address CP from the IPV4 numbers-and-dots
notation into binary form (in network byte) and stores it in the structure
That's inp points to. Inet_aton () returns nonzero if is valid, zero if
Not.


about Inet_aton's man description:


[Vfhky@typecodes ~]$ Mans Inet_ntoa
INET (3) Linux Programmer ' s Manual INET (3)

NAME
Inet_aton, inet_addr, Inet_network, Inet_ntoa, Inet_makeaddr, Inet_lnaof,
Inet_netof-internet Address manipulation Routines

Synopsis
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int Inet_aton (const char *CP, struct in_addr *inp);

in_addr_t inet_addr (const char *CP);

in_addr_t inet_network (const char *CP);

Char *inet_ntoa (struct in_addr in);

struct IN_ADDR inet_makeaddr (int net, int host);

in_addr_t inet_lnaof (struct in_addr in);

in_addr_t inet_netof (struct in_addr in);

Feature Test Macro Requirements for glibc (in the following Feature_test_macros (7)):

Inet_aton (), Inet_ntoa (): _bsd_source | | _svid_source

DESCRIPTION
Inet_aton () Converts the Internet host address CP from the IPV4 numbers-and-dots
notation into binary form (in network byte) and stores it in the structure
That's inp points to. Inet_aton () returns nonzero if is valid, zero if
Not.

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.