[prog] socket programming with C language

Abel Pires da Silva lybel_2000 at yahoo.com
Tue Oct 1 02:00:52 EST 2002


Dear all, 
I'm writing a simple program just like proxy (complete
program attached),
that try to reject connection if there is any match
between the host name 
requested by browser and the list of addresses stored
in file tabel.txt.

I read() a socket descriptor, wich store the reading
result in buffer,
and I now that its content is like below:

GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.75 [en] (X11; U; Linux
2.2.17-21mdk i686)
Host: localhost:8000
Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, image/png, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8

I'm trying to do is to make string comparation between
the *HOST (at line four)
with the addresses stored in file tabel.txt

Any suggestion to do the comparation?
I'm running this on Linux machine 2.2.17-21 mdk i686.

Please help...

Regards,
Abel



__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com-------------- next part --------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>

#define MAXBUF 65535 
#define MAXLINE 65535
#define SERV_ADDR "127.0.0.1"
#define SERV_PORT 80 
#define BACKLOG 20

main (int argc, char *argv [])
{

char buffer [MAXBUF];
char line [MAXLINE];
char cliline [80];
int port, rd, socket_fd, socket_fd2, socket_fd3;
socklen_t serv_len;
struct sockaddr_in server, client;
FILE *PF;

if (argc != 2)
 	{
	printf ("usage: %s <port>\n", argv[0]);
	exit(1);
	}
port = atoi (argv[1]);

if ((socket_fd=socket (PF_INET, SOCK_STREAM, 0))<0)
	{
	printf ("%s: cannot create socket!\n", argv[0]);
	exit(1);
	}
bzero ((char *)&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (port);

if (bind (socket_fd, (struct sockaddr *)&server, sizeof (server))<0)
	{
	printf ("%s: cannot bind socket!\n", argv [0], port);
	exit (1);
	}

listen (socket_fd, BACKLOG);

signal (SIGCHLD, SIG_IGN);

serv_len =  sizeof (server);

if (fork()) exit (0);

for (;;)
{

if ((socket_fd2 = accept (socket_fd, (struct sockaddr *)&server, &serv_len))<0)
	{
	printf ("%s: cannot accept!\n", argv[0]);
	break;
	}

if (fork() !=0)
{
close (socket_fd2);
continue;
}

else {

if (fork() == 0)
{

while (1)
{
nextline:
memset (buffer, 0, MAXBUF);
	if ((rd = read (socket_fd2, buffer, MAXBUF))<0)
		{
		printf ("%s: cannot read socket_fd2!\n", argv[0]);
		break;
		}

	/* at this stage, I need to retrieve the fourth line of the message sent by
	 * browser..and store the line in cliline */
	
if ((PF=fopen("tabel.txt", "r"))!= 0)  /* open the file contained with restricted addresses */
	{
	while ((fgets(line,20, PF))!= NULL)
	/* gets list of restricted addresses from file table.txt */
		{
	if((strstr(line, cliline))!=0) /* deny if any match*/
			{
			/*deny connection */
			write (socket_fd2, "<BODY><PRE>\n", 12);
			write (socket_fd2, "ERROR 401: ACCESS DENIED!\n", 26);
  			write (socket_fd2," </PRE></BODY></HTML>\n", 22);
			fclose (PF);
			goto nextline;
			}
		}
	fclose (PF);
	}

/* continue if there is no macth */

bzero ((char *)&client, sizeof (client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr (SERV_ADDR); /* connect to localhost only.. */
client.sin_port = htons (SERV_PORT);

if ((socket_fd3 = socket (AF_INET, SOCK_STREAM, 0)) < 0)
	{
	printf ("%s: cannot create socket to connect!\n", argv[0]);
	continue;
	}
if (connect (socket_fd3, (struct sockaddr *)&client, sizeof (client))<0)
	{
	printf ("%s: cannot connect to server!\n", argv[0]);
	break;
	}

write (socket_fd3, buffer, rd );  /* continue to server_socket if nothing match*/

memset (buffer, 0, MAXBUF);
if ((rd = read (socket_fd3, buffer, MAXBUF))<0)
	{
	printf ("%s: cannot read from socket_fd3!\n", argv [0]);
	break;
	}

write (socket_fd2, buffer, rd);

} /* while loop */
close (socket_fd3);
close (socket_fd2);
} /* child */
} /* else */
} /* loop forever */
close (socket_fd);
} /* main loop */


More information about the Programming mailing list