Code:
//A function to check if the accessing host have permission to access the page.
struct IP_STAMP{
char address[16];
long int time_stamp;
int count;
};
int allow_http_access(char *deny_hostfile, char *allow_hostfile,char *failed_hostfile, struct IP_STAMP *ip_stamp)
{
char *lenstr;
long len;
char input[1024];
char data[1024];
char string[1024];
char ipaddress[16];
char hostaddress[1024];
int i, j;
lenstr = getenv("REMOTE_ADDR"); /* OS GLOBAL Variable environment */
if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > 1024)
return 0;
/* fgets(input, len+1, stdin); Getting error ?? BUG: Solved but not understood*/
/* 19 January, 2003 */
for(i=0; i < 16 && *lenstr!='\0'; i++, lenstr++)
ipaddress[i]=*lenstr;
ipaddress[i]='\0';
lenstr = getenv("REMOTE_HOST"); /* Environment 4 REMOTE_HOST */
if(lenstr != NULL && sscanf(lenstr,"%ld",&len)==1 && len < 1024)
{
for(i=0; i < 1024 && *lenstr!='\0'; i++, lenstr++)
hostaddress[i]=*lenstr;
hostaddress[i]='\0';
}
printf("<center><font face=\"Verdana\" size=\"1\">IP Address Stamp: %s %s</font><br></center>", ipaddress, hostaddress);
//return find_string_infile(hostfile, ipaddress);
strcpy(ip_stamp->address, ipaddress);
if(!find_ip_infile(deny_hostfile, ipaddress))
return 1;
if(find_ip_infile(allow_hostfile, ipaddress))
return 2;
if(find_ip_failedlist(failed_hostfile, ipaddress, ip_stamp))
return 1;
return 0;
}
//Your CGI application
int main()
{
.............................
................................
if(allow_http_access(DENY_HOSTIP_FILE, ALLOWED_HOSTIP_FILE,
FAILED_HOSTIP_FILE, &ip_stamp) ) {
printf("<center> <font color=\"#FF0000\"> Opps.. Sorry!!!! You don't have permission to access this page.</font><br>");
return 1;
}
...................................
....................................
....................................
}
I guess the above code will help you to understant more. Sorry.. did not put enough comments... If anything not clear, put your questions.