From 7f91994a99cb7d23a22d949454e861e879a68e98 Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 26 Aug 2013 09:48:04 +0800 Subject: ptp v0.4 and std-compliant server --- server/piztor/prob.py | 25 +++++++++++++++++++++---- server/piztor/ptp.rst | 34 +++++++++++++++++++++------------- server/piztor/server.py | 33 +++++++++++++++------------------ 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/server/piztor/prob.py b/server/piztor/prob.py index 57ce2fe..f18e42d 100644 --- a/server/piztor/prob.py +++ b/server/piztor/prob.py @@ -78,12 +78,29 @@ print "status: " + str(status) resp = send(gen_request_location(token, username, gid)) print len(resp) -pl, optcode, status, length = unpack("!LBBL", resp[:10]) +pl, optcode, status = unpack("!LBB", resp[:6]) print "size: " + str((pl, len(resp))) -idx = 10 -print "length: " + str(len(resp[10:])) -for i in xrange(length): +idx = 6 +print "length: " + str(len(resp[6:])) +while idx < pl: print len(resp[idx:idx + 20]) uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20]) idx += 20 print (uid, lat, lng) + +resp = send(gen_request_user_info(token, username, uid)) +pl, optcode, status = unpack("!LBB", resp[:6]) +print "size: " + str((pl, len(resp))) + +idx = 6 +while idx < pl: + info_key, = unpack("!B", resp[idx:idx + 1]) + idx += 1 + print info_key + if info_key == 0x00: + info_value, = unpack("!L", resp[idx:idx + 4]) + idx += 4 + elif info_key == 0x01: + info_value, = unpack("!B", resp[idx:idx + 1]) + idx += 1 + print (info_key, info_value) diff --git a/server/piztor/ptp.rst b/server/piztor/ptp.rst index 9fc5ff4..cc0d30c 100644 --- a/server/piztor/ptp.rst +++ b/server/piztor/ptp.rst @@ -1,4 +1,4 @@ -Piztor Transmission Protocol v0.3a +Piztor Transmission Protocol v0.4 ---------------------------------- - General @@ -23,13 +23,21 @@ Piztor Transmission Protocol v0.3a - In following sections, ``LENGTH`` part is left out for clarity. - ``PADDING`` has value ``0``. + - ``string`` type structure: + + :: + + +-------?b-------+---------+ + | STRING_CONTENT | PADDING | + +----------------+---------+ + - ``AUTH_HEAD`` structure: :: - +----32b-----+----?b----+----1b---+ - | USER_TOKEN | USERNAME | PADDING | - +----raw-----+----------+---------+ + +----32b-----+----?b----+ + | USER_TOKEN | USERNAME | + +----raw-----+--string--+ - Authentication @@ -37,9 +45,9 @@ Piztor Transmission Protocol v0.3a :: - +--1b---+-----?b------+----1b----+-----?b-----+ - | 0x00 | USERNAME | PADDING | PASSWORD | - +-uchar-+-------------+----------+------------+ + +--1b---+-----?b---+----?b----+ + | 0x00 | USERNAME | PASSWORD | + +-uchar-+--string--+--string--+ - Response @@ -91,9 +99,9 @@ Piztor Transmission Protocol v0.3a :: - +--1b---+---1b---+-----4b----+------20b-------+-----+ - | 0x02 | STATUS | ENTRY_CNT | LOCATION_ENTRY | ... | - +-uchar-+-uchar--+----int----+----------------+-----+ + +--1b---+---1b---+------20b-------+-----+ + | 0x02 | STATUS | LOCATION_ENTRY | ... | + +-uchar-+-uchar--+----------------+-----+ ``LOCATION_ENTRY`` : @@ -125,9 +133,9 @@ Piztor Transmission Protocol v0.3a :: - +----1b----+-----?b-----+---1b----+ - | INFO_KEY | INFO_VALUE | PADDING | - +--uchar---+------------+---------+ + +----1b----+-----?b-----+ + | INFO_KEY | INFO_VALUE | + +--uchar---+------------+ ``INFO_KEY`` : diff --git a/server/piztor/server.py b/server/piztor/server.py index 89c7760..511d51b 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -216,7 +216,6 @@ class LocationRequestHandler(RequestHandler): return _SectionSize.LENGTH + \ _SectionSize.OPT_ID + \ _SectionSize.STATUS + \ - _SectionSize.ENTRY_CNT + \ _SectionSize.LOCATION_ENTRY * item_num def handle(self, tr_data): @@ -240,19 +239,17 @@ class LocationRequestHandler(RequestHandler): # Auth failure if uauth is None: logger.warning("Authentication failure") - return struct.pack("!LBBL", LocationRequestHandler \ + return struct.pack("!LBB", LocationRequestHandler \ ._location_request_response_size(0), _OptCode.location_request, - _StatusCode.failure, - 0) + _StatusCode.failure) ulist = session.query(UserModel).filter(UserModel.gid == gid).all() reply = struct.pack( - "!LBBL", + "!LBB", LocationRequestHandler._location_request_response_size(len(ulist)), _OptCode.location_request, - _StatusCode.sucess, - len(ulist)) + _StatusCode.sucess) for user in ulist: loc = user.location @@ -260,6 +257,13 @@ class LocationRequestHandler(RequestHandler): return reply +def pack_int(val): + return struct.pack("!L", val) + +def pack_bool(val): + return struct.pack("!B", 0x01 if val else 0x00) + + class UserInfoRequestHandler(RequestHandler): _failed_response_size = \ @@ -272,24 +276,16 @@ class UserInfoRequestHandler(RequestHandler): _OptCode.user_info_request, _StatusCode.failure) - @classmethod - def pack_int(cls, val): - return struct.pack("!L", val) - - @classmethod - def pack_bool(cls, val): - return struct.pack("!B", 0x01 if val else 0x00) _code_map = {0x00 : ('gid', pack_int), 0x01 : ('sex', pack_bool)} @classmethod def pack_entry(cls, user, entry_code): - attr, pack_method = _code_map(entry_code) + attr, pack_method = cls._code_map[entry_code] info_key = entry_code info_value = getattr(user, attr) - return struct.pack("!B", info_key) + pack_method(info_value) + \ - struct.pack("!B", 0x00) + return struct.pack("!B", info_key) + pack_method(info_value) def handle(self, tr_data): logger.info("Reading user info request data...") @@ -328,8 +324,9 @@ class UserInfoRequestHandler(RequestHandler): except MultipleResultsFound: raise DBCorruptedError() - for code in _code_map: + for code in UserInfoRequestHandler._code_map: reply += UserInfoRequestHandler.pack_entry(quser, code) + reply = struct.pack("!L", len(reply) + _SectionSize.LENGTH) + reply return reply -- cgit v1.2.3-70-g09d2 From 595d0a7826e2fdb9ee859f9cde90cfef844b0342 Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 26 Aug 2013 10:22:24 +0800 Subject: fixed a bug in server --- server/piztor/prob.py | 3 ++- server/piztor/server.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/server/piztor/prob.py b/server/piztor/prob.py index f18e42d..0ed328f 100644 --- a/server/piztor/prob.py +++ b/server/piztor/prob.py @@ -9,11 +9,12 @@ host = "localhost" port = 9990 def gen_auth(username, password): - length = 4 + 1 + len(username) + 1 + len(password) + length = 4 + 1 + len(username) + 1 + len(password) + 1 data = pack("!LB", length, 0x00) data += username data += "\0" data += password + data += "\0" return data def gen_update_location(token, username, lat, lng): diff --git a/server/piztor/server.py b/server/piztor/server.py index 511d51b..725fc5b 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -111,7 +111,7 @@ class UserAuthHandler(RequestHandler): raise BadReqError("Authentication: Malformed request body") username = tr_data[0:pos] - password = tr_data[pos + 1:] + password = tr_data[pos + 1:-1] logger.info("Trying to login with " \ "(username = {0}, password = {1})" \ .format(username, password)) @@ -136,7 +136,7 @@ class UserAuthHandler(RequestHandler): if uauth is None: raise DBCorruptedError() if not uauth.check_password(password): - logger.info("Incorrect password: {0}".format(username)) + logger.info("Incorrect password: {0}".format(password)) return struct.pack("!LBBL32s", UserAuthHandler \ ._user_auth_response_size, _OptCode.user_auth, -- cgit v1.2.3-70-g09d2