inet_aton() returning success for invalid ip addresses?

I am looking for some function to verify that if given string is a valid ipv4 address, but inet_aton() seems to be happy with strings like "11" and "1.1" what is best way to validate an ipv4 string.

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[])
    struct in_addr addr;

   if (argc != 2) {
        fprintf(stderr, "%s <dotted-address>\n", argv[0]);

   if (inet_aton(argv[1], &addr) == 0) {
        fprintf(stderr, "Invalid address\n");

   printf("%s\n", inet_ntoa(addr));

the ouput for some invalid strings are

[root@ ~]# ./a.out 1.1
[root@ ~]# ./a.out "1 some junk"
[root@ ~]# ./a.out "10 some junk"

I want a routine to reject any string not in dotted decimal notation x.x.x.x, x from 0 to 255


This is the specified/documented behavior for inet_aton.

If you want to accept only dotted-quad decimal notation, use:

unsigned char *a = (void *)&addr, dummy;
if (sscanf(src, "%3hhd.%3hhd.%3hhd.%3hhd%c", a, a+1, a+2, a+3, &dummy)!=4) {
    /* error */

Alternatively, you might use the inet_pton function, which is more restrictive in the formats it accepts.

Need Your Help

access to gnome configuration information using python

python gnome

Is there a standard way of accessing Gnome configuration information (i.e. ~/.gconf) using Python?

Facebook application type

facebook mobile canvas web

We are planning to implement Facebook application to enable easier access to one of our service.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.