summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorGoblin911 <[email protected]>2013-08-26 11:18:45 +0800
committerGoblin911 <[email protected]>2013-08-26 11:18:45 +0800
commit70dda8318c79e8c92659ab6be8750efd31b6c6b1 (patch)
treed27079a2d75b7402cbaff17b0f7839500591b4d7 /server
parent686cf083c79fce3a032d229bb1b7ea8ed49046c0 (diff)
parent595d0a7826e2fdb9ee859f9cde90cfef844b0342 (diff)
Merge branch 'master' of ssh://github.com/Determinant/piztor
Diffstat (limited to 'server')
-rw-r--r--server/piztor/prob.py28
-rw-r--r--server/piztor/ptp.rst34
-rw-r--r--server/piztor/server.py37
3 files changed, 61 insertions, 38 deletions
diff --git a/server/piztor/prob.py b/server/piztor/prob.py
index 57ce2fe..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):
@@ -78,12 +79,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..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,
@@ -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