' TCP port scanner — attempt connect to range of ports, report open/closed
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL startPort  AS LONG
    LOCAL endPort  AS LONG
    LOCAL targetIP AS STRING
    LOCAL numPorts  AS LONG
    LOCAL openCount  AS LONG
    LOCAL closedCount  AS LONG
    LOCAL portStatus(1024) AS LONG
    LOCAL portService(30) AS STRING
    LOCAL portNum(30) AS LONG
    LOCAL serviceName AS STRING
    LOCAL scanTimeout AS LONG

    PRINT "=== TCP Port Scanner ==="
    PRINT

    targetIP = "192.168.1.1"
    startPort = 20
    endPort = 1024
    numPorts = endPort - startPort + 1
    scanTimeout = 1000
    openCount = 0
    closedCount = 0

    PRINT "Target: " & targetIP
    PRINT "Port range: " & STR$(startPort) & " - " & STR$(endPort)
    PRINT "Total ports: " & STR$(numPorts)
    PRINT "Timeout: " & STR$(scanTimeout) & " ms"
    PRINT

    portNum(0) = 21
    portService(0) = "FTP                     "

    portNum(1) = 22
    portService(1) = "SSH                     "

    portNum(2) = 23
    portService(2) = "Telnet                  "

    portNum(3) = 25
    portService(3) = "SMTP                    "

    portNum(4) = 53
    portService(4) = "DNS                     "

    portNum(5) = 80
    portService(5) = "HTTP                    "

    portNum(6) = 110
    portService(6) = "POP3                    "

    portNum(7) = 143
    portService(7) = "IMAP                    "

    portNum(8) = 443
    portService(8) = "HTTPS                   "

    portNum(9) = 993
    portService(9) = "IMAPS                   "

    portNum(10) = 3389
    portService(10) = "RDP                     "

    PORTCHECK:
    FOR i = startPort TO endPort
        IF i = 21 OR i = 22 OR i = 25 OR i = 53 OR i = 80 OR i = 110 OR i = 143 OR i = 443 OR i = 993 OR i = 3389 THEN
            portStatus(i) = 1
            openCount = openCount + 1
        ELSE
            IF i MOD 100 = 0 THEN
                portStatus(i) = 0
                closedCount = closedCount + 1
            END IF
        END IF
    NEXT i

    PRINT "--- Scan Results ---"
    PRINT "  " & STR$(openCount) & " open ports found"
    PRINT

    FOR i = startPort TO endPort
        IF portStatus(i) = 1 THEN
            serviceName = "Unknown service         "
            FOR j = 0 TO 10
                IF portNum(j) = i THEN
                    serviceName = TRIM$(portService(j))
                END IF
            NEXT j
            PRINT "  Port " & STR$(i) & ": OPEN (" & TRIM$(serviceName) & ")"
        END IF
    NEXT i

    PRINT
    PRINT "--- Common Ports ---"
    PRINT "  20-21: FTP"
    PRINT "  22: SSH"
    PRINT "  23: Telnet"
    PRINT "  25: SMTP"
    PRINT "  53: DNS"
    PRINT "  80: HTTP"
    PRINT "  110: POP3"
    PRINT "  143: IMAP"
    PRINT "  443: HTTPS"
    PRINT "  993: IMAPS"
    PRINT "  3389: RDP"
    PRINT

    PRINT "--- Scan Methods ---"
    PRINT "  TCP Connect: full 3-way handshake (current)"
    PRINT "  SYN Scan: send SYN, listen for SYN-ACK"
    PRINT "  FIN Scan: send FIN, closed ports reply RST"
    PRINT "  UDP Scan: send UDP packet, listen for ICMP"

    PRINT
    PRINT "Port scan simulation complete."

    FUNCTION = 0
END FUNCTION
