blob: 8df121949ef8bcc2d6872eaf7e688a0ddbddeaf6 [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
Steve McIntyre488b9f92018-02-07 19:22:16 +000095 try:
96 # send the actual number of bytes to read.
97 self.conn.send(data[0])
98 # now send the bytes.
99 self.conn.send(data[1])
100 except socket.error as e:
101 logging.error("Can't send response to client: %s", e)
102 logging.error("Was trying to send data:")
103 logging.error(data)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000104
105 def server_close(self):
106 if self.conn is not None:
Steve McIntyre10dc6632015-01-23 18:04:11 +0000107 self.conn.shutdown(socket.SHUT_RDWR)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000108 self.conn.close()
109
110 def client_connect(self, host, port):
111 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
112 while True:
113 try:
114 ret = self.socket.connect_ex((host, port))
115 if ret:
116 self.socket.close()
117 self.socket = None
118 raise SocketError("Client can't send connect: %s" % ret)
119 else:
120 break
121 except socket.error:
122 time.sleep(1)
123 return True
124
125 def client_send(self, json_data):
126 if self.socket is None:
127 raise SocketError("Client can't send data: no socket")
128
129 data = self._format_message(json_data)
130 if not data:
131 self.socket.shutdown(socket.SHUT_RDWR)
132 self.socket.close()
133 self.socket = None
134 raise SocketError("Client unable to send data")
135
136 # send the actual number of bytes to read.
137 self.socket.send(data[0])
138 # now send the bytes.
139 self.socket.send(data[1])
140
141 def client_recv_and_close(self):
142 if self.socket is None:
143 raise SocketError("Client can't receieve data: no socket")
144
145 data = self.socket.recv(8) # 32bit limit
146 count = int(data, 16)
147 c = 0
148 data = ''
149 while c < count:
150 data += self.socket.recv(1)
151 c += 1
152 try:
153 json_data = json.loads(data)
154 except ValueError:
155 self.socket.close()
156 self.socket = None
157 raise SocketError("Client unable to decode receieved data: corrupt?")
158
159 self.socket.shutdown(socket.SHUT_RDWR)
160 self.socket.close()
161
162 return json_data
Steve McIntyreb8832e02014-12-18 16:16:49 +0000163
164 # The default JSON serialiser code can't deal with datetime
165 # objects by default, so let's tell it how to.
166 def _json_serial(self, obj):
167 """JSON serializer for objects not serialisable by default json code"""
168 if isinstance(obj, datetime.datetime):
169 serial = obj.isoformat()
170 return serial
Steve McIntyre1c8a3212015-07-14 17:07:31 +0100171
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000172 def _format_message(self, json_data):
173 try:
Steve McIntyreb8832e02014-12-18 16:16:49 +0000174 msgstr = json.dumps(json_data, default=self._json_serial)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000175 except ValueError:
176 return None
177 # "header" calculation
178 msglen = "%08X" % len(msgstr)
179 return (msglen, msgstr)