Graphics Programs Reference
In-Depth Information
while(recv_line(sockfd, buffer)) {
if(strncasecmp(buffer, "Server:", 7) == 0) {
printf("The web server for %s is %s\n", argv[1], buffer+8);
exit(0);
}
}
printf("Server line not found\n");
exit(1);
}
Most of this code should make sense to you now. The target_addr struc-
ture's sin_addr element is filled using the address from the host_info structure
by typecasting and then dereferencing as before (but this time it's done in a
single line). The connect() function is called to connect to port 80 of the target
host, the command string is sent, and the program loops reading each line
into buffer. The strncasecmp() function is a string comparison function from
strings.h. This function compares the first n bytes of two strings, ignoring
capitalization. The first two arguments are pointers to the strings, and the third
argument is n , the number of bytes to compare. The function will return 0 if
the strings match, so the if statement is searching for the line that starts with
"Server:" . When it finds it, it removes the first eight bytes and prints the web-
server version information. The following listing shows compilation and
execution of the program.
reader@hacking:~/booksrc $ gcc -o webserver_id webserver_id.c
reader@hacking:~/booksrc $ ./webserver_id www.internic.net
The web server for www.internic.net is Apache/2.0.52 (CentOS)
reader@hacking:~/booksrc $ ./webserver_id www.microsoft.com
The web server for www.microsoft.com is Microsoft-IIS/7.0
r eader@hacking:~/booksrc $
0x427
A Tinyweb Server
A webserver doesn't have to be much more complex than the simple server
we created in the previous section. After accepting a TCP-IP connection, the
webserver needs to implement further layers of communication using the
HTTP protocol.
The server code listed below is nearly identical to the simple server, except
that connection handling code is separated into its own function. This func-
tion handles HTTP GET and HEAD requests that would come from a web browser.
The program will look for the requested resource in the local directory called
webroot and send it to the browser. If the file can't be found, the server will
respond with a 404 HTTP response. You may already be familiar with this
response, which means File Not Found . The complete source code listing
follows.
Search WWH ::




Custom Search