PROGRAM 3
/* filename: browse.c author: anthony f. ortiz */
/* this is a web browser program. */
/* headers */
#include
#include
#include
#include
#include
#include
/* constants */
#define GET_CMD "GET %s HTTP/1.0 \r\n\r\n"
#define OFF_HTML 0
#define ON_HTML 1
#define OFF_HTML_LINK 0
#define ON_HTML_LINK 1
#define MAX_PROTOCOL 20
#define MAX_MACHINENAME 30
#define MAX_FILELOCATION 30
#define MAX_LINECOUNTER 20
#define MAX_LINEBUFFER 80
#define MAX_FILESIZE 100000
#define PORT 80
/* variables */
struct url
{
char protocol [MAX_PROTOCOL];
char machinename [MAX_MACHINENAME];
char filelocation [MAX_FILELOCATION];
} aurl;
struct link
{
char line [80];
} links [100];
/* print urls (this is just for debugging, not used). */
void print_them (int link_count)
{
int i;
for (i = 1; i <= link_count; i++)
{
printf ("\n%s\n", links [i].line);
}
}
/* enter url or link number. */
void enter_url (int link_count)
{
char str [MAX_LINEBUFFER];
printf ("\nenter url or number (after a webpage):\n\n");
gets (str);
if (atoi (str) <= link_count && atoi (str) >= 1)
{
memcpy (str, links [atoi (str)].line, sizeof (links [atoi (str)].line));
}
memcpy (aurl.protocol, strtok (str, "://"), MAX_PROTOCOL);
memcpy (aurl.machinename, strtok (NULL, "/"), MAX_MACHINENAME);
memcpy (aurl.filelocation, "/", 1);
memcpy (aurl.filelocation + 1, strtok (NULL, "\0"), MAX_FILELOCATION);
}
/* get webpage. */
int get_webpage (int sock, char htmlfile [])
{
char line [MAX_LINEBUFFER];
int n;
int total_bytes = 0;
for (;;)
{
n = read (sock, line, sizeof (line));
if (n == 0)
{
break;
}
else if (n < 0)
{
perror ("reading on stream socket.");
}
memcpy (htmlfile + total_bytes, line, n);
total_bytes = total_bytes + n;
}
return total_bytes;
}
/* parse webpage (text and links). */
int parse_webpage (char htmlfile [], int total_bytes, int link_count)
{
int i = 0;
int flag = OFF_HTML;
int flag2 = OFF_HTML_LINK;
int char_count = 0;
int strsize = 0;
int line_counter = 0;
char anykey [] = " ";
link_count = 0;
do
{
/* parse the text. */
if (htmlfile [i] == '<')
{
flag = OFF_HTML;
}
else if (htmlfile [i] == '>')
{
flag = ON_HTML;
}
if (flag == ON_HTML && htmlfile [i] != '<' && htmlfile [i] != '>')
{
printf ("%c", htmlfile [i]);
}
/* parse the links. */
if (flag2 == ON_HTML_LINK && (htmlfile [i] == ' ' || htmlfile [i] == '"'))
{
links [link_count].line [char_count] = '\0';
flag2 = OFF_HTML_LINK;
}
else if (flag2 == OFF_HTML_LINK && htmlfile [i] == 'h' && htmlfile [i +1] == 't' && htmlfile [i + 2] == 't' && htmlfile [i + 3] == 'p')
{
char_count = 0;
flag2 = ON_HTML_LINK;
link_count = link_count + 1;
printf (" [%i] ", link_count);
}
if (flag2 == ON_HTML_LINK)
{
links [link_count].line [char_count] = htmlfile [i];
char_count++;
}
/* count lines and stop output after 20 lines. */
if (htmlfile [i] == '\n' && flag == ON_HTML)
{
line_counter = line_counter + 1;
}
if (line_counter == 20)
{
printf ("press enter key to continue. ");
gets (anykey);
line_counter = 0;
}
i = i + 1;
}
while (i < total_bytes);
printf ("press enter key to continue. ");
gets (anykey);
return link_count;
}
/* main program */
void main ()
{
char htmlfile [MAX_FILESIZE];
char line [MAX_LINEBUFFER];
int sock, n, total_bytes;
int link_count = 0;
struct sockaddr_in server;
struct hostent * hp, * gethostbyname ();
/* main loop */
for (;;)
{
/* create socket. */
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("opening stream socket.");
exit (1);
}
/* enter url or link number. */
enter_url (link_count);
/* connect socket using name specified at command line. */
server.sin_family = AF_INET;
hp = gethostbyname (aurl.machinename);
if (hp == 0)
{
printf ("%s: unknown host\n", aurl.machinename);
exit (2);
}
bcopy (hp->h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons (PORT);
if (connect (sock, (struct sockaddr *) & server, sizeof (server)) < 0)
{
perror ("connecting stream socket.");
exit (1);
}
/* put get command together. */
n = snprintf (line, sizeof (line), GET_CMD, aurl.filelocation);
/* execute get command. */
if (write (sock, line, sizeof (line)) < 0)
{
perror ("writing on stream socket.");
}
/* get webpage. */
total_bytes = get_webpage (sock, htmlfile);
/* parse webpage (text and links). */
link_count = parse_webpage (htmlfile, total_bytes, link_count);
/* display urls (only to debug). */
/* print_them (link_count); */
/* close the socket. */
close (sock);
}
}
/* filename: wserver.c author: anthony f. ortiz */
/* this is a web server program. */
/* headers */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* constants */
#define DOCUMENTFOUND_HEADER "HTTP/1.0 200 document follows\n"
#define SERVER_HEADER "server: ncsa/1.42\n"
#define CONTENTTYPE_HEADER "content-type: text/html\n"
#define LASTMODIFIED_HEADER "last-modified: %s\n"
#define CONTENTLENGTH_HEADER "content-length: %ld\r\n\r\n"
#define DOCUMENTNOTFOUND_HEADER "HTTP/1.0 404 document not found\r\n\r\ndocument wasn't found."
#define PUBLIC "public_html/"
#define TRUE 1
#define MAX_QUEUE 5
#define MAX_LINEBUFFER 80
#define MAX_REQUEST 1024
#define MAX_HEADER 1024
#define MAX_REQUESTTYPE 20
#define MAX_FILELOCATION 40
#define MAX_PROTOCOL 20
#define MAX_DATE 30
#define PORT 2000
#define FILEPOSITION 11
/* variables */
struct request
{
char requesttype [MAX_REQUESTTYPE];
char filelocation [MAX_FILELOCATION];
char protocol [MAX_PROTOCOL];
} arequest;
/* parse the request. */
void parse_getrequest (char requestline [], int sock2)
{
strtok (requestline, "\r\n\r\n");
memcpy (arequest.requesttype, strtok (requestline, " "), MAX_REQUESTTYPE);
memcpy (arequest.filelocation, strtok (NULL, " "), MAX_FILELOCATION);
memcpy (arequest.protocol, strtok (NULL, "\0"), MAX_PROTOCOL);
}
/* construct the filename. copy 'public_html/' to the filename */
/* and add the rest of the contents of filelocation to filename, */
/* not including the shortcut to my directory. */
void get_filename (char filename [])
{
strcpy (filename, PUBLIC);
strcat (filename, arequest.filelocation + FILEPOSITION);
}
/* send document found header to the client. */
void send_documentheader (int sock2, char filename [])
{
char header [MAX_HEADER] = "";
char * datemodified;
struct stat filestats;
if (write (sock2, DOCUMENTFOUND_HEADER, sizeof (DOCUMENTFOUND_HEADER)) < 0)
{
perror ("writing on stream socket.");
}
if (write (sock2, SERVER_HEADER, sizeof (SERVER_HEADER)) < 0)
{
perror ("writing on stream socket.");
}
if (write (sock2, CONTENTTYPE_HEADER, sizeof (CONTENTTYPE_HEADER)) < 0)
{
perror ("writing on stream socket.");
}
if (lstat (filename, & filestats) < 0)
{
perror ("lstat");
}
datemodified = ctime (&filestats.st_mtime);
datemodified [24] = '\0';
sprintf (header, LASTMODIFIED_HEADER, datemodified);
if (write (sock2, header, sizeof (header)) < 0)
{
perror ("writing on stream socket.");
}
sprintf (header, CONTENTLENGTH_HEADER, filestats.st_size);
if (write (sock2, header, sizeof (header)) < 0)
{
perror ("writing on stream socket.");
}
}
/* send document not found header to the client. */
void send_nodocumentheader (sock2)
{
if (write (sock2, DOCUMENTNOTFOUND_HEADER, sizeof (DOCUMENTNOTFOUND_HEADER)) < 0)
{
perror ("writing on stream socket.");
}
}
/* read the file and send the contents to the client. */
int send_filecontents (int fd, int sock2)
{
char line [MAX_LINEBUFFER] = "";
int rval;
if ((rval = read (fd, line, sizeof (line))) < 0)
{
perror ("reading file.");
}
if (write (sock2, line, rval) < 0)
{
perror ("writing on stream socket.");
}
return rval;
}
/* main program */
void main()
{
/* variables */
int sock, sock2, rval, slen, fd;
int maxqueue = MAX_QUEUE;
char line [MAX_LINEBUFFER] = "";
char requestline [MAX_REQUEST] = "";
char filename [MAX_FILELOCATION] = "public_html/";
struct sockaddr_in server;
int countbytes = 0;
/* create socket. */
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("opening stream socket.");
exit (1);
}
/* name socket using wildcards. */
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0; /*PORT;*/
if (bind (sock, (struct sockaddr *) & server, sizeof (server)))
{
perror ("binding socket name.");
exit (1);
}
/* find out assigned port number and print out. */
slen = sizeof(server);
if (getsockname (sock, (struct sockaddr *) & server, &slen))
{
perror ("getting socket name.");
exit (1);
}
printf ("socket has port #%d\n", ntohs (server.sin_port));
/* send header and webpage to the client. */
listen (sock, maxqueue);
do
{
if((sock2 = accept (sock, 0, 0)) < 0)
{
perror ("accept.");
}
else
{
/* read the get or other type of request. */
do
{
bzero (line, sizeof (line));
if ((rval = read (sock2, line, sizeof (line))) < 0)
{
perror ("reading message stream.");
}
/* display it on the screen. */
printf ("%s", line);
/* copy buffer to request line. */
memcpy (requestline + countbytes, line, rval);
/* keep track of number of bytes in request line. */
countbytes = countbytes + rval;
/* end after length of buffer read is less than the. */
/* maximum line buffer. */
if (rval < MAX_LINEBUFFER)
{
break;
}
}
while (TRUE);
/* initialize countbytes to 0. */
countbytes = 0;
/* parse request. */
parse_getrequest (requestline, sock2);
/* if the request is not a get, close connection and break */
/* from the loop. */
if (strcmp (arequest.requesttype, "GET") != 0 || strcmp (arequest.protocol, "HTTP/1.0") != 0)
{
printf ("that isn't a get request.");
close (sock2);
}
else
{
/* construct filename. */
get_filename (filename);
/* open the file. */
if ((fd = open (filename, O_RDONLY)) < 0)
{
perror ("openning file");
}
/* the file is either in the current directory or it isn't. */
/* if it is, construct document header and send to client. */
/* otherwise, construct nodocument header and send to the */
/* client. */
if (fd >= 0 && strcmp (arequest.requesttype, "GET") == 0 && strcmp (arequest.protocol, "HTTP/1.0") == 0)
{
send_documentheader (sock2, filename);
}
else if (fd == -1 && strcmp (arequest.requesttype, "GET") == 0 && strcmp (arequest.protocol, "HTTP/1.0") == 0)
{
send_nodocumentheader (sock2);
}
/* if the file doesn't exist, break from the loop. otherwise, */
/* read the file and send it to the client. */
do
{
if (fd < 0)
{
break;
}
rval = send_filecontents (fd, sock2);
}
while (rval > 0);
/* close the file. */
close (fd);
/* close the connection to the client. */
close (sock2);
}
}
}
while (TRUE);
/* close the socket. */
close (sock);
}
/* filename: prog3.out */
/* get a webpage and explore some links on it. */
enter url or number (after a webpage):
http://www.mcs.csuhayward.edu/~kbrown/CS4590/F99/browse.html
----- Beginning of Page -----
CS 4590 Computer Networks
Browser Test page
There are three links on this page. We will follow them all.
Try this [1] link
first.
It will take you to a simple page with several links.
This link points you to an [2] interesting
quote. What do you think?
This last link takes you to my [3] home
page. Can your browser handle it?
----- End of Page --------
press enter key to continue.
press enter key to continue.
enter url or number (after a webpage):
1
----- Beginning of Page -----
CS 4590 Computer Networks
Page 2
There are two links on this page. We will follow them both.
This link points you to an [1] interesting
quote. What do you think?
Back to the [2] Browser
Test page.
----- End of Page --------
press enter key to continue.
enter url or number (after a webpage):
1
----- Beginning of Page -----
CS 4590 Computer Networks
Quote Page
There is only one link on this page. We will follow it.
"I love Computer Science" - anonymous.
Back to the [1] Browser
Test page.
----- End of Page --------
press enter key to continue.
enter url or number (after a webpage):
1
----- Beginning of Page -----
CS 4590 Computer Networks
Browser Test page
There are three links on this page. We will follow them all.
Try this [1] link
first.
It will take you to a simple page with several links.
This link points you to an [2] interesting
quote. What do you think?
This last link takes you to my [3] home
page. Can your browser handle it?
----- End of Page --------
press enter key to continue.
press enter key to continue.
enter url or number (after a webpage):
3
[1] Document moved
Document moved
This document has permanently moved [2] here.
press enter key to continue.
enter url or number (after a webpage):
2
Kevin Brown's Home Page
Kevin Brown
Assistant Professor
Department of Accounting and Computer Information Systems
Department of Math and Computer Science
California State University Hayward
press enter key to continue.
Address:
Kevin Brown
[1] Department of Accounting
and Computer Information Systems
[2] California State University Hayward
[3] Hayward, [4] CA
94542
+1 510 885-4007
Office:
493 Warren Hall
press enter key to continue.
E-mail:
kbrown@csuhayward.edu
Fall quarter 1999 office hours:
Tuesday and Thursday 8:30-9:00 p.m.
Wednesday 1:00-3:00
I am an Assistant Professor at [5] California
State University Hayward, where I enjoy a joint appointment between
press enter key to continue.
the [6] Department of Accounting
and Computer Information Systems in the [7] School
of Business and Economics and the [8] Department
of Math and Computer Science in the [9] School
of Science.
I received my B.S. in Electrical Engineering with a Computer Science
option from [10] Berkeley in 1988.
Afterwards, I worked for [11] ROLM Systems,
a producer of PBX and voice processing products. In 1993, I received my
M.S. in Computer Science from [12] San Diego
State University. And finally, in May of 1997, I received my Ph.D.
in Computer Science from the [13] University
of South Carolina, where I worked in the [14] Mobile
Networking Group supervised by [15] Dr.
Suresh Singh.
My research interests are in network protocols designed to support mobile
hosts.
press enter key to continue.
Courses I'm teaching in Fall 1999:
[16] TC
4266 Section 01 (Day) Network Architecture and Operating Systems
[17] TC
4266 Section 30 (Night) Network Architecture and Operating Systems
[18] CS
4590 Computer Networks
press enter key to continue.
Courses I've taught before:
TC 3250 Section 01
Introduction to Telecommunications
TC 3265/4266
Network Architecture and Operating Systems
CS 3590
Data Communications and Networking
CS 4590
Computer Networks
CIS 6276
--
TC 3250/3276 Introduction to Telecommunications
TC 4266/3265 Network Architecture and Operating Systems
TC 4300 Telecommunications Analysis and Design
press enter key to continue.
CS 3590 Data Communications and Networking
CS 4520 Wireless and Mobile Networking
CS 4590 Computer Networks
CIS 6276 Data and Voice Communications
Recent and Current Work:
[19] My Curriculum
Vitae Research Interests
press enter key to continue.
--
[20] CSUH TELCOT Home page [21] CSUH TELCOT Home page
--
[22] Wireless and Multimedia Conferencing
Group (WAMCON) Home page
[23] Oregon State (used
to be USC) NetGroup Home Page [24] USC NetGroup Research
Papers
My thesis: Communication
Protocols for Wireless Mobile Networks
--
Telecommunications Links
press enter key to continue.
[25] World Wide
Web Virtual Library: Mobile and Wireless Computing Index
[26] CSUH Information and Telecommunications
Society: a student-run group here at CSUH devoted to CIS and Telecom
[27] Internetworking
Technology Overview (Cisco Systems)
[28] Telecom Information
Resources
[29] Telecom
FAQs
press enter key to continue.
[30] Mobilis magazine
When you're not coding, you have to have some [31] fun.
Page Last Modified : June 30, 1998
press enter key to continue.
enter url or number (after a webpage):
Segmentation Fault
/* send four pages that are in the current directory to the web server, */
/* main.html, work.html, fun.html, and movies.html. then, send one */
/* page that is not in the current directory, notfound.html. */
palazzo% wserver2
socket has port #37529
GET /~cs631-17/main.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (Win95; I)
Host: palazzo.mcs.csuhayward.edu:37529
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
GET /~cs631-17/work.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (Win95; I)
Host: palazzo.mcs.csuhayward.edu:37529
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
GET /~cs631-17/fun.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (Win95; I)
Host: palazzo.mcs.csuhayward.edu:37529
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
GET /~cs631-17/movies.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (Win95; I)
Host: palazzo.mcs.csuhayward.edu:37529
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
GET /~cs631-17/notfound.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (Win95; I)
Host: palazzo.mcs.csuhayward.edu:37529
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
openning file: No such file or directory
BACK TO CS4590 PAGE.