IP address/地址 检查
1、Determine if a string is a valid IP address in C
Beej's Guide to Network Programming
2、9.14. inet_ntop(), inet_pton()
3、Program to validate an IP address
step 1) Parse string with “.” as delimiter using “strtok()” function.
e.g. ptr = strtok(str, DELIM);
step 2)
……..a) If ptr contains any character which is not digit then return 0
……..b) Convert “ptr” to decimal number say ‘NUM’
……..c) If NUM is not in range of 0-255 return 0
……..d) If NUM is in range of 0-255 and ptr is non-NULL increment “dot_counter” by 1
……..e) if ptr is NULL goto step 3 else goto step 1
step 3) if dot_counter != 3 return 0 else return 1.
方法Ⅰ
// Program to check if a given string is valid IPv4 address or not
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIM "."
/* return 1 if string contain only digits, else return 0 */
int valid_digit(char *ip_str)
{
while (*ip_str) {
if (*ip_str >= '0' && *ip_str <= '9')
++ip_str;
else
return 0;
}
return 1;
}
/* return 1 if IP string is valid, else return 0 */
/* Check whether ip_address conforms to xxx.xxx.xxx.xxx, and there are 3 dots/'.' */
/* 暫時只能檢查符合常規IP地址的字符串,比如192.168.3.100, 如果是 8、192.167.8等這種非常規的,則處理不了。 */
int is_valid_IPV4(char * ip_str)
{
int i, num, dots = 0;
char *ptr;
if(ip_str == NULL)
return 0;
// 15+1 can't be replace by sizeof(ip_str), as the sizeof(*ip_str) is 4.
char *tmpstr = (char *)malloc(15+1);
memset(tmpstr, 0, sizeof(tmpstr));
memcpy(tmpstr, ip_str, 15);
printf("tmpstr is %s\n", tmpstr);
// See following link for strtok()
// http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok_r.html
//ptr = strtok(ip_str, DELIM);
ptr = strtok(tmpstr, DELIM);
if (ptr == NULL){
free(tmpstr);
return 0;
}
while (ptr) {
/* after parsing string, it must contain only digits */
if (!valid_digit(ptr)){
free(tmpstr);
return 0;
}
num = atoi(ptr);
/* check for valid IP */
if (num >= 0 && num <= 255) {
/* parse remaining string */
ptr = strtok(NULL, DELIM);
if (ptr != NULL)
++dots;
} else{
free(tmpstr);
return 0;
}
}
/* valid IP string must contain 3 dots */
if (dots != 3){
free(tmpstr);
return 0;
}
free(tmpstr);
return 1;
}
// Driver program to test above functions
int main()
{
char ip1[] = "128.0.0.1";
char ip2[] = "125.16.100.1";
char ip3[] = "125.512.100.1";
char ip4[] = "125.512.100.abc";
is_valid_IPV4(ip1)? printf("Valid\n"): printf("Not valid\n");
is_valid_IPV4(ip2)? printf("Valid\n"): printf("Not valid\n");
is_valid_IPV4(ip3)? printf("Valid\n"): printf("Not valid\n");
is_valid_IPV4(ip4)? printf("Valid\n"): printf("Not valid\n");
printf("ip4 is %s\n", ip4);
return 0;
}
結果是:
Valid
Valid
Not Valid
Not Valid
方法Ⅱ
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isValidIpAddress(char *ipAddress)
{
struct sockaddr_in sa;
/* 函數 inet_pton會同時將 ipAddress複製到 函數的第3個參數上區,格式爲 struct sock_addr_in* */
int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
return result != 0;
}
void main()
{
char ip1[]="192.168.3.1";
char ip2[]="192.168.3.a";
char ip3[]="192.168.3";
isValidIpAddress(ip1) ? printf("Valid\n"): printf("Not valid\n");
isValidIpAddress(ip2) ? printf("Valid\n"): printf("Not valid\n");
isValidIpAddress(ip3) ? printf("Valid\n"): printf("Not valid\n");
return;
}
結果是:
Valid
Not Valid
Not Valid
IP address/地址 检查的更多相关文章
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- [Swift]LeetCode468. 验证IP地址 | Validate IP Address
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither ...
- lwip IP address handling 关于 IP 地址的 操作 API接口
lwip 2.0.3 IP address handling /** * @file * IP address API (common IPv4 and IPv6) */ 1.u32_t ipadd ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)
Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my ...
- [LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode 1108. Defanging an IP Address (IP 地址无效化)
题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...
- C#调用百度高精度IP定位API通过IP获取地址
API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...
- MySQL [Warning]: IP address 'xxxx' could not be resolved: Name or service not known
MySQL的error log 出现大量的 DNS反解析错误. DNS解析是指,将 域名解析成ip地址: DNS反解析是指,将IP地址反解析成域名: Version: MySQL Community ...
随机推荐
- MSSQLSERVER数据库- 触发器
参考了别人写的文章,我删除掉一些废话,只看一些我想看的信息.整理了一下,记录在这里,方便以后查阅! 1.当触发INSERT触发器时,新的数据行就会被插入到触发器表和inserted表中. 2.当触发d ...
- Spring Framework 5.0.0.M3中文文档 翻译记录 Part I. Spring框架概览1-2.2
Part I. Spring框架概览 The Spring Framework is a lightweight solution and a potential one-stop-shop for ...
- JQuery上传插件Uploadify API详解
一.相关key值介绍uploader:uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框,默认值:uploadify.swf. scrip ...
- 解决下载Android Build-tools 19.1.0失败
准备从Eclipse转到Android Studio了.今天尝试Android Studio的时候,被它提醒我SDK的Android Build-tools版本过低,需要升级. 于是打开Android ...
- Get Files from Directory
http://www.csharp-examples.net/get-files-from-directory/ Get Files from Directory [C#] This example ...
- [Practical Git] Format commit history with git log arguments
When running the git log command, we can pass in options as arguments toformat the data shown for ea ...
- mysqldump原理5
http://blog.csdn.net/niu870781892/article/details/6186078 导出多张表的时候表之间用空格分开: # mysqldump -h192.168.25 ...
- nginx rewrite 参数和例子
http://www.cnblogs.com/analyzer/articles/1377684.html ] 本位转自:http://blog.c1gstudio.com/archives/434 ...
- Android 自定义View修炼-自定义弹幕效果View
一.概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂 ...
- 不使用var定义变量和使用var的区别
最基本的var关键字是上下文的,而不采用var是全局的这就不讨论了 “不管是使用var关键字(在全局上下文)还是不使用var关键字(在任何地方),都可以声明一个变量”.这貌似一个错误的概念:任何时候, ...