1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 prod_pat = re.compile(r"""\(\s*x-hp-prod_id\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
36 mac_pat = re.compile(r"""\(\s*x-hp-mac\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
37 num_port_pat = re.compile(r"""\(\s*x-hp-num_port\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
38 ip_pat = re.compile(r"""\(\s*x-hp-ip\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
39 p1_pat = re.compile(r"""\(\s*x-hp-p1\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
40 p2_pat = re.compile(r"""\(\s*x-hp-p2\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
41 p3_pat = re.compile(r"""\(\s*x-hp-p3\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
42 hn_pat = re.compile(r"""\(\s*x-hp-hn\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
45 def detectNetworkDevices(ttl=4, timeout=10): #, xid=None, qappobj = None):
46 mcast_addr, mcast_port ='224.0.1.60', 427
49 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
51 x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
54 x.connect(('1.2.3.4', 56))
56 log.error("Network is unreachable. Please check your network connection and try again.")
59 intf = x.getsockname()[0]
63 ttl = struct.pack('B', ttl)
66 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
67 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
68 except (AttributeError, socket.error):
72 s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
73 s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
74 s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP ,1)
76 log.error("Unable to setup multicast socket for SLP: %s" % e)
79 packet = ''.join(['\x01\x06\x00\x2c\x00\x00\x65\x6e\x00\x03',
80 struct.pack('!H', random.randint(1, 65535)), '\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00'])
83 s.sendto(packet, 0, (mcast_addr, mcast_port))
84 except socket.error, e:
85 log.error("Unable to send broadcast SLP packet: %s" % e)
89 start_time = time.time()
90 r, w, e = select.select([s], [], [s], time_left)
91 time_left -= (time.time() - start_time)
94 data, addr = s.recvfrom(2048)
97 log.log_data(data, width=32)
100 ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
101 struct.unpack("!BBHBBHHHHH", data[:16])
103 x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
108 num_ports = int(num_port_pat.search(x).group(1))
109 except (AttributeError, ValueError):
112 if num_ports == 0: # Embedded devices
115 y = {'num_devices' : 0, 'num_ports': num_ports, 'product_id' : '',
116 'status_code': 0, 'device2': '0', 'device3': '0', 'note': '', 'device1': '0'}
120 y['device1'] = p1_pat.search(x).group(1)
121 except AttributeError:
124 y['num_devices'] += 1
127 if num_ports > 1: # Check port 2
129 y['device2'] = p2_pat.search(x).group(1)
130 except AttributeError:
133 y['num_devices'] += 1
136 if num_ports > 2: # Check port 3
138 y['device3'] = p3_pat.search(x).group(1)
139 except AttributeError:
142 y['num_devices'] += 1
144 if y['device1'] is None:
147 if y['device2'] is None:
150 if y['device3'] is None:
154 y['product_id'] = prod_pat.search(x).group(1)
155 except AttributeError:
158 y['mac'] = mac_pat.search(x).group(1)
159 except AttributeError:
162 y['ip'] = ip_pat.search(x).group(1)
163 except AttributeError:
166 y['hn'] = hn_pat.search(x).group(1)
167 except AttributeError:
170 y['status_code'] = status_code
171 found_devices[addr[0]] = y
173 log.debug("Found device: %s" % y)