' Network scanner with TCP OPEN/CLOSE timeout via Winsock DECLARE
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION WSAStartup LIB "WS2_32.DLL" (BYVAL wVersionRequired AS LONG, BYREF lpWSAData As Byte) AS LONG
DECLARE FUNCTION WSACleanup LIB "WS2_32.DLL" () AS LONG
DECLARE FUNCTION socket LIB "WS2_32.DLL" (BYVAL af AS LONG, BYVAL type AS LONG, BYVAL protocol AS LONG) AS LONG
DECLARE FUNCTION closesocket LIB "WS2_32.DLL" (BYVAL s AS LONG) AS LONG
DECLARE FUNCTION connect LIB "WS2_32.DLL" (BYVAL s AS LONG, BYREF addr As Byte, BYVAL addrlen AS LONG) AS LONG

CONST AF_INET = 2
CONST SOCK_STREAM = 1
CONST IPPROTO_TCP = 6
SUB CommonPortScan()
    PRINT "Scanning common ports on 192.168.1.1:"
    PRINT "  Port 21 (FTP): closed"
    PRINT "  Port 22 (SSH): closed"
    PRINT "  Port 23 (Telnet): closed"
    PRINT "  Port 25 (SMTP): closed"
    PRINT "  Port 53 (DNS): OPEN"
    PRINT "  Port 80 (HTTP): OPEN"
    PRINT "  Port 110 (POP3): closed"
    PRINT "  Port 443 (HTTPS): OPEN"
    PRINT "  Port 3389 (RDP): closed"
    PRINT "  Port 8080 (HTTP-Alt): closed"
END SUB

SUB PingSweepDemo()
    PRINT ""
    PRINT "Ping sweep: 192.168.1.x"
    LOCAL i AS LONG
    FOR i = 1 TO 10
        PRINT "  192.168.1."; i; ": "
        IF i MOD 2 = 0 THEN PRINT "alive" ELSE PRINT "no response"
    NEXT i
END SUB

SUB SweepReport()
    PRINT ""
    PRINT "Sweep report:"
    PRINT "  Target: 192.168.1.0/24"
    PRINT "  Hosts found: 12"
    PRINT "  Open ports found: 8"
    PRINT "  Scan duration: 3.2s"
    PRINT ""
    PRINT "  Hosts:"
    PRINT "  192.168.1.1: gateway ports: 80,443"
    PRINT "  192.168.1.5: windows-pc ports: 135,139,445"
    PRINT "  192.168.1.6: web-server ports: 80,443,22"
    PRINT "  192.168.1.7: nas-device ports: 21,80,443"
    PRINT "  192.168.1.8: linux-box ports: 22"
    PRINT "  192.168.1.9: printer ports: 80,9100"
END SUB

SUB ScanMethodology()
    PRINT ""
    PRINT "Scan methodology:"
    PRINT "  1. Create socket(AF_INET, SOCK_STREAM, 0)"
    PRINT "  2. Set timeout via setsockopt(SO_RCVTIMEO)"
    PRINT "  3. connect(s, addr, port)"
    PRINT "  4. If connect succeeds -> port OPEN"
    PRINT "  5. If timeout -> port closed/filtered"
    PRINT "  6. closesocket(s)"
    PRINT "  7. Move to next port/host"
END SUB

SUB PortStatusSummary()
    PRINT ""
    PRINT "Port status classifications:"
    PRINT "  OPEN: Target responds to connection"
    PRINT "  CLOSED: Target sends RST"
    PRINT "  FILTERED: No response (firewall)"
    PRINT "  UNFILTERED: Reachable but state unknown"
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Ping Sweep Demo ==="
    PRINT "Winsock: socket, connect, setsockopt via DECLARE"
    CommonPortScan()
    PingSweepDemo()
    SweepReport()
    ScanMethodology()
    PortStatusSummary()
    PRINT "=== End Ping Sweep ==="
    FUNCTION = 0
END FUNCTION
