Use sscanf to determine IP address correctness

Source: Internet
Author: User

Because we can see the traversal string to judge one by oneCodeIt is not concise, althoughC LanguageIt is not as good as the scripting language, but there is another sscanf. So I wrote a small function.

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/>/* this function can verify valid IP addresses. The IP address can contain leading 0 or spaces */<br/> int isvalidstr (char * Buf, int * paddr) <br/>{< br/> int Part1, Part2, Part3, part4; <br/> char tail = 0; <br/> int field; <br/> If (BUF = NULL) <br/>{< br/> return 0; <br/>}< br/> Field = sscanf (BUF, "% d. % d. % d. % d % C ", & Part1, & Part2, & Part3, & part4, & tail); <br/> If (field <4 | field> 5) <br/>{< br/> printf ("Keep CT 4 field, get % d /N ", field); <br/> return 0; <br/>}< br/> If (tail! = 0) <br/>{< br/> printf ("ip address mixed with non number/N"); <br/> return 0; <br/>}< br/> If (Part1> = 0 & Part1 <= 255) & (Part2> = 0 & Part2 <= 255) & (Part3> = 0 & Part3 <= 255) & (part4> = 0 & part4 <= 255 )) <br/> {<br/> If (paddr) <br/> * paddr = part4 <24 | Part3 <16 | Part2 <8 | Part1; /* convert to network Order */<br/> return 1; <br/>}< br/> else <br/> {<br/> printf ("not good IP % d: % d/N ", part1, Part2, Part3, part4); <br/>}< br/> return 0; <br/>}< br/> int testip (char * Buf, int unique CT) <br/> {<br/> int result = 0; <br/> int ADDR = 0; <br/> result = isvalidstr (BUF, & ADDR ); <br/> If (result = wrong CT) <br/> {<br/> printf ("OK: valid IP % s, wrong CT % d, get % d/N ", Buf, CT, result); <br/> If (Ct = 1) <br/>{< br/> printf ("/twe convert % s to 0x % x/N", Buf, ADDR ); <br/>}< br/> else <br/> {<br/> printf ("error: valid IP % s, CT % d, get % d/N ", Buf, exact CT, result); <br/>}< br/> return 0; <br/>}< br/> int main (INT argc, char const * argv []) <br/> {<br/>/* empty IP */<br/> testip (null, 0 ); <br/>/* normal IP */<br/> testip ("10.129.43.244", 1 ); <br/>/* IP address with Spaces */<br/> testip ("10.129.43.244", 1); <br/> testip ("10. 129. 43.244 ", 1); <br/>/* IP address with leading 0 */<br/> testip (" 010.129.043.244 ", 1 ); <br/> testip ("010.0129.043.0244", 1); <br/>/* IP address with invalid characters in front */<br/> testip ("x10.129.43.244", 0 ); <br/>/* IP address with invalid characters */<br/> testip ("10.129.43.24y", 0); <br/> testip ("10.129.43.y", 0 ); <br/> return 0; <br/>}< br/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.