简体   繁体   中英

How to determine if a URL is an IP Address in C/C++?

I am using squid and need to do some customization. I have a list of IP Addresses for which some action needs to be taken. Cant use the acl directives of squid as final action needs to be taken by some other server.

I need to check if the URL being accessed is an valid IP Address or not. If it is, i need to make some changes in teh header.

I tried doing it via regular expressions, didnt quite seem to be working well.

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

Also it will fail in case of an IPv6 address.

Is there a C/C++ API using which this can be determined?

Pls suggest.

You probably want the inet_pton - which returns -1 on failure, and supports both the IPv4 and future IPv6 addresses. If you still need to write your own IP address system, remember that a standard 32-bit hex number is a valid IP address but not in dotted-decimal notation.

This function both verifies the address, and also allows you to use the same address in related socket calls.

(From Raymond Martineau's post here: How do you validate that a string is a valid IPv4 address in C++? )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM