[tor-commits] [stem/master] Utility function to provide port usage information

atagar at torproject.org atagar at torproject.org
Mon Jan 6 05:55:32 UTC 2014


commit 42bdd8307705ff33b762a60b2ba9d958650f4b59
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 5 21:55:37 2014 -0800

    Utility function to provide port usage information
    
    Adding a stem.util.connection.port_usage() function that provides a brief
    description of a port's common use. This is a capability arm has long hand, and
    nicely complements our ability to query connection information.
---
 docs/change_log.rst          |    1 +
 stem/util/connection.py      |   51 ++++++-
 stem/util/ports.cfg          |  312 ++++++++++++++++++++++++++++++++++++++++++
 test/unit/util/connection.py |   11 ++
 4 files changed, 374 insertions(+), 1 deletion(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 352d4a8..fa8b638 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -49,6 +49,7 @@ The following are only available within stem's `git repository
 
  * **Utilities**
 
+  * Added :func:`stem.util.connection.port_usage`
   * Added :func:`stem.util.system.files_with_suffix`
 
 .. _version_1.1:
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 893230e..cd4fa70 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -8,6 +8,7 @@ Connection and networking based utility functions.
 
   get_connections - quieries the connections belonging to a given process
   get_system_resolvers - provides connection resolution methods that are likely to be available
+  port_usage - brief description of the common usage for a port
 
   is_valid_ipv4_address - checks if a string is a valid IPv4 address
   is_valid_ipv6_address - checks if a string is a valid IPv6 address
@@ -47,7 +48,7 @@ import re
 import stem.util.proc
 import stem.util.system
 
-from stem.util import enum, log
+from stem.util import conf, enum, log
 
 # Connection resolution is risky to log about since it's highly likely to
 # contain sensitive information. That said, it's also difficult to get right in
@@ -79,6 +80,8 @@ FULL_IPv6_MASK = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
 
 CRYPTOVARIABLE_EQUALITY_COMPARISON_NONCE = os.urandom(32)
 
+PORT_USES = None  # port number => description
+
 RESOLVER_COMMAND = {
   Resolver.PROC: '',
 
@@ -269,6 +272,52 @@ def get_system_resolvers(system = None):
   return resolvers
 
 
+def port_usage(port):
+  """
+  Provides the common use of a given port. For example, 'HTTP' for port 80 or
+  'SSH' for 22.
+
+  .. versionadded:: 1.2.0
+
+  :param int port: port number to look up
+
+  :returns: **str** with a description for the port, **None** if none is known
+  """
+
+  global PORT_USES
+
+  if PORT_USES is None:
+    config = conf.Config()
+    config_path = os.path.join(os.path.dirname(__file__), 'ports.cfg')
+
+    try:
+      config.load(config_path)
+      port_uses = {}
+
+      for key, value in config.get('port', {}).items():
+        if key.isdigit():
+         port_uses[int(key)] = value
+        elif '-' in key:
+          min_port, max_port = key.split('-', 1)
+
+          for port_entry in range(int(min_port), int(max_port) + 1):
+            port_uses[port_entry] = value
+        else:
+          raise ValueError("'%s' is an invalid key" % key)
+
+      PORT_USES = port_uses
+    except Exception as exc:
+      log.warn("BUG: stem failed to load its internal port descriptions from '%s': %s" % (config_path, exc))
+
+  if not PORT_USES:
+    return None
+
+  if isinstance(port, str) and port.isdigit():
+    port = int(port)
+
+  return PORT_USES.get(port)
+
+
 def is_valid_ipv4_address(address):
   """
   Checks if a string is a valid IPv4 address.
diff --git a/stem/util/ports.cfg b/stem/util/ports.cfg
new file mode 100644
index 0000000..90c5e90
--- /dev/null
+++ b/stem/util/ports.cfg
@@ -0,0 +1,312 @@
+################################################################################
+#
+# Common usages for port . This is based on...
+#
+#   https://secure.wikimedia.org/wikipedia/en/wiki/List_of_TCP_and_UDP_port numbers
+#   http://isc.sans.edu/services.html
+#
+################################################################################
+
+port 1 => TCPMUX
+port 2 => CompressNET
+port 3 => CompressNET
+port 5 => RJE
+port 7 => Echo
+port 9 => Discard
+port 11 => SYSTAT
+port 13 => Daytime
+port 15 => netstat
+port 17 => QOTD
+port 18 => MSP
+port 19 => CHARGEN
+port 20 => FTP
+port 21 => FTP
+port 22 => SSH
+port 23 => Telnet
+port 24 => Priv-mail
+port 25 => SMTP
+port 34 => RF
+port 35 => Printer
+port 37 => TIME
+port 39 => RLP
+port 41 => Graphics
+port 42 => WINS
+port 43 => WHOIS
+port 47 => NI FTP
+port 49 => TACACS
+port 50 => Remote Mail
+port 51 => IMP
+port 52 => XNS
+port 53 => DNS
+port 54 => XNS
+port 55 => ISI-GL
+port 56 => RAP
+port 57 => MTP
+port 58 => XNS
+port 67 => BOOTP
+port 68 => BOOTP
+port 69 => TFTP
+port 70 => Gopher
+port 79 => Finger
+port 80 => HTTP
+port 81 => HTTP Alternate
+port 82 => Torpark
+port 83 => MIT ML
+port 88 => Kerberos
+port 90 => dnsix
+port 99 => WIP
+port 101 => NIC
+port 102 => ISO-TSAP
+port 104 => ACR/NEMA
+port 105 => CCSO
+port 107 => Telnet
+port 108 => SNA
+port 109 => POP2
+port 110 => POP3
+port 111 => ONC RPC
+port 113 => ident
+port 115 => SFTP
+port 117 => UUCP
+port 118 => SQL
+port 119 => NNTP
+port 123 => NTP
+port 135 => DCE
+port 137 => NetBIOS
+port 138 => NetBIOS
+port 139 => NetBIOS
+port 143 => IMAP
+port 152 => BFTP
+port 153 => SGMP
+port 156 => SQL
+port 158 => DMSP
+port 161 => SNMP
+port 162 => SNMPTRAP
+port 170 => Print-srv
+port 177 => XDMCP
+port 179 => BGP
+port 194 => IRC
+port 199 => SMUX
+port 201 => AppleTalk
+port 209 => QMTP
+port 210 => ANSI
+port 213 => IPX
+port 218 => MPP
+port 220 => IMAP
+port 256 => 2DEV
+port 259 => ESRO
+port 264 => BGMP
+port 308 => Novastor
+port 311 => OSX Admin
+port 318 => PKIX TSP
+port 319 => PTP
+port 320 => PTP
+port 323 => IMMP
+port 350 => MATIP
+port 351 => MATIP
+port 366 => ODMR
+port 369 => Rpc2port ap
+port 370 => codaauth2
+port 371 => ClearCase
+port 383 => HP Alarm Mgr
+port 384 => ARNS
+port 387 => AURP
+port 389 => LDAP
+port 401 => UPS
+port 402 => Altiris
+port 427 => SLP
+port 443 => HTTPS
+port 444 => SNPP
+port 445 => SMB
+port 464 => Kerberos (kpasswd)
+port 465 => SMTP
+port 475 => tcpnethaspsrv
+port 497 => Retrospect
+port 500 => ISAKMP
+port 501 => STMF
+port 502 => Modbus
+port 504 => Citadel
+port 510 => FirstClass
+port 512 => Rexec
+port 513 => rlogin
+port 514 => rsh
+port 515 => LPD
+port 517 => Talk
+port 518 => NTalk
+port 520 => efs
+port 524 => NCP
+port 530 => RPC
+port 531 => AIM/IRC
+port 532 => netnews
+port 533 => netwall
+port 540 => UUCP
+port 542 => commerce
+port 543 => Kerberos (klogin)
+port 544 => Kerberos (kshell)
+port 545 => OSISoft PI
+port 546 => DHCPv6
+port 547 => DHCPv6
+port 548 => AFP
+port 550 => new-who
+port 554 => RTSP
+port 556 => RFS
+port 560 => rmonitor
+port 561 => monitor
+port 563 => NNTPS
+port 587 => SMTP
+port 591 => FileMaker
+port 593 => HTTP RPC
+port 604 => TUNNEL
+port 623 => ASF-RMCP
+port 631 => CUPS
+port 635 => RLZ DBase
+port 636 => LDAPS
+port 639 => MSDP
+port 641 => Support oft
+port 646 => LDP
+port 647 => DHCP
+port 648 => RRP
+port 651 => IEEE-MMS
+port 652 => DTCP
+port 653 => Support oft
+port 654 => MMS/MMP
+port 657 => RMC
+port 660 => OSX Admin
+port 665 => sun-dr
+port 666 => Doom
+port 674 => ACAP
+port 691 => MS Exchange
+port 692 => Hyperwave-ISP
+port 694 => Linux-HA
+port 695 => IEEE-MMS-SSL
+port 698 => OLSR
+port 699 => Access Network
+port 700 => EPP
+port 701 => LMP
+port 702 => IRIS
+port 706 => SILC
+port 711 => MPLS
+port 712 => TBRPF
+port 720 => SMQP
+port 749 => Kerberos (admin)
+port 750 => rfile
+port 751 => pump
+port 752 => qrh
+port 753 => rrh
+port 754 => tell send
+port 760 => ns
+port 782 => Conserver
+port 783 => spamd
+port 829 => CMP
+port 843 => Flash
+port 847 => DHCP
+port 860 => iSCSI
+port 873 => rsync
+port 888 => CDDB
+port 901 => SWAT
+port 902-904 => VMware
+port 911 => NCA
+port 953 => DNS RNDC
+port 981 => SofaWare Firewall
+port 989 => FTPS
+port 990 => FTPS
+port 991 => NAS
+port 992 => Telnet
+port 993 => IMAPS
+port 994 => IRC
+port 995 => POP3S
+port 999 => ScimoreDB
+port 1001 => JtoMB
+port 1002 => cogbot
+
+port 1080 => SOCKS
+port 1085 => WebObjects
+port 1109 => KPOP
+port 1169 => Tripwire
+port 1194 => OpenVPN
+port 1214 => Kazaa
+port 1220 => QuickTime
+port 1234 => VLC
+port 1241 => Nessus
+port 1270 => SCOM
+port 1293 => IPSec
+port 1433 => MSSQL
+port 1434 => MSSQL
+port 1500 => NetGuard
+port 1503 => MSN
+port 1512 => WINS
+port 1521 => Oracle
+port 1526 => Oracle
+port 1533 => Sametime
+port 1666 => Perforce
+port 1677 => GroupWise
+port 1723 => PPTP
+port 1725 => Steam
+port 1863 => MSNP
+port 2049 => NFS
+port 2082 => Infowave
+port 2083 => radsec
+port 2086 => GNUnet
+port 2087 => ELI
+port 2095 => NBX SER
+port 2096 => NBX DIR
+port 2102-2104 => Zephyr
+port 2401 => CVS
+port 2525 => SMTP
+port 2710 => BitTorrent
+port 3074 => XBox LIVE
+port 3101 => BlackBerry
+port 3128 => SQUID
+port 3306 => MySQL
+port 3389 => WBT
+port 3690 => SVN
+port 3723 => Battle.net
+port 3724 => WoW
+port 4321 => RWHOIS
+port 4643 => Virtuozzo
+port 4662 => eMule
+port 5003 => FileMaker
+port 5050 => Yahoo IM
+port 5060 => SIP
+port 5061 => SIP
+port 5190 => AIM/ICQ
+port 5222 => Jabber
+port 5223 => Jabber
+port 5228 => Android Market
+port 5269 => Jabber
+port 5298 => Jabber
+port 5432 => PostgreSQL
+port 5500 => VNC
+port 5556 => Freeciv
+port 5666 => NRPE
+port 5667 => NSCA
+port 5800 => VNC
+port 5900 => VNC
+port 6346 => gnutella
+port 6347 => gnutella
+port 6660-6669 => IRC
+port 6679 => IRC
+port 6697 => IRC
+port 6881-6999 => BitTorrent
+port 8000 => iRDMI
+port 8008 => HTTP Alternate
+port 8010 => XMPP
+port 8074 => Gadu-Gadu
+port 8080 => HTTP Proxy
+port 8087 => SPP
+port 8088 => Radan HTTP
+port 8118 => Privoxy
+port 8123 => Polipo
+port 8443 => PCsync HTTPS
+port 8888 => NewsEDGE
+port 9030 => Tor
+port 9050 => Tor
+port 9051 => Tor
+port 9418 => Git
+port 9999 => distinct
+port 10000 => Webmin
+port 19294 => Google Voice
+port 19638 => Ensim
+port 23399 => Skype
+port 30301 => BitTorrent
+port 33434 => traceroute
+
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index e9c8980..2e0e4bd 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -121,6 +121,17 @@ class TestConnection(unittest.TestCase):
     is_available_mock.return_value = False
     self.assertEqual([Resolver.PROC], stem.util.connection.get_system_resolvers('Linux'))
 
+  def test_port_usage(self):
+    """
+    Check that port_usage can load our config and provide the expected results.
+    """
+
+    self.assertEqual('HTTP', stem.util.connection.port_usage(80))
+    self.assertEqual('HTTP', stem.util.connection.port_usage('80'))  # query with a string
+    self.assertEqual('BitTorrent', stem.util.connection.port_usage(6881))  # min and max value of a range
+    self.assertEqual('BitTorrent', stem.util.connection.port_usage(6999))
+    self.assertEqual(None, stem.util.connection.port_usage(30000))  # unrecognized port
+
   @patch('stem.util.proc.get_connections')
   def test_get_connections_by_proc(self, proc_mock):
     """



More information about the tor-commits mailing list