aboutsummaryrefslogtreecommitdiffstats
path: root/tools/make-services.py
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2013-08-06 23:45:51 +0000
committerGerald Combs <gerald@wireshark.org>2013-08-06 23:45:51 +0000
commit79713e0ce7cca8521902003da05511c0a8b3a052 (patch)
tree0aed525413e3df517237a6277c7db101f308d1c2 /tools/make-services.py
parenta1c6c09f8fb27f1e71e59ee4360e80d9990e6550 (diff)
Add a Python version of make-services.pl. Instead of trying to parse the
plain text version of the registry it parses the CSV version (which should hopefully be more reliable). Tested with Pythons 2.5, 2.6, 2.7, and 3.3. Update the services file. svn path=/trunk/; revision=51178
Diffstat (limited to 'tools/make-services.py')
-rwxr-xr-xtools/make-services.py168
1 files changed, 168 insertions, 0 deletions
diff --git a/tools/make-services.py b/tools/make-services.py
new file mode 100755
index 0000000000..42047b8f59
--- /dev/null
+++ b/tools/make-services.py
@@ -0,0 +1,168 @@
+#!/usr/bin/env python
+#
+# Parses the CSV version of the IANA Service Name and Transport Protocol Port Number Registry
+# and generates a services(5) file.
+#
+# Wireshark - Network traffic analyzer
+# By Gerald Combs <gerald@wireshark.org>
+# Copyright 2013 Gerald Combs
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+iana_ns = 'http://www.iana.org/assignments'
+iana_port_url = iana_ns + '/service-names-port-numbers/service-names-port-numbers.csv'
+
+__doc__ = '''\
+Usage: make-services.py [url]
+
+url defaults to
+ %s
+''' % (iana_port_url)
+
+import sys
+import getopt
+import csv
+import re
+
+if sys.version_info[0] < 3:
+ import urllib
+else:
+ import urllib.request, urllib.error, urllib.parse
+ import codecs
+
+services_file = 'services'
+
+exclude_services = [
+ '^spr-itunes',
+ '^spl-itunes',
+ '^shilp',
+ ]
+
+exclude_comments = [
+ 'should not be used for discovery purposes',
+ 'NOTE Conflict',
+]
+
+min_body_size = 900000 # Size was ~ 922000 on 2013-08-06
+
+def parse_rows(port_fd):
+ lines = []
+ port_reader = csv.reader(port_fd)
+
+ # Header positions as of 2013-08-06
+ if sys.version_info[0] < 3:
+ headers = port_reader.next()
+ else:
+ headers = next(port_reader)
+
+ try:
+ sn_pos = headers.index('Service Name')
+ except:
+ sn_pos = 0
+ try:
+ pn_pos = headers.index('Port Number')
+ except:
+ pn_pos = 1
+ try:
+ tp_pos = headers.index('Transport Protocol')
+ except:
+ tp_pos = 2
+
+ positions = [sn_pos, pn_pos, tp_pos]
+ positions.sort()
+ positions.reverse()
+
+ for row in port_reader:
+ service = row[sn_pos]
+ port = row[pn_pos]
+ proto = row[tp_pos]
+
+ if len(service) < 1 or len(port) < 1 or len(proto) < 1:
+ continue
+
+ for pos in positions:
+ del row[pos]
+ row = filter(None, row)
+ comment = ' '.join(row)
+ comment = re.sub('[\n]', '', comment)
+
+ if re.search('|'.join(exclude_services), service):
+ continue
+ if re.search('|'.join(exclude_comments), comment):
+ continue
+
+ lines.append('%-15s %5s/%s # %s' % (
+ service,
+ port,
+ proto,
+ comment
+ ))
+
+ return '\n'.join(lines)
+
+def exit_msg(msg=None, status=1):
+ if msg is not None:
+ sys.stderr.write(msg + '\n')
+ sys.stderr.write(__doc__ + '\n')
+ sys.exit(1)
+
+def main(argv):
+ try:
+ opts, args = getopt.getopt(argv, "h", ["help"])
+ except getopt.GetoptError:
+ exit_msg()
+ for opt, arg in opts:
+ if opt in ("-h", "--help"):
+ exit_msg(None, 0)
+
+ if (len(argv) > 0):
+ port_url = argv[0]
+ else:
+ port_url = iana_port_url
+
+ try:
+ if sys.version_info[0] < 3:
+ port_fd = urllib.urlopen(port_url)
+ else:
+ req = urllib.request.urlopen(port_url)
+ port_fd = codecs.getreader('utf8')(req)
+ except URLError:
+ exit_err(URLError)
+
+ body = parse_rows(port_fd)
+ if len(body) < min_body_size:
+ exit_err('Not enough parsed data')
+
+ out = open(services_file, 'w')
+ out.write('''\
+# This is a local copy of the IANA port-numbers file.
+#
+# $Id$
+#
+# Wireshark uses it to resolve port numbers into human readable
+# service names, e.g. TCP port 80 -> http.
+#
+# It is subject to copyright and being used with IANA's permission:
+# http://www.wireshark.org/lists/wireshark-dev/200708/msg00160.html
+#
+# The original file can be found at:
+# %s
+#
+
+%s
+''' % (iana_port_url, body))
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))