blob: a7c6c49d7bffe20dcc42820584b04ae8eaa3e087 [file] [log] [blame]
Steve McIntyre94ef65e2015-09-25 01:08:14 +01001# Copyright 2014-2015 Linaro Limited
Steve McIntyreb1cbad32014-12-12 21:40:25 +00002#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16# MA 02110-1301, USA.
17#
18# Simple VLANd IPC module
19
Steve McIntyre5fa22652015-04-01 18:01:45 +010020import socket
21import json
22import time
23import datetime
24import os
25import sys
26import logging
Steve McIntyrea3c87672014-12-15 17:59:48 +000027
28vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
29sys.path.insert(0, vlandpath)
30from errors import CriticalError, InputError, ConfigError, SocketError
Steve McIntyreb1cbad32014-12-12 21:40:25 +000031
32class VlanIpc:
33 """VLANd IPC class"""
34
Steve McIntyre5fa22652015-04-01 18:01:45 +010035 def __init__(self):
36 self.conn = None
37 self.socket = None
38
Steve McIntyreb1cbad32014-12-12 21:40:25 +000039 def server_init(self, host, port):
40 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Steve McIntyre10dc6632015-01-23 18:04:11 +000041 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Steve McIntyre6efb2792015-02-09 06:36:11 +000042 self.conn = None
Steve McIntyreb1cbad32014-12-12 21:40:25 +000043
44 while True:
45 try:
46 self.socket.bind((host, port))
47 break
Steve McIntyredfa1e2c2014-12-23 13:24:33 +000048 except socket.error as e:
49 print "Can't bind to port %d: %s" % (port, e)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000050 time.sleep(1)
51
52 def server_listen(self):
53 if self.socket is None:
54 raise SocketError("Server can't receive data: no socket")
55 self.socket.listen(1)
56
57 def server_recv(self):
58 if self.socket is None:
59 raise SocketError("Server can't receive data: no socket")
60
61 self.conn, addr = self.socket.accept()
Steve McIntyre5fa22652015-04-01 18:01:45 +010062 logging.debug("server: Connection from")
63 logging.debug(addr)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000064 data = self.conn.recv(8) # 32bit limit
65 count = int(data, 16)
66 c = 0
67 data = ''
68 while c < count:
69 data += self.conn.recv(1)
70 c += 1
71 try:
72 json_data = json.loads(data)
73 except ValueError:
74 self.conn.close()
75 self.conn = None
76 raise SocketError("Server unable to decode receieved data: corrupt?")
77
78 if 'client_name' not in json_data:
79 self.conn.close()
80 self.conn = None
81 raise SocketError("Server unable to detect client name: corrupt packet?")
82
83 return json_data
84
85 def server_reply(self, json_data):
86 if self.conn is None:
87 raise SocketError("Server can't send data: no connection")
88
89 data = self._format_message(json_data)
90 if not data:
91 self.conn.close()
92 self.conn = None
93 raise SocketError("Server unable to format reply data")
94
95 # send the actual number of bytes to read.
96 self.conn.send(data[0])
97 # now send the bytes.
98 self.conn.send(data[1])
99
100 def server_close(self):
101 if self.conn is not None:
Steve McIntyre10dc6632015-01-23 18:04:11 +0000102 self.conn.shutdown(socket.SHUT_RDWR)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000103 self.conn.close()
104
105 def client_connect(self, host, port):
106 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
107 while True:
108 try:
109 ret = self.socket.connect_ex((host, port))
110 if ret:
111 self.socket.close()
112 self.socket = None
113 raise SocketError("Client can't send connect: %s" % ret)
114 else:
115 break
116 except socket.error:
117 time.sleep(1)
118 return True
119
120 def client_send(self, json_data):
121 if self.socket is None:
122 raise SocketError("Client can't send data: no socket")
123
124 data = self._format_message(json_data)
125 if not data:
126 self.socket.shutdown(socket.SHUT_RDWR)
127 self.socket.close()
128 self.socket = None
129 raise SocketError("Client unable to send data")
130
131 # send the actual number of bytes to read.
132 self.socket.send(data[0])
133 # now send the bytes.
134 self.socket.send(data[1])
135
136 def client_recv_and_close(self):
137 if self.socket is None:
138 raise SocketError("Client can't receieve data: no socket")
139
140 data = self.socket.recv(8) # 32bit limit
141 count = int(data, 16)
142 c = 0
143 data = ''
144 while c < count:
145 data += self.socket.recv(1)
146 c += 1
147 try:
148 json_data = json.loads(data)
149 except ValueError:
150 self.socket.close()
151 self.socket = None
152 raise SocketError("Client unable to decode receieved data: corrupt?")
153
154 self.socket.shutdown(socket.SHUT_RDWR)
155 self.socket.close()
156
157 return json_data
Steve McIntyreb8832e02014-12-18 16:16:49 +0000158
159 # The default JSON serialiser code can't deal with datetime
160 # objects by default, so let's tell it how to.
161 def _json_serial(self, obj):
162 """JSON serializer for objects not serialisable by default json code"""
163 if isinstance(obj, datetime.datetime):
164 serial = obj.isoformat()
165 return serial
Steve McIntyre1c8a3212015-07-14 17:07:31 +0100166
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000167 def _format_message(self, json_data):
168 try:
Steve McIntyreb8832e02014-12-18 16:16:49 +0000169 msgstr = json.dumps(json_data, default=self._json_serial)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000170 except ValueError:
171 return None
172 # "header" calculation
173 msglen = "%08X" % len(msgstr)
174 return (msglen, msgstr)