1
# -*- coding: utf-8 -*-
3
# (c) Copyright 2003-2015 HP 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
34
from .sixext import to_bytes_utf8, to_unicode, to_string_utf8
36
prod_pat = re.compile(r"""\(\s*x-hp-prod_id\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
37
mac_pat = re.compile(r"""\(\s*x-hp-mac\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
38
num_port_pat = re.compile(r"""\(\s*x-hp-num_port\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
39
ip_pat = re.compile(r"""\(\s*x-hp-ip\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
40
p1_pat = re.compile(r"""\(\s*x-hp-p1\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
41
p2_pat = re.compile(r"""\(\s*x-hp-p2\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
42
p3_pat = re.compile(r"""\(\s*x-hp-p3\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
43
hn_pat = re.compile(r"""\(\s*x-hp-hn\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
45
def createSocketsWithsetOption(ttl=4):
49
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
50
x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
51
x.connect(('1.2.3.4', 56))
52
intf = x.getsockname()[0]
55
ttl = struct.pack('B', ttl)
57
log.error("Network error")
63
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
64
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
65
except (AttributeError, socket.error):
69
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
70
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
71
s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP ,1)
72
except Exception as e:
73
log.error("Unable to setup multicast socket for SLP: %s" % e)
80
def detectNetworkDevices(ttl=4, timeout=10): #, xid=None, qappobj = None):
81
mcast_addr, mcast_port ='224.0.1.60', 427
84
s = createSocketsWithsetOption(ttl)
88
packet = b''.join([to_bytes_utf8('\x01\x06\x00\x2c\x00\x00\x65\x6e\x00\x03'),
89
struct.pack('!H', random.randint(1, 65535)), to_bytes_utf8('\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00')])
92
s.sendto(packet, 0, (mcast_addr, mcast_port))
93
except socket.error as e:
94
log.error("Unable to send broadcast SLP packet: %s" % e)
98
start_time = time.time()
99
r, w, e = select.select([s], [], [s], time_left)
100
time_left -= (time.time() - start_time)
103
data, addr = s.recvfrom(2048)
106
log.log_data(data, width=32)
109
ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
110
struct.unpack("!BBHBBHHHHH", data[:16])
112
x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
117
num_ports = int(num_port_pat.search(x).group(1))
118
except (AttributeError, ValueError):
121
if num_ports == 0: # Embedded devices
124
y = {'num_devices' : 0, 'num_ports': num_ports, 'product_id' : '',
125
'status_code': 0, 'device2': '0', 'device3': '0', 'note': '', 'device1': '0'}
129
y['device1'] = p1_pat.search(x).group(1)
130
except AttributeError:
133
y['num_devices'] += 1
136
if num_ports > 1: # Check port 2
138
y['device2'] = p2_pat.search(x).group(1)
139
except AttributeError:
142
y['num_devices'] += 1
145
if num_ports > 2: # Check port 3
147
y['device3'] = p3_pat.search(x).group(1)
148
except AttributeError:
151
y['num_devices'] += 1
153
if y['device1'] is None:
156
if y['device2'] is None:
159
if y['device3'] is None:
163
y['product_id'] = prod_pat.search(x).group(1)
164
except AttributeError:
167
y['mac'] = mac_pat.search(x).group(1)
168
except AttributeError:
171
y['ip'] = ip_pat.search(x).group(1)
172
except AttributeError:
175
y['hn'] = hn_pat.search(x).group(1)
176
except AttributeError:
179
y['status_code'] = status_code
180
found_devices[addr[0]] = y
182
log.debug("Found device: %s" % y)