网络序和主机序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DSS_SUCCESS 0
#define DSS_ERROR -1
typedef  unsigned  int  uint32;      /* Unsigned 32 bit value */
typedef  signed  int    int32;       /* Signed 32 bit value */
typedef  unsigned char      uint8;       /* Unsigned 8  bit value */
#define ps_htonl(x)                                                      \
  (((((uint32)(x) & 0x000000FFU) << 24) |                                \
  (((uint32)(x) & 0x0000FF00U) <<  8) |                                  \
  (((uint32)(x) & 0x00FF0000U) >>  8) |                                  \
  (((uint32)(x) & 0xFF000000U) >> 24)))

#define dss_htonl(x) ps_htonl(x)
typedef long long     int64;

struct ps_in_addr  /* structure defined for historic reasons.*/
{
  uint32 ps_s_addr; /**< Socket address.*/
};
#define in_addr          ps_in_addr

/*===========================================================================
FUNCTION  DSS_INET_ATON()

DESCRIPTION
  Convert internet address from dotted string to network ordered struct
  ps_in_addr.

DEPENDENCIES
  None.

RETURN VALUE
  DSS_ERROR    For failure
  DSS_SUCCESS  For success

  The IP address is returned in struct ps_in_addr *addr

SIDE EFFECTS
  None.
===========================================================================*/
int32 dss_inet_aton
(
  const  char       *cp,
  struct ps_in_addr *addr
)
{
  enum
  {
    BASE_OCT = 8,
    BASE_DEC = 10,
    BASE_HEX = 16
  }         base        = BASE_DEC;
  uint32    index       = 0;
  int32     number      = 0;
  uint32    value       = 0;
  uint32    ipaddr      = 0;
  uint32    check_value = 0;
  uint8    *presult     = (uint8 *) &ipaddr;
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

   if( NULL == cp || NULL == addr || 0 == strlen(cp) )
   {
      return DSS_ERROR;
   }

  /*-------------------------------------------------------------------------
    The ASCII string containing the address could be in one of 4 forms:
    i  ) a.b.c.d - each of a,b,c and d is one octet of IPv4 address
    ii ) a.b.c   - a and b are most significant octets, c is remaining 16
                   bits of address.
    iii) a.b     - a is most significant octet while b is remaining 24 bits
    iv ) a       - a is the complete address

    Each of a,b,c and d can be a decimal number, an octal number or a hex
    number.
  -------------------------------------------------------------------------*/
  for(index = 0; index < sizeof(struct ps_in_addr); index++)
  {
   /*------------------------------------------------------------------------
     Determine the base of the number.  If number starts with 0x or 0X then
     base is 16, if the number starts with 0 the next charachter being
     neither x nor X then base 8 else base 10.
    -----------------------------------------------------------------------*/
    base  = BASE_DEC;
    value = 0;
    if( '0' == *cp )
    {
      /*---------------------------------------------------------------------
        Either a hex or an octal number follows or the number is zero
      ---------------------------------------------------------------------*/
      cp++;
      base = ( 'X' == *cp || 'x' == *cp ) ?  BASE_HEX : BASE_OCT;
      if( BASE_HEX == base )
      {
        /*-------------------------------------------------------------------
          Skip the 'x' or 'X'.
        -------------------------------------------------------------------*/
        cp++;
      }
    }

   /*------------------------------------------------------------------------
     Find the rest of the number.
    -----------------------------------------------------------------------*/
    while( '.' != *cp && 0 != *cp )
    {
      number = *cp - '0';
      switch(base)
      {
        case BASE_DEC:
        case BASE_OCT:
          if(number < 0 || number > ((int32)base - 1))
          {
            /*---------------------------------------------------------------
              Illegal number
            ---------------------------------------------------------------*/
            return DSS_ERROR;
          }
          break;

        case BASE_HEX:
          /*-----------------------------------------------------------------
            Convert to decimal.
          -----------------------------------------------------------------*/
          if(number < 0 || number > 9)
          {
            number = *cp - 'a' + 10;
            if (number < 10 || number > 15)
            {
              number = *cp - 'A' + 10;
              if(number < 10 || number > 15)
              {
                return DSS_ERROR;
              }
            }
          }
          break;

        default:
          return DSS_ERROR;
      } /* switch */

      cp++;

      /*---------------------------------------------------------------------
        Verify that the number can be contained in a 32 bit quantity.
      ---------------------------------------------------------------------*/
      if( ((int64) value * (int) base) + number > (int64) 0xffffffff )
      {
        return DSS_ERROR;
      }

      value = (value * (int)base) + (uint32)number;
    } /* while */

    /*-----------------------------------------------------------------------
      Each '.' seperated number should be at most 0xff.
    -----------------------------------------------------------------------*/
    if(value > 0xff && *cp != 0)
    {
      return DSS_ERROR;
    }
    else
    {
      if( 0 != *cp )
      {
        printf("if branch,value:%d\n", value);
        *presult++ = (uint8)value;
      }
      else
      {
        printf("else branch,value:0x%x,index:%d\n",value,index);
        /*-------------------------------------------------------------------
          Verify that the last number is in the correct range.
          -------------------------------------------------------------------*/
        switch( index )
        {
          case 0:
            check_value = 0xffffffffU;
            break;

          case 1:
            check_value = 0xffffff;
            break;

          case 2:
            check_value = 0xffff;
            break;

          case 3:
            check_value = 0xff;
            break;

          default:
            return DSS_ERROR;
        } /* switch */

        if( value > check_value )
        {
          return DSS_ERROR;
        }
        printf("ipaddr:0x%x, dss_htonl(value):0x%x\n", ipaddr, dss_htonl(value));

        ipaddr |= dss_htonl(value);
      }
    }

    /*-----------------------------------------------------------------------
      Break if this was the last number.
    -----------------------------------------------------------------------*/
    if(*cp++ == 0)
    {
      break;
    }
  } /* for */

  addr->ps_s_addr = ipaddr;

  return DSS_SUCCESS;
} /* dss_inet_aton() */
void print(uint32 ip_addr)
{
    uint8* octet = (uint8*)&ip_addr;
    for (int index = 0; index < sizeof(struct ps_in_addr); index++,octet++)
        printf("address of octet: 0x%p, value of octet: %d\n", octet, *octet);
}
void print_ip_address(char* ip_str, struct in_addr* ip_address)
{
    printf("original ip string:%s\n", ip_str);
    dss_inet_aton(ip_str, ip_address);
    printf("address: 0x%x\n", ip_address->ps_s_addr);
    print(ip_address->ps_s_addr);
}

int main()
{
    char * ip_str = "117.131.85.139";
    struct in_addr ip_address;
    uint32 ip1 = 0x7583558B;
    printf("ip1:0x%x\n", ip1);
    print(ip1);
    printf("----------------a.b.c.d format--------------\n");
    print_ip_address(ip_str, &ip_address);
    printf("----------------a format--------------\n");
    ip_str = "0x7583558B";
    print_ip_address(ip_str, &ip_address);
    printf("----------------a.b format--------------\n");
    ip_str = "0x75.0x83558B";
    print_ip_address(ip_str, &ip_address);
    printf("----------------a.b.c format--------------\n");
    ip_str = "0x75.0x83.0x558B";
    print_ip_address(ip_str, &ip_address);
    return 0;
}
mali@mali:~/code/aton/aton$ gcc main.c -o main -std=c11
mali@mali:~/code/aton/aton$ ./main
ip1:0x7583558b
address of octet: 0x0x7ffced15587c, value of octet: 139
address of octet: 0x0x7ffced15587d, value of octet: 85
address of octet: 0x0x7ffced15587e, value of octet: 131
address of octet: 0x0x7ffced15587f, value of octet: 117
----------------a.b.c.d format--------------
original ip string:117.131.85.139
if branch,value:117
if branch,value:131
if branch,value:85
else branch,value:0x8b,index:3
ipaddr:0x558375, dss_htonl(value):0x8b000000
address: 0x8b558375
address of octet: 0x0x7ffced15585c, value of octet: 117
address of octet: 0x0x7ffced15585d, value of octet: 131
address of octet: 0x0x7ffced15585e, value of octet: 85
address of octet: 0x0x7ffced15585f, value of octet: 139
----------------a format--------------
original ip string:0x7583558B
else branch,value:0x7583558b,index:0
ipaddr:0x0, dss_htonl(value):0x8b558375
address: 0x8b558375
address of octet: 0x0x7ffced15585c, value of octet: 117
address of octet: 0x0x7ffced15585d, value of octet: 131
address of octet: 0x0x7ffced15585e, value of octet: 85
address of octet: 0x0x7ffced15585f, value of octet: 139
----------------a.b format--------------
original ip string:0x75.0x83558B
if branch,value:117
else branch,value:0x83558b,index:1
ipaddr:0x75, dss_htonl(value):0x8b558300
address: 0x8b558375
address of octet: 0x0x7ffced15585c, value of octet: 117
address of octet: 0x0x7ffced15585d, value of octet: 131
address of octet: 0x0x7ffced15585e, value of octet: 85
address of octet: 0x0x7ffced15585f, value of octet: 139
----------------a.b.c format--------------
original ip string:0x75.0x83.0x558B
if branch,value:117
if branch,value:131
else branch,value:0x558b,index:2
ipaddr:0x8375, dss_htonl(value):0x8b550000
address: 0x8b558375
address of octet: 0x0x7ffced15585c, value of octet: 117
address of octet: 0x0x7ffced15585d, value of octet: 131
address of octet: 0x0x7ffced15585e, value of octet: 85
address of octet: 0x0x7ffced15585f, value of octet: 139
mali@mali:~/code/aton/aton$ 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值