ip地址转化代码实例

/*@author: lgh@

 *

 * */

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include <netinet/in.h>//ntohl

#include <arpa/inet.h> //inet_addr

#include <sys/types.h>

#ifdef UNICODE

#define T(x)          __TXT(x)

#define __TXT(s)      L ## s

#else

#define T(s)          s

#endif /* UNICODE */

/*

#if defined (DEBUG_DISABLE)

#undef DEBUG_DISABLE

#else

#endif

*/



/*********************************************************************/

#ifndef DEBUG_DISABLE

/*

#define lprint(tag, fmt...)     \

do{if(tag)printf(fmt);}while(0)

*/

#define lprint(x, ...)  do{printf(x, ##__VA_ARGS__);}while(0)

#else

#define lprint(tag, fmt...)

#endif





/*********************************************************************/

#ifdef CONFIG_DUAL_IMAGE

#if defined (CONFIG_RT2880_FLASH_2M)

#define IMAGE1_SIZE             0x100000

#elif defined (CONFIG_RT2880_FLASH_4M)

#define IMAGE1_SIZE             0x200000

#elif defined (CONFIG_RT2880_FLASH_8M)

#define IMAGE1_SIZE             0x400000

#elif defined (CONFIG_RT2880_FLASH_16M)

#define IMAGE1_SIZE             0x800000

#elif defined (CONFIG_RT2880_FLASH_32M)

#define IMAGE1_SIZE             0x1000000

#else

#define IMAGE_SIZE      TEST

#endif

#endif//end #ifdef,否则会出现25:1: error: unterminated #ifdef,这样的错





/*********************************************************************/

typedef void* (*square_t)(int* x);



void* square (int* x)

{

        *x *= *x;

        return (void*)square;

}



int lt1224(void)

{

        int x = 2;

        ((square_t)((square_t)((square_t)square (&x)) (&x)) (&x)) (&x);

        printf ("%d\n", x);

        return 0;

}

/*********************************************************************/

#ifndef NIP4

#define NIP4_FMT "%u.%u.%u.%u"

#define NIP4(addr)                      \

        ((unsigned char *)&addr)[0],    \

        ((unsigned char *)&addr)[1],    \

        ((unsigned char *)&addr)[2],    \

        ((unsigned char *)&addr)[3]



#define HIP4_FMT "%u.%u.%u.%u"

#define HIP4(addr)                      \

        ((unsigned char *)&addr)[3],    \

        ((unsigned char *)&addr)[2],    \

        ((unsigned char *)&addr)[1],    \

        ((unsigned char *)&addr)[0]

#endif



#ifndef NIP6

#define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"

#define NIP6(addr)                    \

    (unsigned)((addr)[0]),            \

    (unsigned)((addr)[1]),            \

    (unsigned)((addr)[2]),            \

    (unsigned)((addr)[3]),            \

    (unsigned)((addr)[4]),            \

    (unsigned)((addr)[5]),            \

    (unsigned)((addr)[6]),            \

    (unsigned)((addr)[7]),            \

    (unsigned)((addr)[8]),            \

    (unsigned)((addr)[9]),            \

    (unsigned)((addr)[10]),            \

    (unsigned)((addr)[11]),            \

    (unsigned)((addr)[12]),            \

    (unsigned)((addr)[13]),            \

    (unsigned)((addr)[14]),            \

    (unsigned)((addr)[15])

#endif



/*

#ifdef __CHECKER__

#define __bitwise__ __attribute__((bitwise))

#else

#define __bitwise__

#endif

#ifdef __CHECK_ENDIAN__

#define __bitwise __bitwise__

#else

#define __bitwise

#endif

typedef __u16 __bitwise __le16;

typedef __u16 __bitwise __be16;

typedef __u32 __bitwise __le32;

typedef __u32 __bitwise __be32;

typedef __u64 __bitwise __le64;

typedef __u64 __bitwise __be64;

*/



typedef __signed__ char __s8;

typedef unsigned char __u8;



typedef __signed__ short __s16;

typedef unsigned short __u16;



typedef __signed__ int __s32;

typedef unsigned int __u32;



typedef __signed__ long __s64;

typedef unsigned long __u64;



union nf_inet_addr {

        __u32           all[4];

        __u32           ip;//   __be32          ip;

//      __be32          ip6[4];

        struct in_addr  in;

        struct in6_addr in6;

};

/*********************************************************************/



union endian{

        int i;

        char c;

};



int main(int argc, char *argv[])

{

        char *s="0123456789012345678901234567890";

        char *p;

        p=strpbrk(s,"a1 839"); /*1会最先在s字符串中找到*/

        lprint("%s\n",p);

        p=strpbrk(s,"4398");/*3 会最先在s 字符串中找到*/

        lprint("%s\n",p);

        lprint("%s\n", T("deling"));



        lprint("************************************************************************\n");

        lt1224();



        lprint("************************************************************************\n");

        if(argc != 2)

        {

                return 1;

        }

        union endian le;

        le.i = 0x3456;/*16bit宽的数*/

        if(le.c == 0x56){

                lprint("little endian\n");

        }else lprint("big endian\n");

        printf("%0x, %0x\n", le.i, le.c);



        char ipdot[32] = {0};/*点分十进制*/

        long iphost = 0;/*主机字节序,x86一般都是小端模式*/

        long ipnet = 0;/*网络字节序,大端模式*/



        strncpy(ipdot, argv[1], sizeof(ipdot));

        lprint("Get ipdot:%s\n", ipdot);



        ipnet = inet_addr(ipdot);

        lprint("Get ipnet:%ld:"NIP4_FMT"\n", ipnet, NIP4(ipnet));

        iphost = ntohl(ipnet);

        lprint("Get iphost:%ld:"NIP4_FMT"\n", iphost, NIP4(iphost));

        lprint("Get iphost:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost));

        iphost++;

        lprint("Get iphost++:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost));



                /*net --> host*/

        unsigned array[4];

        array[0] = (ntohl(ipnet) >> 24) & 0xFF;

        array[1] = (ntohl(ipnet) >> 16) & 0xFF;

        array[2] = (ntohl(ipnet) >> 8) & 0xFF;

        array[3] = ntohl(ipnet) & 0xFF;

        lprint(NIP4_FMT"\n", array[0], array[1], array[2], array[3]);



        lprint("%ld.%ld.%ld.%ld\n", ipnet & 0xff,

                        ipnet>>8 & 0xff,

                        ipnet>>16 & 0xff,

                        ipnet>>24 & 0xff);

        lprint("************************************************************************\n");

        return 1;

}

 

你可能感兴趣的:(IP地址)