I want to create a tcp connection to a web server using information that I get from a packet like the one shown below. To do this I need the hostname and portno from the packet to get an address that I can use with the connection function
This is assuming that I am using c++ Can I assume that the portno to talk to a server to request for html pages will be 80? How do I get the hostname from the packet assuming that the packet is in the form of char array? I currently extract the string of characters bits.wikimedia.org and using that as the hostname. Is that correct? Once I have the host name, I assume that I pass it in to getaddrinfo and use the structure that I passed in along with this function to generate a struct containing information understandable to the connect function. Is this assumption correct?
GET http://bits.wikimedia.org/en.wikipedia.org/load.php?debug=false&lang=en&modules=site&only=scripts&skin=vector&* HTTP/1.1
Host: bits.wikimedia.org
Proxy-Connection: close
User-Agent: Mozilla/5.0 (compatible; Konqueror/4.6; Linux) KHTML/4.6.5 (like Gecko) Fedora/4.6.5-7.fc15
Referer: http://en.wikipedia.org/wiki/Firewall_(computing)
Accept: */*
Accept-Encoding: x-gzip, x-deflate, gzip, deflate
Accept-Charset: utf-8, utf-8;q=0.5, *;q=0.5
Accept-Language: en-US,en;q=0.9
You should be able to rely on the Host: header field to have the host name in it.
Look at the link to see how this is formatted. You need to read the header line by line, identify the "Host:" line, extract the following string, possibly extract the port number if given (host:port).
Yes, getaddrinfo() could be used to obtain an IP address(es) for the host name.