2014-04-07 20:43:39 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2014 IBM Corporation
|
2015-01-19 19:45:24 +00:00
|
|
|
# Copyright 2015 Lenovo
|
2014-04-07 20:43:39 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2013-10-13 01:19:15 +00:00
|
|
|
|
2021-02-17 22:04:16 +00:00
|
|
|
import array
|
2021-02-17 19:31:14 +00:00
|
|
|
import ctypes
|
|
|
|
import ctypes.util
|
2014-05-06 19:01:35 +00:00
|
|
|
import confluent.tlv as tlv
|
2022-06-08 15:03:56 +00:00
|
|
|
try:
|
|
|
|
import eventlet.green.socket as socket
|
|
|
|
import eventlet.green.select as select
|
|
|
|
except ImportError:
|
|
|
|
import socket
|
|
|
|
import select
|
2016-04-20 20:51:01 +00:00
|
|
|
from datetime import datetime
|
2014-02-09 15:43:26 +00:00
|
|
|
import json
|
2021-02-18 19:58:45 +00:00
|
|
|
import os
|
2013-10-13 01:19:15 +00:00
|
|
|
import struct
|
|
|
|
|
2018-06-21 18:29:54 +00:00
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
unicode = str
|
|
|
|
|
2019-10-02 15:29:13 +00:00
|
|
|
try:
|
|
|
|
range = xrange
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
|
2021-02-17 19:31:14 +00:00
|
|
|
class iovec(ctypes.Structure): # from uio.h
|
|
|
|
_fields_ = [('iov_base', ctypes.c_void_p),
|
|
|
|
('iov_len', ctypes.c_size_t)]
|
2021-02-01 14:05:15 +00:00
|
|
|
|
2021-02-17 22:04:16 +00:00
|
|
|
|
2021-02-26 21:17:43 +00:00
|
|
|
iovec_ptr = ctypes.POINTER(iovec)
|
|
|
|
|
|
|
|
|
2021-02-17 22:04:16 +00:00
|
|
|
class cmsghdr(ctypes.Structure): # also from bits/socket.h
|
|
|
|
_fields_ = [('cmsg_len', ctypes.c_size_t),
|
|
|
|
('cmsg_level', ctypes.c_int),
|
|
|
|
('cmsg_type', ctypes.c_int)]
|
|
|
|
|
2021-02-26 21:17:43 +00:00
|
|
|
@classmethod
|
|
|
|
def init_data(cls, cmsg_len, cmsg_level, cmsg_type, cmsg_data):
|
|
|
|
Data = ctypes.c_ubyte * ctypes.sizeof(cmsg_data)
|
|
|
|
class _flexhdr(ctypes.Structure):
|
|
|
|
_fields_ = cls._fields_ + [('cmsg_data', Data)]
|
|
|
|
|
|
|
|
datab = Data(*bytearray(cmsg_data))
|
|
|
|
return _flexhdr(cmsg_len=cmsg_len, cmsg_level=cmsg_level,
|
|
|
|
cmsg_type=cmsg_type, cmsg_data=datab)
|
|
|
|
|
|
|
|
|
|
|
|
def CMSG_LEN(length):
|
|
|
|
sizeof_cmshdr = ctypes.sizeof(cmsghdr)
|
|
|
|
return ctypes.c_size_t(CMSG_ALIGN(sizeof_cmshdr).value + length)
|
|
|
|
|
|
|
|
|
|
|
|
SCM_RIGHTS = 1
|
|
|
|
|
2021-02-17 22:04:16 +00:00
|
|
|
|
2021-02-01 14:05:15 +00:00
|
|
|
class msghdr(ctypes.Structure): # from bits/socket.h
|
|
|
|
_fields_ = [('msg_name', ctypes.c_void_p),
|
|
|
|
('msg_namelen', ctypes.c_uint),
|
|
|
|
('msg_iov', ctypes.POINTER(iovec)),
|
|
|
|
('msg_iovlen', ctypes.c_size_t),
|
|
|
|
('msg_control', ctypes.c_void_p),
|
|
|
|
('msg_controllen', ctypes.c_size_t),
|
|
|
|
('msg_flags', ctypes.c_int)]
|
|
|
|
|
|
|
|
|
2021-02-17 22:04:16 +00:00
|
|
|
def CMSG_ALIGN(length): # bits/socket.h
|
|
|
|
ret = (length + ctypes.sizeof(ctypes.c_size_t) - 1
|
|
|
|
& ~(ctypes.sizeof(ctypes.c_size_t) - 1))
|
|
|
|
return ctypes.c_size_t(ret)
|
|
|
|
|
|
|
|
|
|
|
|
def CMSG_SPACE(length): # bits/socket.h
|
|
|
|
ret = CMSG_ALIGN(length).value + CMSG_ALIGN(ctypes.sizeof(cmsghdr)).value
|
|
|
|
return ctypes.c_size_t(ret)
|
|
|
|
|
|
|
|
|
2021-02-18 19:58:45 +00:00
|
|
|
class ClientFile(object):
|
|
|
|
def __init__(self, name, mode, fd):
|
|
|
|
self.fileobject = os.fdopen(fd, mode)
|
|
|
|
self.filename = name
|
|
|
|
|
2021-02-01 14:05:15 +00:00
|
|
|
libc = ctypes.CDLL(ctypes.util.find_library('c'))
|
|
|
|
recvmsg = libc.recvmsg
|
2021-02-17 19:31:14 +00:00
|
|
|
recvmsg.argtypes = [ctypes.c_int, ctypes.POINTER(msghdr), ctypes.c_int]
|
2021-02-26 21:17:43 +00:00
|
|
|
recvmsg.restype = ctypes.c_int
|
2021-02-01 14:05:15 +00:00
|
|
|
sendmsg = libc.sendmsg
|
2021-02-17 19:31:14 +00:00
|
|
|
sendmsg.argtypes = [ctypes.c_int, ctypes.POINTER(msghdr), ctypes.c_int]
|
|
|
|
sendmsg.restype = ctypes.c_size_t
|
|
|
|
|
2016-03-26 14:02:16 +00:00
|
|
|
def decodestr(value):
|
|
|
|
ret = None
|
|
|
|
try:
|
|
|
|
ret = value.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
try:
|
|
|
|
ret = value.decode('cp437')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
ret = value
|
2019-06-27 19:28:59 +00:00
|
|
|
except AttributeError:
|
|
|
|
return value
|
2016-03-26 14:02:16 +00:00
|
|
|
return ret
|
2014-04-18 21:13:50 +00:00
|
|
|
|
2015-03-20 19:39:51 +00:00
|
|
|
def unicode_dictvalues(dictdata):
|
|
|
|
for key in dictdata:
|
2019-10-02 15:29:13 +00:00
|
|
|
if isinstance(dictdata[key], bytes):
|
2016-03-26 14:02:16 +00:00
|
|
|
dictdata[key] = decodestr(dictdata[key])
|
2016-04-20 20:51:01 +00:00
|
|
|
elif isinstance(dictdata[key], datetime):
|
|
|
|
dictdata[key] = dictdata[key].strftime('%Y-%m-%dT%H:%M:%S')
|
2015-03-20 19:39:51 +00:00
|
|
|
elif isinstance(dictdata[key], list):
|
2018-03-15 16:55:32 +00:00
|
|
|
_unicode_list(dictdata[key])
|
2015-03-20 19:39:51 +00:00
|
|
|
elif isinstance(dictdata[key], dict):
|
|
|
|
unicode_dictvalues(dictdata[key])
|
|
|
|
|
|
|
|
|
2018-03-15 16:55:32 +00:00
|
|
|
def _unicode_list(currlist):
|
2019-10-02 15:29:13 +00:00
|
|
|
for i in range(len(currlist)):
|
2018-03-15 16:55:32 +00:00
|
|
|
if isinstance(currlist[i], str):
|
|
|
|
currlist[i] = decodestr(currlist[i])
|
|
|
|
elif isinstance(currlist[i], dict):
|
|
|
|
unicode_dictvalues(currlist[i])
|
|
|
|
elif isinstance(currlist[i], list):
|
|
|
|
_unicode_list(currlist[i])
|
|
|
|
|
|
|
|
|
2021-02-17 19:31:14 +00:00
|
|
|
def send(handle, data, filehandle=None):
|
2018-07-12 18:16:44 +00:00
|
|
|
if isinstance(data, unicode):
|
2018-06-21 18:29:54 +00:00
|
|
|
try:
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2019-10-02 15:29:13 +00:00
|
|
|
if isinstance(data, bytes) or isinstance(data, unicode):
|
2014-02-09 15:43:26 +00:00
|
|
|
# plain text, e.g. console data
|
2013-10-13 01:19:15 +00:00
|
|
|
tl = len(data)
|
2015-10-21 14:30:41 +00:00
|
|
|
if tl == 0:
|
|
|
|
# if you don't have anything to say, don't say anything at all
|
|
|
|
return
|
2013-10-13 01:19:15 +00:00
|
|
|
if tl < 16777216:
|
2015-03-20 19:39:51 +00:00
|
|
|
# type for string is '0', so we don't need
|
|
|
|
# to xor anything in
|
2013-11-02 17:05:58 +00:00
|
|
|
handle.sendall(struct.pack("!I", tl))
|
2013-10-13 01:19:15 +00:00
|
|
|
else:
|
|
|
|
raise Exception("String data length exceeds protocol")
|
2013-11-02 17:05:58 +00:00
|
|
|
handle.sendall(data)
|
2013-10-13 01:19:15 +00:00
|
|
|
elif isinstance(data, dict): # JSON currently only goes to 4 bytes
|
2014-02-09 15:43:26 +00:00
|
|
|
# Some structured message, like what would be seen in http responses
|
2015-03-20 19:39:51 +00:00
|
|
|
unicode_dictvalues(data) # make everything unicode, assuming UTF-8
|
2015-01-19 19:45:24 +00:00
|
|
|
sdata = json.dumps(data, ensure_ascii=False, separators=(',', ':'))
|
2015-03-20 19:39:51 +00:00
|
|
|
sdata = sdata.encode('utf-8')
|
2013-10-13 01:19:15 +00:00
|
|
|
tl = len(sdata)
|
|
|
|
if tl > 16777215:
|
|
|
|
raise Exception("JSON data exceeds protocol limits")
|
|
|
|
# xor in the type (0b1 << 24)
|
2021-02-17 19:31:14 +00:00
|
|
|
if filehandle is None:
|
2021-02-01 14:05:15 +00:00
|
|
|
tl |= 16777216
|
|
|
|
handle.sendall(struct.pack("!I", tl))
|
|
|
|
handle.sendall(sdata)
|
|
|
|
else:
|
|
|
|
tl |= (2 << 24)
|
|
|
|
handle.sendall(struct.pack("!I", tl))
|
2021-02-26 21:17:43 +00:00
|
|
|
cdtype = ctypes.c_ubyte * len(sdata)
|
|
|
|
cdata = cdtype.from_buffer(bytearray(sdata))
|
|
|
|
ciov = iovec(iov_base=ctypes.addressof(cdata),
|
|
|
|
iov_len=ctypes.c_size_t(ctypes.sizeof(cdata)))
|
|
|
|
fd = ctypes.c_int(filehandle)
|
|
|
|
cmh = cmsghdr.init_data(
|
|
|
|
cmsg_len=CMSG_LEN(
|
|
|
|
ctypes.sizeof(fd)), cmsg_level=socket.SOL_SOCKET,
|
|
|
|
cmsg_type=SCM_RIGHTS, cmsg_data=fd)
|
|
|
|
mh = msghdr(msg_name=None, msg_len=0, msg_iov=iovec_ptr(ciov),
|
|
|
|
msg_iovlen=1, msg_control=ctypes.addressof(cmh),
|
|
|
|
msg_controllen=ctypes.c_size_t(ctypes.sizeof(cmh)))
|
|
|
|
sendmsg(handle.fileno(), mh, 0)
|
2021-02-01 14:05:15 +00:00
|
|
|
|
2013-10-13 01:19:15 +00:00
|
|
|
|
2018-06-22 18:41:41 +00:00
|
|
|
def recvall(handle, size):
|
|
|
|
rd = handle.recv(size)
|
|
|
|
while len(rd) < size:
|
|
|
|
nd = handle.recv(size - len(rd))
|
|
|
|
if not nd:
|
|
|
|
raise Exception("Error reading data")
|
|
|
|
rd += nd
|
|
|
|
return rd
|
2014-04-18 21:13:50 +00:00
|
|
|
|
2014-03-04 22:12:19 +00:00
|
|
|
def recv(handle):
|
2013-11-02 17:05:58 +00:00
|
|
|
tl = handle.recv(4)
|
2014-05-19 20:52:01 +00:00
|
|
|
if not tl:
|
|
|
|
return None
|
2014-05-19 14:12:16 +00:00
|
|
|
while len(tl) < 4:
|
2014-05-19 20:52:01 +00:00
|
|
|
ndata = handle.recv(4 - len(tl))
|
|
|
|
if not ndata:
|
|
|
|
raise Exception("Error reading data")
|
|
|
|
tl += ndata
|
2014-03-03 21:03:10 +00:00
|
|
|
if len(tl) == 0:
|
|
|
|
return None
|
2014-02-09 15:43:26 +00:00
|
|
|
tl = struct.unpack("!I", tl)[0]
|
|
|
|
if tl & 0b10000000000000000000000000000000:
|
2013-10-13 01:19:15 +00:00
|
|
|
raise Exception("Protocol Violation, reserved bit set")
|
|
|
|
# 4 byte tlv
|
2014-02-09 15:43:26 +00:00
|
|
|
dlen = tl & 16777215 # grab lower 24 bits
|
2014-04-18 21:13:50 +00:00
|
|
|
datatype = (tl & 2130706432) >> 24 # grab 7 bits from near beginning
|
2015-10-21 14:22:49 +00:00
|
|
|
if dlen == 0:
|
|
|
|
return None
|
2021-02-17 22:04:16 +00:00
|
|
|
if datatype == tlv.Types.filehandle:
|
|
|
|
filehandles = array.array('i')
|
2021-02-26 21:17:43 +00:00
|
|
|
rawbuffer = bytearray(2048)
|
|
|
|
pkttype = ctypes.c_ubyte * 2048
|
|
|
|
data = pkttype.from_buffer(rawbuffer)
|
|
|
|
cmsgsize = CMSG_SPACE(ctypes.sizeof(ctypes.c_int)).value
|
|
|
|
cmsgarr = bytearray(cmsgsize)
|
|
|
|
cmtype = ctypes.c_ubyte * cmsgsize
|
|
|
|
cmsg = cmtype.from_buffer(cmsgarr)
|
|
|
|
cmsg.cmsg_level = socket.SOL_SOCKET
|
|
|
|
cmsg.cmsg_type = SCM_RIGHTS
|
|
|
|
cmsg.cmsg_len = CMSG_LEN(ctypes.sizeof(ctypes.c_int))
|
|
|
|
iov = iovec()
|
|
|
|
iov.iov_base = ctypes.addressof(data)
|
|
|
|
iov.iov_len = 2048
|
|
|
|
msg = msghdr()
|
|
|
|
msg.msg_iov = ctypes.pointer(iov)
|
|
|
|
msg.msg_iovlen = 1
|
|
|
|
msg.msg_control = ctypes.addressof(cmsg)
|
|
|
|
msg.msg_controllen = ctypes.sizeof(cmsg)
|
|
|
|
select.select([handle], [], [])
|
|
|
|
i = recvmsg(handle.fileno(), ctypes.pointer(msg), 0)
|
|
|
|
cdata = cmsgarr[CMSG_LEN(0).value:]
|
|
|
|
data = rawbuffer[:i]
|
|
|
|
if cmsg.cmsg_level == socket.SOL_SOCKET and cmsg.cmsg_type == SCM_RIGHTS:
|
2022-06-06 15:37:39 +00:00
|
|
|
try:
|
|
|
|
filehandles.fromstring(bytes(
|
|
|
|
cdata[:len(cdata) - len(cdata) % filehandles.itemsize]))
|
|
|
|
except AttributeError:
|
|
|
|
filehandles.frombytes(bytes(
|
|
|
|
cdata[:len(cdata) - len(cdata) % filehandles.itemsize]))
|
2021-02-26 21:17:43 +00:00
|
|
|
data = json.loads(bytes(data))
|
2021-02-18 19:58:45 +00:00
|
|
|
return ClientFile(data['filename'], data['mode'], filehandles[0])
|
2021-02-17 22:04:16 +00:00
|
|
|
else:
|
|
|
|
data = handle.recv(dlen)
|
|
|
|
while len(data) < dlen:
|
|
|
|
ndata = handle.recv(dlen - len(data))
|
|
|
|
if not ndata:
|
|
|
|
raise Exception("Error reading data")
|
|
|
|
data += ndata
|
2014-04-18 21:13:50 +00:00
|
|
|
if datatype == tlv.Types.text:
|
2014-02-09 15:43:26 +00:00
|
|
|
return data
|
2014-04-18 21:13:50 +00:00
|
|
|
elif datatype == tlv.Types.json:
|
2014-02-09 15:43:26 +00:00
|
|
|
return json.loads(data)
|