Getting the socket information from HTTP and DNS sessions
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Mar 5 11:00:58 PST 2015
On 03/04/2015 11:46 PM, Kadir Erdem Demir wrote:
> I have been ask to write a test tool which initiates DNS-HTTP-HTTPS-TCP
> sessions. And ofcourse I wrote this with D.
>
>
> For HTTP I used std.net like "m_HTTP = HTTP(m_url);m_HTTP.perform();"
> For DNS I simply used "getAddressInfo(m_domainName);"
>
> Than tool makes some simple checks which are npt subjects of this
> thread and the tool works well,
>
> But now I need to make an improvement. I need to get sourceIP(ip which
> program runs), sourcePort( eg: 53212 not 80), destIP, destPort(most
> probably 80) from the session which I initiated.
>
> I need functions like std.socket.localAddress, std.socket.remoteAddress
> for HTTP-DNS but couldn't managed to find them.
>
> Is there any way that I can get socket info in HTTP and DNS sessions
> which I initiated?
>
> Regards
> Kadir Erdem
std.net is implemented in terms of libcurl but does not expose all of
its functionality. I could not find a way of extracting curl-related
information from it.
I found the following Socket example in my stash of code where it's easy
to get the address information with remoteAddress() and localAddress().
But of course Socket is lower level than std.net. :-/
import std.stdio;
import std.socket;
void main() {
Socket server = new TcpSocket();
server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR,
true);
server.bind(new InternetAddress(8080));
server.listen(1);
while(true) {
Socket client = server.accept();
writefln("client Socket connected on %s", client.remoteAddress);
char[1024] buffer;
auto received = client.receive(buffer);
writefln("The client said:\n%s", buffer[0.. received]);
enum header =
"HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n";
string response = header ~ "Hello World!\n";
client.send(response);
client.shutdown(SocketShutdown.BOTH);
client.close();
}
}
Ali
More information about the Digitalmars-d-learn
mailing list