www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Network scanner

reply "RuZzz" <xruzzzz gmail.com> writes:
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?
Nov 06 2014
next sibling parent "RuZzz" <xruzzzz gmail.com> writes:
Or the program doesn't find the address after IP 192.168.110.34...
Nov 06 2014
prev sibling next sibling parent "RuZzz" <xruzzzz gmail.com> writes:
OS WinXP
Nov 11 2014
prev sibling parent reply "RuZzz" <xruzzzz gmail.com> writes:
netstat reports that the socket is in the TIME_WAIT or CLOSE_WAIT 
state.
Nov 11 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 11 Nov 2014 15:35:28 +0000
RuZzz via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 netstat reports that the socket is in the TIME_WAIT or CLOSE_WAIT=20
 state.
i'm not an expert in winsock, but did you tried to set SO_LINGER to "off"?
Nov 11 2014
parent =?UTF-8?B?IkrDvHJnZW4=?= Reichmann" <jr rdvsb.de> writes:
On Tuesday, 11 November 2014 at 16:04:21 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Tue, 11 Nov 2014 15:35:28 +0000
 RuZzz via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:

 netstat reports that the socket is in the TIME_WAIT or 
 CLOSE_WAIT state.
i'm not an expert in winsock, but did you tried to set SO_LINGER to "off"?
Closed ports stay in state WAIT for quite a while. If SO_LINGER=off doesn't help try to tune some Windows TCP parameters: - reduce TcpTimedWaitDelay from 120 to 30 seconds - use ports above 5000 - bigger port hash table Remember to reboot after stetting these registry values: REGEDIT4 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] "TcpTimedWaitDelay"=dword:0000001e "MaxUserPort"=dword:fffe "MaxHashTableSize"=dword:ffff "NumTcbTablePartitions"=dword:8
Nov 11 2014