The Connect directive calls the connect function you specify.
Connect fn=your-connect-function
Only the first applicable Connect function is called, starting from the most restrictive object. Occasionally you might want to call multiple functions until a connection is established. The function returns REQ_NOACTION if the next function should be called. If the function fails to connect, the return value is REQ_ABORT. If the function connects successfully, the connected socket descriptor will be returned.
The Connect function must have this prototype:
int your_connect_function(pblock *pb, Session *sn, Request *rq);
Connect receives its destination host name and port number from:
pblock_findval (“connect-host”, rq->vars) atoi (pblock_findval (“connect-port”, rq->vars))
The host can be in a numeric IP address format.
To use the NSAPI custom DNS class functions to resolve the host name, make a call to this function:
struct hostent *servact_gethostbyname(char *host name, Session *sn, Request *rq);
This example uses the native connect mechanism to establish the connection:
#include "base/session.h" #include "frame/req.h" #include <ctype.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> int my_connect_func(pblock *pb, Session *sn, Request *rq) { struct sockaddr_in sa; int sd; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(atoi (pblock_findval (“connect-port”, rq->vars))); /* host name resolution */ if (isdigit(*pblock_findval (“connect-host”, rq->vars))) sa.sin_addr.s_addr = inet_addr(rq->host); else { struct hostent *hp = servact_gethostbyname(pblock_findval (“connect-host”, rq->vars), sn, rq)); if (!hp) return REQ_ABORTED; /* can’t resolv */ memcpy(&sa.sin_addr, hp->h_addr, hp->h_lenght); } /* create the socket and connect */ sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) return REQ_ABORTED; /* can’t create socket */ if (connect(sd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { close(sd); return REQ_ABORTED; /* can’t connect */ } return sd; /* ok */ } |