Network scanner

RuZzz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 6 05:42:39 PST 2014


Hi ppl! I want to scan the local network to find nodes with open 
80 port.
code:
import core.thread, core.atomic;
import std.stdio, std.system, std.file, std.conv, std.datetime, 
std.socket, std.socketstream, std.stream;
import vibe.core.log;
import vibe.d;

void main()
{
	ushort port = 80;
	auto stIP ="192.168.110.";
	ushort startIP = 1;
	uint size_buf;
	for (ushort i = 1; i < 150; i += size_buf)
	{
		auto socksGlobal = new SocketSet;
		auto socks = new SocketSet;
		size_buf = socksGlobal.max;
		writeln("start cycle ", stIP, i, "\t size socks max ", 
size_buf);
		ushort byteIP = i;
		Socket [] ar_sock;
		ar_sock.length = size_buf;
		foreach (ref msa; ar_sock)
		{
			msa = new TcpSocket();
			msa.blocking = false;
			socksGlobal.add(msa);
			auto addrr = stIP~to!string(byteIP);
			byteIP++;

			auto objAddr = new InternetAddress(addrr, port);
			try
			{
				msa.connect(objAddr);
			}
			catch (SocketException e)
			{
				writeln(e.msg);
			}
		}
		auto ipC = 0;
		while(ipC < 5)
		{
			ipC++;
			int re = 0;
			foreach (ref msa; ar_sock)
			{
				if (socksGlobal.isSet(msa))
					socks.add(msa);
			}
			try
			{
				re = Socket.select(null, socks, null ,dur!"seconds"(3));
			}
			catch (SocketException e)
			{
				writeln(e.msg);
			}
			st = Clock.currTime(UTC());
			writeln(st.toSimpleString~"\t\t: select res : ", re, " sock 
seln", socksGlobal.selectn);

				foreach (ref msa; ar_sock)
				{
					if (socks.isSet(msa))
					{
						st = Clock.currTime(UTC());
						auto addrr = to!string(msa.remoteAddress);
						writeln(st.toSimpleString~"\t\t: "~addrr~" online");
						socksGlobal.remove(msa);
					}
					//msa.close();
				}
			socks.reset();
		}
		foreach (ref msa; ar_sock)
		{
			msa.shutdown(SocketShutdown.BOTH);
			msa.close();
		}
		socksGlobal.reset();
	}
}

output:
start cycle 192.168.110.1	 size socks max 64
2014-Nov-06 12:51:59.46875Z		: select res : 22 sock seln64
2014-Nov-06 12:51:59.46875Z		: 192.168.110.6:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.9:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.10:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.11:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.12:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.14:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.15:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.16:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.17:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.18:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.19:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.20:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.21:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.22:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.23:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.24:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.26:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.27:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.28:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.29:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.30:80 online
2014-Nov-06 12:51:59.484375Z		: 192.168.110.32:80 online
2014-Nov-06 12:52:00.0625Z		: select res : 1 sock seln42
2014-Nov-06 12:52:00.0625Z		: 192.168.110.35:80 online
2014-Nov-06 12:52:00.0625Z		: select res : 1 sock seln41
2014-Nov-06 12:52:00.0625Z		: 192.168.110.31:80 online
2014-Nov-06 12:52:00.421875Z		: select res : 1 sock seln40
2014-Nov-06 12:52:00.421875Z		: 192.168.110.34:80 online
2014-Nov-06 12:52:03.421875Z		: select res : 0 sock seln39
start cycle 192.168.110.65	 size socks max 64
2014-Nov-06 12:52:06.453125Z		: select res : 0 sock seln64
2014-Nov-06 12:52:09.453125Z		: select res : 0 sock seln64
2014-Nov-06 12:52:12.453125Z		: select res : 0 sock seln64
2014-Nov-06 12:52:15.453125Z		: select res : 0 sock seln64
2014-Nov-06 12:52:18.453125Z		: select res : 0 sock seln64
start cycle 192.168.110.129	 size socks max 64
2014-Nov-06 12:52:21.46875Z		: select res : 0 sock seln64
2014-Nov-06 12:52:24.46875Z		: select res : 0 sock seln64
2014-Nov-06 12:52:27.46875Z		: select res : 0 sock seln64
2014-Nov-06 12:52:30.46875Z		: select res : 0 sock seln64
2014-Nov-06 12:52:33.46875Z		: select res : 0 sock seln64

The program doesn't find the address after IP 192.168.110.65
Why?
What can be improved in the program?
Where can i read more about dlang sockets?


More information about the Digitalmars-d-learn mailing list