Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 1 | # Copyright 2014 Linaro Limited |
| 2 | # |
| 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 20 | import socket |
| 21 | import json |
| 22 | import time |
| 23 | import datetime |
| 24 | import os |
| 25 | import sys |
| 26 | import logging |
Steve McIntyre | a3c8767 | 2014-12-15 17:59:48 +0000 | [diff] [blame] | 27 | |
| 28 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 29 | sys.path.insert(0, vlandpath) |
| 30 | from errors import CriticalError, InputError, ConfigError, SocketError |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 31 | |
| 32 | class VlanIpc: |
| 33 | """VLANd IPC class""" |
| 34 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 35 | def __init__(self): |
| 36 | self.conn = None |
| 37 | self.socket = None |
| 38 | |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 39 | def server_init(self, host, port): |
| 40 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Steve McIntyre | 10dc663 | 2015-01-23 18:04:11 +0000 | [diff] [blame] | 41 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Steve McIntyre | 6efb279 | 2015-02-09 06:36:11 +0000 | [diff] [blame] | 42 | self.conn = None |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 43 | |
| 44 | while True: |
| 45 | try: |
| 46 | self.socket.bind((host, port)) |
| 47 | break |
Steve McIntyre | dfa1e2c | 2014-12-23 13:24:33 +0000 | [diff] [blame] | 48 | except socket.error as e: |
| 49 | print "Can't bind to port %d: %s" % (port, e) |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 50 | 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 McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 62 | logging.debug("server: Connection from") |
| 63 | logging.debug(addr) |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 64 | 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 McIntyre | 10dc663 | 2015-01-23 18:04:11 +0000 | [diff] [blame] | 102 | self.conn.shutdown(socket.SHUT_RDWR) |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 103 | 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 McIntyre | b8832e0 | 2014-12-18 16:16:49 +0000 | [diff] [blame] | 158 | |
| 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 McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame^] | 166 | |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 167 | def _format_message(self, json_data): |
| 168 | try: |
Steve McIntyre | b8832e0 | 2014-12-18 16:16:49 +0000 | [diff] [blame] | 169 | msgstr = json.dumps(json_data, default=self._json_serial) |
Steve McIntyre | b1cbad3 | 2014-12-12 21:40:25 +0000 | [diff] [blame] | 170 | except ValueError: |
| 171 | return None |
| 172 | # "header" calculation |
| 173 | msglen = "%08X" % len(msgstr) |
| 174 | return (msglen, msgstr) |