close

C/C++ : IPv4字串轉換成IPv4數值格式

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
This article was contributed by Jacky Xie.
Environment: VC6,BCB1/3/4/5/6,Linux
*/
// static functions implementation to operate with project- relative file
// names an include might be needed if not specified already somewhere


// ===============================================================
// 將ipv4字串轉換成ipv4數值格式
// ===============================================================

//---------------------------------------------------------------------------
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

#pragma hdrstop

//---------------------------------------------------------------------------
#pragma argsused
#ifndef CHAR
#define CHAR   char
#endif
typedef unsigned long u_long;
typedef unsigned char   u_char;
typedef unsigned short  u_short;

/*
typedef struct in_addr
{
    u_long  s_addr;
} in_addr;
*/
typedef struct in_addr {
        union 
        {
                struct  { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
                struct  { u_short s_w1,s_w2; } S_un_w;
                u_long  S_addr;
#define s_addr  S_un.S_addr   /* can be used for most tcp & ip code */                
        } S_un;
} in_addr;

int ip_atoi(CHAR* argv, CHAR* ip)
{
	int   i;
	int   value;
	CHAR  tmp_ip[4];

	for (i = 0; i < 4; i++)
	{
		value = atoi(argv);

		if (value >255)
			return -1;

		tmp_ip[i] = (CHAR)value;

		if (((argv = strchr(argv, '.')) == NULL) && (i < 3))
			return -1;

		argv++; 
	}

	for (i = 0; i < 4; i++)
                ip[i] = tmp_ip[3 - i];

    return i;   
}


int inet_aton(char *a, struct  in_addr *b)
{
    int     value=0;
    char    c_ip[4];
 
 
    memset(c_ip, 0, 4);

    value = ip_atoi(a, c_ip);
 
    memcpy((char *)&b->s_addr, c_ip, 4);
 
    if(value < 4)
        return 0;
    else
        return 1;
}

int Check_IP(char* str)
{
    int   i;
    int   ip[4];
    char  tmp[16];

    if (!str)
        return 0;

    if (sscanf(str, "%d.%d.%d.%d%s", &ip[0], &ip[1], &ip[2], &ip[3], tmp) != 4)
	return 0;

    for (i = 0; i <= 3; i++)
    {
	if (ip[i] < 0 || ip[i] > 255)
		return 0;
    }

    return 1;
}

void checkip_help_cmd(void)
{
    printf("nShow checkip commands:n");
    printf("    checkip [ip-addr]n");
}

// ===============================================================
// example of the function use
// ===============================================================
int main(int argc, char* argv[])
{
        unsigned char* ip = "234.53.198.76";
        in_addr ipaddr;
        inet_aton(ip,&ipaddr);
        printf("%d.%d.%d.%dn",ipaddr.S_un.S_un_b.s_b4,ipaddr.S_un.S_un_b.s_b3,
                               ipaddr.S_un.S_un_b.s_b2,ipaddr.S_un.S_un_b.s_b1);
        system("pause");

	// =======================
	// 檢驗 ip-addr 是否符合 ipv4 格式
	// =======================
        if(argc<2)
    	{
       		checkip_help_cmd();
       		return 0;
    	}
    	if (!Check_IP(argv[1]))
    	{
    		printf("IP Address is invalid !!!n");
    	}
    	else
    	{
        	printf("IP Address is valid !!!n");
    	}
    	getch();
        system("pause");

        return 0;
}
//---------------------------------------------------------------------------
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Bluelove1968 的頭像
    Bluelove1968

    藍色情懷

    Bluelove1968 發表在 痞客邦 留言(0) 人氣()