<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to determine if a string contains a validate IP address in C</title>
	<atom:link href="http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/</link>
	<description>The pulp of technology</description>
	<lastBuildDate>Mon, 30 Jan 2012 07:42:48 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Frank</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-193</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Wed, 18 May 2011 21:51:50 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-193</guid>
		<description>I think Loteanu has a valid point about long inputs, but wouldn&#039;t it be easier to just make a small modification to the sscanf format? Putting a number in the %u causes sscanf to look at a maximum of that many characters between the dots.

	if(sscanf(ip_str,&quot;%3u.%3u.%3u.%3u&quot;, &amp;n1, &amp;n2, &amp;n3, &amp;n4) != 4) return 0;

I tried it in VS 2008 (had to replace sscanf with sscanf_s and sprintf with sprintf_s) and it seemed to work just find. To make the code just a bit more bulletproof, it might be good to use snprintf and strncmp instead of the standard functions.</description>
		<content:encoded><![CDATA[<p>I think Loteanu has a valid point about long inputs, but wouldn&#8217;t it be easier to just make a small modification to the sscanf format? Putting a number in the %u causes sscanf to look at a maximum of that many characters between the dots.</p>
<p>	if(sscanf(ip_str,&#8221;%3u.%3u.%3u.%3u&#8221;, &amp;n1, &amp;n2, &amp;n3, &amp;n4) != 4) return 0;</p>
<p>I tried it in VS 2008 (had to replace sscanf with sscanf_s and sprintf with sprintf_s) and it seemed to work just find. To make the code just a bit more bulletproof, it might be good to use snprintf and strncmp instead of the standard functions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loteanu Adrian</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-187</link>
		<dc:creator>Loteanu Adrian</dc:creator>
		<pubDate>Fri, 08 Apr 2011 07:37:27 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-187</guid>
		<description>Sorry, the above version contains 2 bugs and I don&#039;t know how to delete the post. I will post the updated version. I intentionally avoided using functions such as atoi in order to have full control of the complexity (to avoid parsing large strings). Here is my final version:
&lt;code&gt;
int isValidIp(const char* str)
{

int i = 0; //string iterator
char buf[3] = {0};
int partcnt = 0;
int numdots = 0;

//start reading the input
for (;;i++)
	{
	//read a character from str =&gt; str[i]
	
	//test that it is valid
	if ((str[i]  &#039;9&#039;) &amp;&amp; str[i] != &#039;.&#039; &amp;&amp; str[i] != &#039;&#039;)
		{
		return FALSE; 
		}
	
	//str[i] is a valid character
	
	//if it is a number 0-9
	if( str[i] &gt;= &#039;0&#039; &amp;&amp; str[i] = 3)
			{
			return FALSE;
			}

		//add it to the buffer
		buf[partcnt] = str[i];
		partcnt++;
		
		continue;	
		}

	//if we are at a dot or the end
	if (str[i] == &#039;.&#039;)
		{
		//check that we have a valid number of chars in buff.
		//they are sureley valid numbers because they were verified in the condition above
		if (partcnt == 0)
			{
			//no chars before dot, sorry
			return FALSE;
			}
		
		//check validity of the buffer contents
		int k;
		int rank = 1;
		int val = 0;
		
		if (buf[0] == &#039;0&#039; &amp;&amp; partcnt != 1)
			{
			return FALSE;
			}
			
		//first parse the number
		for (k = partcnt-1; k&gt;=0; k--)
			{
			val += ((buf[k] - &#039;0&#039;) * rank);
			rank *= 10;
			}
		//then check the val	
		if (val  255)
			{
			return FALSE;
			}
		
		//value is good then continue but do not forget to reset the buffer
		partcnt = 0;
		buf[0]=buf[1]=buf[2] = &#039;&#039;;
		
		numdots++;
		if (numdots &gt; 3) 
			{
			return FALSE;
			}
			
		continue;
		}
	
	//final case
	if (str[i] == &#039;&#039;)
		{
		if (numdots != 3)
			{
			return FALSE;
			}
		if (partcnt == 0)
			{
			return FALSE;
			}
		
		//check validity of the buffer contents
		int k;
		int rank = 1;
		int val = 0;
		
		if (buf[0] == &#039;0&#039; &amp;&amp; partcnt != 1)
			{
			return FALSE;
			}
			
		//first parse the number
		for (k = partcnt-1; k&gt;=0; k--)
			{
			val += ((buf[k] - &#039;0&#039;) * rank);
			rank *= 10;
			}
		//then check the val	
		if (val  255)
			{
			return FALSE;
			}
			
			
		return TRUE;
		}
		
		
	}//endfor


}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Sorry, the above version contains 2 bugs and I don&#8217;t know how to delete the post. I will post the updated version. I intentionally avoided using functions such as atoi in order to have full control of the complexity (to avoid parsing large strings). Here is my final version:<br />
<code><br />
int isValidIp(const char* str)<br />
{</p>
<p>int i = 0; //string iterator<br />
char buf[3] = {0};<br />
int partcnt = 0;<br />
int numdots = 0;</p>
<p>//start reading the input<br />
for (;;i++)<br />
	{<br />
	//read a character from str =&gt; str[i]</p>
<p>	//test that it is valid<br />
	if ((str[i]  '9') &amp;&amp; str[i] != '.' &amp;&amp; str[i] != '')<br />
		{<br />
		return FALSE;<br />
		}</p>
<p>	//str[i] is a valid character</p>
<p>	//if it is a number 0-9<br />
	if( str[i] &gt;= '0' &amp;&amp; str[i] = 3)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		//add it to the buffer<br />
		buf[partcnt] = str[i];<br />
		partcnt++;</p>
<p>		continue;<br />
		}</p>
<p>	//if we are at a dot or the end<br />
	if (str[i] == '.')<br />
		{<br />
		//check that we have a valid number of chars in buff.<br />
		//they are sureley valid numbers because they were verified in the condition above<br />
		if (partcnt == 0)<br />
			{<br />
			//no chars before dot, sorry<br />
			return FALSE;<br />
			}</p>
<p>		//check validity of the buffer contents<br />
		int k;<br />
		int rank = 1;<br />
		int val = 0;</p>
<p>		if (buf[0] == '0' &amp;&amp; partcnt != 1)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		//first parse the number<br />
		for (k = partcnt-1; k&gt;=0; k--)<br />
			{<br />
			val += ((buf[k] - '0') * rank);<br />
			rank *= 10;<br />
			}<br />
		//then check the val<br />
		if (val  255)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		//value is good then continue but do not forget to reset the buffer<br />
		partcnt = 0;<br />
		buf[0]=buf[1]=buf[2] = '';</p>
<p>		numdots++;<br />
		if (numdots &gt; 3)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		continue;<br />
		}</p>
<p>	//final case<br />
	if (str[i] == '')<br />
		{<br />
		if (numdots != 3)<br />
			{<br />
			return FALSE;<br />
			}<br />
		if (partcnt == 0)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		//check validity of the buffer contents<br />
		int k;<br />
		int rank = 1;<br />
		int val = 0;</p>
<p>		if (buf[0] == '0' &amp;&amp; partcnt != 1)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		//first parse the number<br />
		for (k = partcnt-1; k&gt;=0; k--)<br />
			{<br />
			val += ((buf[k] - '0') * rank);<br />
			rank *= 10;<br />
			}<br />
		//then check the val<br />
		if (val  255)<br />
			{<br />
			return FALSE;<br />
			}</p>
<p>		return TRUE;<br />
		}</p>
<p>	}//endfor</p>
<p>}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loteanu Adrian</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-185</link>
		<dc:creator>Loteanu Adrian</dc:creator>
		<pubDate>Fri, 08 Apr 2011 07:11:35 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-185</guid>
		<description>Hey. I think there is a problem with the function.

if you give it a large string of the form 1.2.3.498471294712987..... (very long string) it will take a very long time to (in)validate it since scanf will read until it reaches a termination character. Since the last number is so large it the result of scanf might be unpredictable and it may even validate such strings. I recently implemented such a function and used a finite automaton to catch all the error cases. The alternative is using regexp&#039;s but I am not familiar with C support for them. It might be possible to use such an expression is scanf.</description>
		<content:encoded><![CDATA[<p>Hey. I think there is a problem with the function.</p>
<p>if you give it a large string of the form 1.2.3.498471294712987&#8230;.. (very long string) it will take a very long time to (in)validate it since scanf will read until it reaches a termination character. Since the last number is so large it the result of scanf might be unpredictable and it may even validate such strings. I recently implemented such a function and used a finite automaton to catch all the error cases. The alternative is using regexp&#8217;s but I am not familiar with C support for them. It might be possible to use such an expression is scanf.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sara Mithal</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-141</link>
		<dc:creator>Sara Mithal</dc:creator>
		<pubDate>Thu, 17 Feb 2011 18:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-141</guid>
		<description>Well..in that case you can add reconstruction of ip string and comparison with the original ip string.</description>
		<content:encoded><![CDATA[<p>Well..in that case you can add reconstruction of ip string and comparison with the original ip string.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neo</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-140</link>
		<dc:creator>Neo</dc:creator>
		<pubDate>Thu, 17 Feb 2011 18:30:57 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-140</guid>
		<description>If you see man page of inet_aton, it says that a string of pattern &quot;a.b.c.d&quot;, &quot;a.b.c&quot;, &quot;a.b&quot; and &quot;a&quot; are considered valid. In that case your function is going to return VALID for an ip &quot;1.2.3&quot;, &quot;1.2&quot; and 1.</description>
		<content:encoded><![CDATA[<p>If you see man page of inet_aton, it says that a string of pattern &#8220;a.b.c.d&#8221;, &#8220;a.b.c&#8221;, &#8220;a.b&#8221; and &#8220;a&#8221; are considered valid. In that case your function is going to return VALID for an ip &#8220;1.2.3&#8243;, &#8220;1.2&#8243; and 1.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sara Mithal</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-139</link>
		<dc:creator>Sara Mithal</dc:creator>
		<pubDate>Thu, 17 Feb 2011 18:27:46 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-139</guid>
		<description>How about my version of the same function. It uses standard inet_aton() to validate partially.
&lt;code&gt;
int is_valid_ip(const char *ip_str)
{
        int rv;
        unsigned long ip;
        unsigned char *p;
        rv = inet_aton(ip_str,(struct in_addr*)&amp;ip);
        if(rv) {
                p = (unsigned char*) &amp;ip;
                if(p[0] != 0) return 1;
        }
        return 0;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>How about my version of the same function. It uses standard inet_aton() to validate partially.<br />
<code><br />
int is_valid_ip(const char *ip_str)<br />
{<br />
        int rv;<br />
        unsigned long ip;<br />
        unsigned char *p;<br />
        rv = inet_aton(ip_str,(struct in_addr*)&amp;ip);<br />
        if(rv) {<br />
                p = (unsigned char*) &amp;ip;<br />
                if(p[0] != 0) return 1;<br />
        }<br />
        return 0;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ook</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-138</link>
		<dc:creator>Ook</dc:creator>
		<pubDate>Tue, 08 Feb 2011 20:39:21 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-138</guid>
		<description>Nice, thanks. I fiddled with it and came up with this solution before I read yours:
...
unsigned int n5;
if(sscanf(ip_str,&quot;%u.%u.%u.%u.%u&quot;, &amp;n1, &amp;n2, &amp;n3, &amp;n4, &amp;n5) != 4) return 0;
...

Basically adding one more unsigned int, and if the sscanf returns 5, it bombs out.

Thanks for this! I would have come up with a WAY more complicated method. You saved me a few hours!</description>
		<content:encoded><![CDATA[<p>Nice, thanks. I fiddled with it and came up with this solution before I read yours:<br />
&#8230;<br />
unsigned int n5;<br />
if(sscanf(ip_str,&#8221;%u.%u.%u.%u.%u&#8221;, &amp;n1, &amp;n2, &amp;n3, &amp;n4, &amp;n5) != 4) return 0;<br />
&#8230;</p>
<p>Basically adding one more unsigned int, and if the sscanf returns 5, it bombs out.</p>
<p>Thanks for this! I would have come up with a WAY more complicated method. You saved me a few hours!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neo</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-137</link>
		<dc:creator>Neo</dc:creator>
		<pubDate>Tue, 08 Feb 2011 16:31:37 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-137</guid>
		<description>I have added more code to validate the ip string further. Basic idea is to compare the input ip string with reconstructed ip string from the parsed values. This additional check should identify any unwanted characters.</description>
		<content:encoded><![CDATA[<p>I have added more code to validate the ip string further. Basic idea is to compare the input ip string with reconstructed ip string from the parsed values. This additional check should identify any unwanted characters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ook</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-136</link>
		<dc:creator>Ook</dc:creator>
		<pubDate>Mon, 07 Feb 2011 23:17:50 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-136</guid>
		<description>It looks like it will validate addresses with more than 4 octets. The address 1.2.3.4.5 will return valid.</description>
		<content:encoded><![CDATA[<p>It looks like it will validate addresses with more than 4 octets. The address 1.2.3.4.5 will return valid.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neo</title>
		<link>http://www.techpulp.com/blog/2008/10/how-to-validate-ip-address-in-c/#comment-10</link>
		<dc:creator>Neo</dc:creator>
		<pubDate>Mon, 16 Aug 2010 18:18:13 +0000</pubDate>
		<guid isPermaLink="false">http://techpulp.com/?p=126#comment-10</guid>
		<description>You are right! code is updated for the same.</description>
		<content:encoded><![CDATA[<p>You are right! code is updated for the same.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

