Syntax
#include <snmp_dpi.h>
long int query_DPI_port( /* Query (GET) SNMP_DPI port */
char *hostname_p, /* target hostname/IPaddress */
char *community_p, /* communityname for GET */
int porttype); /* port type, one of: */
/* dpiPortForTCP */
/* dpiPortForUDP */
Parameters
hostname_p
#define dpiPortForTCP 1 #define dpiPortForUDP 2
At this time, the dpiPORTForUDP port type is not supported. If you use it, the return value is set to -1, which indicates a failure.
Return Values
If successful, the DPI port number for the specified protocol, TCP or UDP, is returned.
If failure, a value of -1 is returned.
Description
The query_DPI_port function is used to obtain the port number on which the DPI capable SNMP agent at the specified host is listening for connections (TCP) or packets (UDP).
The DPI subagent programmer only needs to use this function to code the connection setup and send or await the packet. The programmer then obtains the DPI port number, finds the IP address of the agent with the lookup_host() function and then sets up a socket for communication.
This function is implicitly executed by the DPIconnect_to_agent_TCP() function, which is the function that the DPI subagent programmer would normally use. So the query_DPI_port() function is normally not used by the DPI subagent programmer.
Examples
#include <snmp_dpi.h>#include /* other include files for BSD sockets and such */
int handle;
unsigned char *pack_p;
long int dpi_port;
int fd;
struct sockaddr_in s,t; /* source and target */
dpi_port = query_DPI_port("localhost", /* get DPI port number */
"public", /* for TCP, local host */
dpiPortForTCP);
if (dpi_port < 0) exit(1); /* error if negative */
host_addr = lookup_host("localhost"); /* find target IP addr */
if (host_addr == 0) exit(1); /* unknown, that's it */
fd = socket(AF_INET,SOCK_STREAM,0); /* create a TCP socket */
if (fd < 0) exit(1); /* failure to do so */
memset(&s,0,sizeof(s));
s.sin_family = AF_INET; /* set AF_INET family */
s.sin_port = 0; /* give us any port, */
s.sin_addr.s_addr = htonl(INADDR_ANY); /* any local IPaddress */
rc = bind(fd,(struct sockaddr *)s, /* bind our socket(fd) */
sizeof(s_sock)); /* defined in s socket */
if (rc < 0) exit(1); /* failure, so exit */
memset(&d,0,sizeof(d));
d.sin_family = AF_INET; /* set AF_INET family */
d.sin_port = htons(dpi_port); /* set requested port */
d.sin_addr.s_addr = host_addr; /* destination IP addr */
/* network byte order */
rc = connect(fd,(struct sockaddr *)d, /* connect to target */
sizeof(d)); /* based on d sock */
if (rc < 0) exit(1); /* failed, exit */
/* now we have a socket on which to send/receive DPI packets */
Related Information