3 # Parses the CSV version of the IANA Service Name and Transport Protocol Port Number Registry
4 # and generates a services(5) file.
6 # Wireshark - Network traffic analyzer
7 # By Gerald Combs <gerald@wireshark.org>
8 # Copyright 2013 Gerald Combs
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 iana_svc_url = 'http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv'
27 Usage: make-services.py [url]
38 python_version = sys.hexversion >> 16
39 if python_version < 0x300:
42 import urllib.request, urllib.error, urllib.parse
45 services_file = 'services'
54 'should not be used for discovery purposes',
58 min_body_size = 900000 # Size was ~ 922000 on 2013-08-06
60 def parse_rows(svc_fd):
62 port_reader = csv.reader(svc_fd)
64 # Header positions as of 2013-08-06
65 if python_version < 0x206:
66 headers = port_reader.next()
68 headers = next(port_reader)
71 sn_pos = headers.index('Service Name')
75 pn_pos = headers.index('Port Number')
79 tp_pos = headers.index('Transport Protocol')
83 positions = [sn_pos, pn_pos, tp_pos]
87 for row in port_reader:
92 if len(service) < 1 or len(port) < 1 or len(proto) < 1:
97 row = filter(None, row)
98 comment = ' '.join(row)
99 comment = re.sub('[\n]', '', comment)
101 if re.search('|'.join(exclude_services), service):
103 if re.search('|'.join(exclude_comments), comment):
106 lines.append('%-15s %5s/%s # %s' % (
113 return '\n'.join(lines)
115 def exit_msg(msg=None, status=1):
117 sys.stderr.write(msg + '\n\n')
118 sys.stderr.write(__doc__ + '\n')
123 opts, args = getopt.getopt(argv, "h", ["help"])
124 except getopt.GetoptError:
126 for opt, arg in opts:
127 if opt in ("-h", "--help"):
133 svc_url = iana_svc_url
136 if python_version < 0x300:
137 svc_fd = urllib.urlopen(svc_url)
139 req = urllib.request.urlopen(svc_url)
140 svc_fd = codecs.getreader('utf8')(req)
142 exit_msg('Error opening ' + svc_url)
144 body = parse_rows(svc_fd)
145 if len(body) < min_body_size:
146 exit_msg('Not enough parsed data')
148 out = open(services_file, 'w')
150 # This is a local copy of the IANA port-numbers file.
154 # Wireshark uses it to resolve port numbers into human readable
155 # service names, e.g. TCP port 80 -> http.
157 # It is subject to copyright and being used with IANA's permission:
158 # http://www.wireshark.org/lists/wireshark-dev/200708/msg00160.html
160 # The original file can be found at:
165 ''' % (iana_svc_url, body))
167 if __name__ == "__main__":
168 sys.exit(main(sys.argv[1:]))