SNMP Manager Programmer's Guide

Programming Examples
SNMP Manager Programmer’s Guide–134249
2-102
SNMPTRAP
if (isdigit(*agentstr)) <--8
{
/*
* error checking of IP address value
* and error checking on inet_addr()
*/
name = agentstr;
dots = 0;
while ((ptr = strchr(name, '.')) != NULL)
{
dots++; /* keep track of no. of dots */
memset(id, NULL, 20);
strncpy(id, name, ptr - name);
byte = atoi(id);
if ( byte < 0 || byte > 255)
{
fprintf(stderr,"Invalid manager host identifier %s\n", agentstr);
exit(0);
}
name = ptr + 1;
} /* of while */
/* if IP address contains invalid no. of dots exit */
if ( dots < 3)
{
fprintf(stderr, "Invalid manager host identifier %s\n", agentstr);
exit(0);
}
if ((host_addr = inet_addr(agentstr)) == INVALID_HOST_ADDR)
{
fprintf(stderr, "Invalid manager host identifier %s\n", agentstr);
exit(0);
}
} else
{
/* host_addr = gethostid(); */
if ((host = gethostbyname(agentstr)) == (struct hostent *)0)
{
fprintf(stderr, "Cannot locate host : %s\n", agentstr);
exit(0);
}
host_addr = *(u_long *)(host->h_addr);
}
Example 2-13. Contents of snmptrpc (page 6 of 10)