def kXR_attn_asynresp(self, streamid=None, status=None, dlen=None, actnum=None, reserved=None, rstreamid=None, rstatus=None, rlen=None, rdata=None): """Return a packed representation of a kXR_attn_asynresp response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asynresp') if not rdata: rdata = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(rdata) + 16, 'actnum' : actnum if actnum else get_attncode('kXR_asynresp'), 'reserved': reserved if reserved else (4 * '\0'), 'rsid' : rstreamid if rstreamid else 0, 'rstatus' : rstatus if rstatus else get_responseid('kXR_ok'), 'rlen' : rlen if rlen else len(rdata), 'rdata' : rdata} return self.mh.build_message(response_struct, params)
def kXR_read(self, streamid=None, requestid=None, fhandle=None, offset=None, rlen=None, dlen=None, pathid=None, reserved=None, readahead=False, fhandle2=None, rlen2=None, roffset2=None): """Return a packed representation of a kXR_read request. Pass readahead=True to enable pre-read.""" read_args = get_struct('read_args') params = \ {'pathid' : pathid if pathid else '', 'reserved' : reserved if reserved else (7 * '\0')} read_args = self.mh.build_message(read_args, params) if readahead: readahead_list = get_struct('readahead_list') params = \ {'fhandle2': fhandle2 if fhandle2 else (4 * '\0'), 'rlen2' : rlen2 if rlen2 else 0, 'roffset2': roffset2 if roffset2 else 0} readahead_list = self.mh.build_message(readahead_list, params) else: readahead_list = '' request_struct = get_struct('ClientRequestHdr') + get_struct('ClientReadRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_read'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'offset' : offset if offset else 0, 'rlen' : rlen if rlen else 0, 'dlen' : dlen if dlen else len(read_args + readahead_list)} return self.mh.build_message(request_struct, params) \ + read_args + readahead_list
def kXR_login(self, streamid=None, status=None, dlen=None, sessid=None, sec=None, verify_auth=False): """Return a packed representation of a kXR_login response. Pass verify_auth=True to enable authentication.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Login') # Check if client needs to authenticate if verify_auth and not sec: auth = AuthHelper.AuthHelper(self.context) sec = auth.getsectoken() else: sec = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else len(sec) + 16, 'sessid' : sessid if sessid else gen_sessid(), 'sec' : sec} return self.mh.build_message(response_struct, params)
def kXR_login(self, streamid=None, requestid=None, pid=None, username=None, reserved=None, zone=None, capver=None, role=None, dlen=None): """Return a packed representation of a kXR_login request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientLoginRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_login'), 'pid' : pid if pid else os.getpid(), 'username' : username if username else ''.ljust(8, "\0"), 'reserved' : reserved if reserved else '\0', 'zone' : zone if zone else '\0', 'capver' : capver if capver else chr(XProtocol.XLoginCapVer .kXR_asyncap \ | XProtocol.XLoginVersion .kXR_ver003), 'role' : role if role else '0', 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_open(self, streamid=None, status=None, dlen=None, fhandle=None, cpsize=None, cptype=None, data=None): """Return a packed representation of a kXR_open response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Open') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : 0, 'fhandle' : fhandle if fhandle else (4 * '\0'), 'cpsize' : cpsize if cpsize else 0, 'cptype' : cptype if cptype else (4 * '\0'), 'data' : data if data else ''} if not cpsize: del response_struct[4] del params['cpsize'] if not cptype: del response_struct[4] del params['cptype'] if not data: del response_struct[4] del params['data'] if not dlen: dlen = 4 + (len(cpsize) if cpsize else 0) \ + (len(cptype) if cptype else 0) \ + (len(data) if data else 0) params['dlen'] = dlen return self.mh.build_message(response_struct, params)
def kXR_readv(self, streamid=None, requestid=None, reserved=None, pathid=None, dlen=None, **read_lists): """Return a packed representation of a kXR_readv request. You can pass in multiple read_lists with arbitrary keyword names, but they must have the format (fhandle, rlen, offset).""" all_read_lists = '' for read_list in read_lists.itervalues(): read_struct = get_struct('ClientRequestHdr') + get_struct( 'readahead_list') params = \ {'fhandle2': read_list[0] if read_list[0] else (4 * '\0'), 'rlen2' : read_list[1] if read_list[1] else 0, 'roffset2': read_list[2] if read_list[2] else 0} all_read_lists += self.mh.build_message(read_struct, params) request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientReadVRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_readv'), 'reserved' : reserved if reserved else (15 * "\0"), 'pathid' : pathid if pathid else '0', 'dlen' : dlen if dlen else len(all_read_lists)} return self.mh.build_message(request_struct, params) + all_read_lists
def kXR_verifyw(self, streamid=None, requestid=None, fhandle=None, offset=None, pathid=None, vertype=None, reserved=None, dlen=None, data=None): """Return a packed representation of a kXR_verifyw request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientVerifywRequest') if not data: data = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_verifyw'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'offset' : offset if offset else 0, 'pathid' : pathid if pathid else '\0', 'vertype' : vertype if vertype else '\0', 'reserved' : reserved if reserved else (2 * "\0"), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(request_struct, params)
def build_request(self, authtoken=None, contcred=None, streamid=None, requestid=None, reserved=None, credtype=None, dlen=None, cred=None): """Return a packed kXR_auth request.""" if not authtoken and not contcred: print "[!] Can't build kXR_auth request: no auth token or continuation \ credentials supplied" sys.exit(1) credname, credentials, credlen = \ self.getcredentials(authtoken, contcred, self.context['socket'].fileno()) request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientAuthRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else XProtocol.XRequestTypes.kXR_auth, 'reserved' : reserved if reserved else 12 * '\0', 'credtype' : credtype if credtype else credname.ljust(4, '\0'), 'dlen' : dlen if dlen else credlen, 'cred' : cred if cred else credentials} return self.mh.build_message(request_struct, params)
def kXR_endsess(self, streamid=None, requestid=None, sessid=None, dlen=None): """Return a packed representation of a kXR_endsess request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientEndsessRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_endsess'), 'sessid' : sessid if sessid else (16 * '\0'), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_ping(self, streamid=None, requestid=None, reserved=None, dlen=None): """Return a packed representation of a kXR_ping request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientPingRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_ping'), 'reserved' : reserved if reserved else (16 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_bind(self, streamid=None, status=None, dlen=None, pathid=None): """Return a packed representation of a kXR_bind response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Bind') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else 1, 'pathid' : pathid if pathid else '\0'} return self.mh.build_message(response_struct, params)
def kXR_waitresp(self, streamid=None, status=None, dlen=None, seconds=None): """Return a packed representation of a kXR_waitresp response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Waitresp') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_waitresp'), 'dlen' : dlen if dlen else 4, 'seconds' : seconds if seconds else 0} return self.mh.build_message(response_struct, params)
def kXR_bind(self, streamid=None, requestid=None, sessid=None, dlen=None): """Return a packed representation of a kXR_bind request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientBindRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_bind'), 'sessid' : sessid if sessid else gen_sessid(), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_sync(self, streamid=None, requestid=None, fhandle=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_sync request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientSyncRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_sync'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'reserved' : reserved if reserved else (12 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_ok(self, streamid=None, status=None, dlen=None, data=None): """Return a packed representation of a kXR_ok response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Buffer') if not data: data = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(response_struct, params)
def kXR_protocol(self, streamid=None, requestid=None, clientpv=None, reserved=None, dlen=None): """Return a packed representation of a kXR_protocol request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientProtocolRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_protocol'), 'clientpv' : clientpv if clientpv else XProtocol.XLoginVersion .kXR_ver003, 'reserved' : reserved if reserved else (12 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_set(self, streamid=None, requestid=None, reserved=None, dlen=None, data=None): """Return a packed representation of a kXR_set request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientSetRequest') if not data: data = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_set'), 'reserved' : reserved if reserved else (16 * '\0'), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(request_struct, params)
def unpack_request(self, request_raw): """Return an unpacked named tuple representation of a client request.""" if not len(request_raw): return # Unpack the header to find the request ID requestid = self.unpack('>HH' + (str(len(request_raw) - 4) + 's'), request_raw)[1] # Check if this is a handshake request if requestid == XProtocol.XRequestTypes.handshake: request_struct = get_struct('ClientInitHandShake') else: request_type = XProtocol.XRequestTypes.reverse_mapping[requestid] request_struct = get_struct('ClientRequestHdr') request_struct += get_struct('Client' + request_type[4:].title() + 'Request') if requestid == XProtocol.XRequestTypes.kXR_read: request_struct += get_struct('read_args') # Check if another request is being piggybacked. pending_req = None if len(request_raw) > format_length(struct_format(request_struct)): pending_req = request_raw[ format_length(struct_format(request_struct)):] request_raw = request_raw[:format_length( struct_format(request_struct))] # Build the complete format string format = '>' for member in request_struct: if member.has_key('size'): if member['size'] == 'dlen': format += str(len(request_raw) - format_length(format)) \ + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] # Unpack to a regular tuple request_tuple = self.unpack(format, request_raw) # Convert to named tuple request_struct.insert(0, {'name': 'type'}) if requestid == XProtocol.XRequestTypes.handshake: type = 'handshake' else: type = get_requestid(requestid) request = namedtuple('request', ' '.join([m['name'] for m in request_struct])) return request(type, *request_tuple), pending_req
def handshake(self, streamid=None, status=None, dlen=None, protover=None, msgval=None): """Return a packed representation of a server handshake response.""" response_struct = get_struct('ServerResponseHeader') \ + get_struct('ServerInitHandShake') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else 0, 'dlen' : dlen if dlen else 8, 'protover': protover if protover else 663, 'msgval' : msgval if msgval else 1} return self.mh.build_message(response_struct, params)
def kXR_protocol(self, streamid=None, status=None, dlen=None, pval=None, flags=None): """Return a packed representation of a kXR_protocol response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Protocol') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else 8, 'pval' : pval if pval else XProtocol.kXR_PROTOCOLVERSION, 'flags' : flags if flags else XProtocol.kXR_isServer} return self.mh.build_message(response_struct, params)
def kXR_rm(self, streamid=None, requestid=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_rm request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientRmRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_rm'), 'reserved' : reserved if reserved else (16 * '\0'), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_close(self, streamid=None, requestid=None, fhandle=None, fsize=None, reserved=None, dlen=None): """Return a packed representation of a kXR_close request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientCloseRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_close'), 'fhandle' : fhandle if fhandle else (4 * '\0'), 'fsize' : fsize if fsize else 0, 'reserved' : reserved if reserved else (4 * '\0'), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_attn_asyncwt(self, streamid=None, status=None, dlen=None, actnum=None, wsec=None): """Return a packed representation of a kXR_attn_asyncwt response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncwt') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else 8, 'actnum' : actnum if actnum else get_attncode('kXR_asyncwt'), 'wsec' : wsec if wsec else 0} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncab(self, streamid=None, status=None, dlen=None, actnum=None, msg=None): """Return a packed representation of a kXR_attn_asyncab response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn') if not msg: msg = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(msg) + 4, 'actnum' : actnum if actnum else get_attncode('kXR_asyncab'), 'parms' : msg} return self.mh.build_message(response_struct, params)
def unpack_request(self, request_raw): """Return an unpacked named tuple representation of a client request.""" if not len(request_raw): return # Unpack the header to find the request ID requestid = self.unpack('>HH' + (str(len(request_raw) - 4) + 's'), request_raw)[1] # Check if this is a handshake request if requestid == XProtocol.XRequestTypes.handshake: request_struct = get_struct('ClientInitHandShake') else: request_type = XProtocol.XRequestTypes.reverse_mapping[requestid] request_struct = get_struct('ClientRequestHdr') request_struct += get_struct('Client' + request_type[4:].title() + 'Request') if requestid == XProtocol.XRequestTypes.kXR_read: request_struct += get_struct('read_args') # Check if another request is being piggybacked. pending_req = None if len(request_raw) > format_length(struct_format(request_struct)): pending_req = request_raw[format_length(struct_format(request_struct)):] request_raw = request_raw[:format_length(struct_format(request_struct))] # Build the complete format string format = '>' for member in request_struct: if member.has_key('size'): if member['size'] == 'dlen': format += str(len(request_raw) - format_length(format)) \ + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] # Unpack to a regular tuple request_tuple = self.unpack(format, request_raw) # Convert to named tuple request_struct.insert(0, {'name': 'type'}) if requestid == XProtocol.XRequestTypes.handshake: type = 'handshake' else: type = get_requestid(requestid) request = namedtuple('request', ' '.join([m['name'] for m in request_struct])) return request(type, *request_tuple), pending_req
def kXR_redirect(self, streamid=None, status=None, dlen=None, port=None, host=None, opaque=None, token=None): """Return a packed representation of a kXR_redirect response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Redirect') if not host: host = '' else: host += (opaque if opaque else '') + (token if token else '') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_redirect'), 'dlen' : dlen if dlen else len(host) + 4, 'port' : port if port else 0, 'host' : host if host else r''} return self.mh.build_message(response_struct, params)
def kXR_error(self, streamid=None, status=None, dlen=None, errnum=None, errmsg=None): """Return a packed representation of a kXR_error response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Error') if not errmsg: errmsg = '' if not errnum: errnum = XProtocol.XErrorCode.kXR_ArgInvalid params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_error'), 'dlen' : dlen if dlen else len(errmsg + str(errnum)), 'errnum' : errnum, 'errmsg' : errmsg} return self.mh.build_message(response_struct, params)
def kXR_truncate(self, streamid=None, requestid=None, fhandle=None, size=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_truncate request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientTruncateRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_truncate'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'size' : size if size else 0, 'reserved' : reserved if reserved else (4 * "\0"), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_ping(self, streamid=None, requestid=None, reserved=None, dlen=None): """Return a packed representation of a kXR_ping request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientPingRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_ping'), 'reserved' : reserved if reserved else (16 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_open(self, streamid=None, requestid=None, mode=None, options=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_open request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientOpenRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_open'), 'mode' : mode if mode else 0, 'options' : options if options else 0, 'reserved' : reserved if reserved else (12 * '\0'), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_stat(self, streamid=None, requestid=None, options=None, reserved=None, fhandle=None, dlen=None, path=None): """Return a packed representation of a kXR_stat request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientStatRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_stat'), 'options' : options if options else '\0', 'reserved' : reserved if reserved else (11 * "\0"), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_attn_asyncrd(self, streamid=None, status=None, dlen=None, actnum=None, port=None, host=None, token=None): """Return a packed representation of a kXR_attn_asyncrd response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncrd') if not host: host = '' else: host += (token if token else '') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(host), 'actnum' : actnum if actnum else get_attncode('kXR_asyncrd'), 'port' : port if port else 0, 'host' : host} return self.mh.build_message(response_struct, params)
def kXR_prepare(self, streamid=None, requestid=None, options=None, prty=None, port=None, reserved=None, dlen=None, plist=None): """Return a packed representation of a kXR_prepare request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientPrepareRequest') if not plist: plist = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_prepare'), 'options' : options if options else '\0', 'prty' : prty if prty else '\0', 'port' : port if port else 0, 'reserved' : reserved if reserved else (12 * "\0"), 'dlen' : dlen if dlen else len(plist), 'plist' : plist} return self.mh.build_message(request_struct, params)
def kXR_query(self, streamid=None, requestid=None, reqcode=None, reserved1=None, fhandle=None, reserved2=None, dlen=None, args=None): """Return a packed representation of a kXR_query request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientQueryRequest') if not args: args = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_query'), 'reqcode' : reqcode if reqcode else 0, 'reserved1' : reserved1 if reserved1 else (2 * "\0"), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'reserved2' : reserved2 if reserved2 else (8 * "\0"), 'dlen' : dlen if dlen else len(args), 'args' : args} return self.mh.build_message(request_struct, params)
def kXR_set(self, streamid=None, requestid=None, reserved=None, dlen=None, data=None): """Return a packed representation of a kXR_set request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientSetRequest') if not data: data = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_set'), 'reserved' : reserved if reserved else (16 * '\0'), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(request_struct, params)
def kXR_sync(self, streamid=None, requestid=None, fhandle=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_sync request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientSyncRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_sync'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'reserved' : reserved if reserved else (12 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_rm(self, streamid=None, requestid=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_rm request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientRmRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_rm'), 'reserved' : reserved if reserved else (16 * '\0'), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_verifyw(self, streamid=None, requestid=None, fhandle=None, offset=None, pathid=None, vertype=None, reserved=None, dlen=None, data=None): """Return a packed representation of a kXR_verifyw request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientVerifywRequest') if not data: data = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_verifyw'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'offset' : offset if offset else 0, 'pathid' : pathid if pathid else '\0', 'vertype' : vertype if vertype else '\0', 'reserved' : reserved if reserved else (2 * "\0"), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(request_struct, params)
def kXR_protocol(self, streamid=None, requestid=None, clientpv=None, reserved=None, dlen=None): """Return a packed representation of a kXR_protocol request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientProtocolRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_protocol'), 'clientpv' : clientpv if clientpv else XProtocol.XLoginVersion .kXR_ver003, 'reserved' : reserved if reserved else (12 * "\0"), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_close(self, streamid=None, requestid=None, fhandle=None, fsize=None, reserved=None, dlen=None): """Return a packed representation of a kXR_close request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientCloseRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_close'), 'fhandle' : fhandle if fhandle else (4 * '\0'), 'fsize' : fsize if fsize else 0, 'reserved' : reserved if reserved else (4 * '\0'), 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_login(self, streamid=None, requestid=None, pid=None, username=None, reserved=None, zone=None, capver=None, role=None, dlen=None): """Return a packed representation of a kXR_login request.""" request_struct = get_struct('ClientRequestHdr') + get_struct('ClientLoginRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_login'), 'pid' : pid if pid else os.getpid(), 'username' : username if username else ''.ljust(8, "\0"), 'reserved' : reserved if reserved else '\0', 'zone' : zone if zone else '\0', 'capver' : capver if capver else chr(XProtocol.XLoginCapVer .kXR_asyncap \ | XProtocol.XLoginVersion .kXR_ver003), 'role' : role if role else '0', 'dlen' : dlen if dlen else 0} return self.mh.build_message(request_struct, params)
def kXR_read(self, streamid=None, requestid=None, fhandle=None, offset=None, rlen=None, dlen=None, pathid=None, reserved=None, readahead=False, fhandle2=None, rlen2=None, roffset2=None): """Return a packed representation of a kXR_read request. Pass readahead=True to enable pre-read.""" read_args = get_struct('read_args') params = \ {'pathid' : pathid if pathid else '', 'reserved' : reserved if reserved else (7 * '\0')} read_args = self.mh.build_message(read_args, params) if readahead: readahead_list = get_struct('readahead_list') params = \ {'fhandle2': fhandle2 if fhandle2 else (4 * '\0'), 'rlen2' : rlen2 if rlen2 else 0, 'roffset2': roffset2 if roffset2 else 0} readahead_list = self.mh.build_message(readahead_list, params) else: readahead_list = '' request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientReadRequest') params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_read'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'offset' : offset if offset else 0, 'rlen' : rlen if rlen else 0, 'dlen' : dlen if dlen else len(read_args + readahead_list)} return self.mh.build_message(request_struct, params) \ + read_args + readahead_list
def handshake(self, first=None, second=None, third=None, fourth=None, fifth=None): """Return a packed representation of a client handshake request.""" request_struct = get_struct('ClientInitHandShake') params = {'first' : first if first else 0, 'second' : second if second else 0, 'third' : third if third else 0, 'fourth' : fourth if fourth else 4, 'fifth' : fifth if fifth else 2012} return self.mh.build_message(request_struct, params)
def build_response(self, cred=None, streamid=None, status=None, dlen=None): """Return a packed kXR_auth response.""" if cred: self.auth(cred) response_struct = get_struct('ServerResponseHeader') params = \ {'streamid' : streamid if streamid else 0, 'status' : status if status else XProtocol.XResponseType.kXR_ok, 'dlen' : dlen if dlen else 0} return self.mh.build_message(response_struct, params)
def kXR_open(self, streamid=None, requestid=None, mode=None, options=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_open request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientOpenRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_open'), 'mode' : mode if mode else 0, 'options' : options if options else 0, 'reserved' : reserved if reserved else (12 * '\0'), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_stat(self, streamid=None, requestid=None, options=None, reserved=None, fhandle=None, dlen=None, path=None): """Return a packed representation of a kXR_stat request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientStatRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_stat'), 'options' : options if options else '\0', 'reserved' : reserved if reserved else (11 * "\0"), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)
def kXR_truncate(self, streamid=None, requestid=None, fhandle=None, size=None, reserved=None, dlen=None, path=None): """Return a packed representation of a kXR_truncate request.""" request_struct = get_struct('ClientRequestHdr') + get_struct( 'ClientTruncateRequest') if not path: path = '' params = \ {'streamid' : streamid if streamid else self.context['streamid'], 'requestid' : requestid if requestid else get_requestid('kXR_truncate'), 'fhandle' : fhandle if fhandle else (4 * "\0"), 'size' : size if size else 0, 'reserved' : reserved if reserved else (4 * "\0"), 'dlen' : dlen if dlen else len(path), 'path' : path} return self.mh.build_message(request_struct, params)