inetaddr.c

来自「用于嵌入式系统的TCP/IP协议栈」· C语言 代码 · 共 70 行

C
70
字号
/***********************************************************************//*                                                                     *//*   Module:  inetaddr.c                                               *//*   Release: 2001.3                                                   *//*   Version: 99.0                                                     *//*   Purpose: inet_addr() implementation                               *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               Copyright 1999, Blunk Microsystems                    *//*                      ALL RIGHTS RESERVED                            *//*                                                                     *//*   Licensees have the non-exclusive right to use, modify, or extract *//*   this computer program for software development at a single site.  *//*   This program may be resold or disseminated in executable format   *//*   only. The source code may not be redistributed or resold.         *//*                                                                     *//***********************************************************************/#include "../tcp_ipp.h"#include <ctype.h>/***********************************************************************//* Global Function Definitions                                         *//***********************************************************************//***********************************************************************//*   inet_addr: Convert dotted-decimal address to integer              *//*                                                                     *//*       Input: ptr = pointer to dotted decimal IP address string      *//*                                                                     *//*     Returns: integer IP address                                     *//*                                                                     *//*        Note: Address is returned in network byte order              *//*                                                                     *//***********************************************************************/ui32 inet_addr(const char *ptr){  int i, n;  ui32 lword = 0;  /*-------------------------------------------------------------------*/  /* Loop once for each byte converted.                                */  /*-------------------------------------------------------------------*/  for (n = 4;; lword <<= 8)  {    /*-----------------------------------------------------------------*/    /* Convert one byte's worth and write OR to 32-bit word.           */    /*-----------------------------------------------------------------*/    for (i = 0; isdigit(*ptr); ++ptr)      i = (10 * i) + (*ptr - '0');    lword |= (i & 0xFF);    /*-----------------------------------------------------------------*/    /* If finished, return long word IP address.                       */    /*-----------------------------------------------------------------*/    if (--n == 0)      return htonl(lword);    /*-----------------------------------------------------------------*/    /* Check for '.' between each decimal number.                      */    /*-----------------------------------------------------------------*/    if (*ptr++ != '.')    {      NetError(NULL, EFAULT);      return (ui32)-1;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?