A C function to build the in-addr.arpa name corresponding to a given ipv4 address

int dns_ip4_to_inaddr_arpa(char *ip, char *buf, size_t buflen) {
	
	char inaddr_arpa[32];
	int o1 = 0, o2 = 0, o3 = 0, o4 = 0;
	
	if (strlen(ip) > 15) {
		return -1;
	}
	
	sscanf(ip, "%d.%d.%d.%d", &o1, &o2, &o3, &o4);
	sprintf(inaddr_arpa, "%d.%d.%d.%d.in-addr.arpa", o4, o3, o2, o1);
	
	if (strlen(inaddr_arpa) > buflen) {
		return -1;    
	}
	
	strncpy(buf, inaddr_arpa, buflen);
	return strlen(inaddr_arpa);
}
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki