Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:48:55+00:00 2026-05-27T02:48:55+00:00

Is there a clever/tricky way to analyze if a string represented IP address is

  • 0

Is there a clever/tricky way to analyze if a string represented IP address is valid and to recognize its version, so that to be able to convert it to the appropriate structure, just by using the UNIX API?

I don’t want to use regex, no need to add dependency to additional libraries just about this.


My first approach was:

in_addr addr;
memset( &addr, 0, sizeof( in_addr ) );
// try to convert from standard numbers-and-dots notation into binary data
if( 0 != inet_aton( sIPAddress.c_str(), &addr ) )
{
    return Socket::enIPv4;      // valid IPv4
}

in6_addr addr6;
memset( &addr6, 0, sizeof( in6_addr ) );
if( inet_pton( AF_INET6, sIPAddress.c_str(), &addr6 ) > 0 )
{
    return Socket::enIPv6;      // valid IPv6
}

return Socket::enUnknown;

The problem here is, that if I pass string like 1, it’s successfully converted to IPv4. String like 11111 is converted to IPv4, too. By documentation:

inet_aton() returns non-zero if the address is valid, zero if not.

Obviously, this function recognizes not only XXX.XXX.XXX.XXX format, but does something more internally.

Of course I can write my own function(and it’s going to be fun, actually), by analyzing the string, but I wanted to use already existing and tested functions, instead. Is it possible or I should write my own?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:48:56+00:00Added an answer on May 27, 2026 at 2:48 am

    As per the manual page, strings like "a", "a.b" and "a.b.c" are all valid addresses for inet_aton. If you only want the “normal” dotted-decimal, use inet_pton for those addresses too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any clever way to mix two bit sequences in such way that
Is there any clever way to tell google geocoding api to narrow its search
Is there a clever way of adding XML serialization instructions without modifying the serialized
Some time ago, I found a clever trick on stackoverflow that showed a way
Is there any clever way to make django forms render field with asterisks after
Is there a clever way to parse a tag found in two different areas?
http://en.wikipedia.org/wiki/Upsert Insert Update stored proc on SQL Server Is there some clever way to
Is there a clever way to vectorize a for-loop which assigns elements to submatrices
I was wondering if there is any way (standard or a clever trick) to
Is there a clever way to achieve the following in Ruby? hash1 = {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.