From 28ba2aae818a9285d1418220fbf47e402fecad80 Mon Sep 17 00:00:00 2001 From: totoro Date: Sun, 27 Oct 2013 02:06:04 +0200 Subject: [PATCH] add KAIWA_RECEIVE_MESSAGE constant fix orthograph add simple_wait_for_reply function for waiting Muscle message answer --- CXM_message_constants.anubis | 1 + mail/authentication.anubis | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------- net_services/CXM_generic_protocol.anubis | 355 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 files changed, 285 insertions(+), 267 deletions(-) diff --git a/CXM_message_constants.anubis b/CXM_message_constants.anubis index c91a1be..164ee2c 100644 --- a/CXM_message_constants.anubis +++ b/CXM_message_constants.anubis @@ -163,6 +163,7 @@ public define Word32 _CXM_KAIWA_REMOVE_FRIEND = 0x33772804. public define Word32 _CXM_KAIWA_BLOCK_FRIEND = 0x33772805. public define Word32 _CXM_KAIWA_ONLINE_STATUS = 0x33772806. public define Word32 _CXM_KAIWA_SEND_MESSAGE = 0x33772807. +public define Word32 _CXM_KAIWA_RECEIVE_MESSAGE = 0x33772808. // ************************************************* // ************* KERO_PBX API ************* diff --git a/mail/authentication.anubis b/mail/authentication.anubis index 8fb3a1a..d25eb23 100644 --- a/mail/authentication.anubis +++ b/mail/authentication.anubis @@ -1,98 +1,98 @@ -/* - * Created by PyramIDE. - * User: ricard - * Date: 07/03/2009 - * Time: 21:42 - * - * To change this template use Tools | Options | Coding | Edit Standard Headers. - */ - -read tools/basis.anubis -read system/bytearray.anubis -read system/string.anubis - - /** Give a timestamp according to RFC2822. this sort of timestamp - * is use for CRAM authentication (Challenge Reponse Authentication Mechanism) - * this time timestamp must unique and not predictable to ensure security - * mechanism of authentication. - * @param dummy One meaning nothing - * @return Sring of the timestamp - */ -public define String - get_time_stamp - ( - String domain - )= - with time = (UTime) unow, - "<"+virtual_machine_id+"."+time.seconds+"@mail."+domain+">". - - /** - */ -public define Bool - apop_md5 - ( - String time_stamp, - String password, - String given_hash - ) = - with my_stamp = time_stamp + password, - my_hash = to_lower(to_ascii(md5(to_byte_array(my_stamp)))), -// print("time_stamp "+ time_stamp + "\n"); -// print("given_hash " + given_hash + "\n"); -// print("my_hash " + my_hash + "\n"); - if to_lower(given_hash) = my_hash then - true - else - false. - - - -define String - hmac_md5_compute - ( - ByteArray data, //message to be cripted - ByteArray key //this is share secret key (i.e. password) - ) = - with ba_data = constant_byte_array(64, 0), - ba_ipad = constant_byte_array(64, 0x36), - ba_opad = constant_byte_array(64, 0x5c), - //put key into ByteArray - //but if key is longer than 64 bytes, then we must apply md5 on it and put it into ByteArray - with ba_key = fill_ByteArray(ba_data, if length(key) > 64 then md5(key) else key), - key_ipad = ba_key : ba_ipad, // XOR key with ipad - key_opad = ba_key : ba_opad, // XOR key with opad - to_lower(to_ascii(md5(key_opad + (md5(key_ipad + data))))). // md5(K (+) ipad) and merge with data - - -public define Bool - hmac_md5_check - ( - ByteArray data, //message to be cripted - ByteArray key, //this is share secret key (i.e. password) - String given_digest //The hash given by the user who want to be authenticated - ) = - if to_lower(given_digest) = hmac_md5_compute(data, key) then -// print("HMAC-MD5 Success \n"); - true - else -// print("HMAC-MD5 Failed \n"); -// print(" My Hash = " + my_hash + "\n"); -// print("User Hash = " + given_digest + "\n"); - false. - -public define Bool - hmac_md5_check - ( - String data, //message to be cripted - String key, //this is share secret key (i.e. password) - String given_digest //The hash given by the user who want to be authenticated - ) = - hmac_md5_check(to_byte_array(data), to_byte_array(key), given_digest). - -public define String - hmac_md5_compute - ( - String data, //message to be cripted - String key //this is share secret key (i.e. password) - ) = - hmac_md5_compute(to_byte_array(data), to_byte_array(key)). +/* + * Created by PyramIDE. + * User: ricard + * Date: 07/03/2009 + * Time: 21:42 + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ + +read tools/basis.anubis +read system/bytearray.anubis +read system/string.anubis + + /** Give a timestamp according to RFC2822. this sort of timestamp + * is use for CRAM authentication (Challenge Reponse Authentication Mechanism) + * this time timestamp must unique and not predictable to ensure security + * mechanism of authentication. + * @param dummy One meaning nothing + * @return Sring of the timestamp + */ +public define String + get_time_stamp + ( + String domain + )= + with time = (UTime) unow, + "<"+virtual_machine_id+"."+time.seconds+"@mail."+domain+">". + + /** + */ +public define Bool + apop_md5 + ( + String time_stamp, + String password, + String given_hash + ) = + with my_stamp = time_stamp + password, + my_hash = to_lower(to_ascii(md5(to_byte_array(my_stamp)))), +// print("time_stamp "+ time_stamp + "\n"); +// print("given_hash " + given_hash + "\n"); +// print("my_hash " + my_hash + "\n"); + if to_lower(given_hash) = my_hash then + true + else + false. + + + +define String + hmac_md5_compute + ( + ByteArray data, //message to be crypted + ByteArray key //this is share secret key (i.e. password) + ) = + with ba_data = constant_byte_array(64, 0), + ba_ipad = constant_byte_array(64, 0x36), + ba_opad = constant_byte_array(64, 0x5c), + //put key into ByteArray + //but if key is longer than 64 bytes, then we must apply md5 on it and put it into ByteArray + with ba_key = fill_ByteArray(ba_data, if length(key) > 64 then md5(key) else key), + key_ipad = ba_key : ba_ipad, // XOR key with ipad + key_opad = ba_key : ba_opad, // XOR key with opad + to_lower(to_ascii(md5(key_opad + (md5(key_ipad + data))))). // md5(K (+) ipad) and merge with data + + +public define Bool + hmac_md5_check + ( + ByteArray data, //message to be crypted + ByteArray key, //this is share secret key (i.e. password) + String given_digest //The hash given by the user who want to be authenticated + ) = + if to_lower(given_digest) = hmac_md5_compute(data, key) then +// print("HMAC-MD5 Success \n"); + true + else +// print("HMAC-MD5 Failed \n"); +// print(" My Hash = " + my_hash + "\n"); +// print("User Hash = " + given_digest + "\n"); + false. + +public define Bool + hmac_md5_check + ( + String data, //message to be cripted + String key, //this is share secret key (i.e. password) + String given_digest //The hash given by the user who want to be authenticated + ) = + hmac_md5_check(to_byte_array(data), to_byte_array(key), given_digest). + +public define String + hmac_md5_compute + ( + String data, //message to be cripted + String key //this is share secret key (i.e. password) + ) = + hmac_md5_compute(to_byte_array(data), to_byte_array(key)). diff --git a/net_services/CXM_generic_protocol.anubis b/net_services/CXM_generic_protocol.anubis index 433f9d8..8e84b1f 100644 --- a/net_services/CXM_generic_protocol.anubis +++ b/net_services/CXM_generic_protocol.anubis @@ -1,169 +1,186 @@ -/* - * - * User: David RENE - * Date: 25/04/2007 - * Time: 16:20 - * (c) Calexium - * - */ - -read system/muscle.anubis -read system/data_io.anubis -read system/string.anubis -read tools/basis.anubis -read system/message_queue.anubis -read calexium_lib/CXM_message_constants.anubis - -public define Word32 _CXM_OK = 0. -public define Word32 _CXM_ERROR = 1. -public define Word32 _CXM_UNKNOW_CMD = 2. -public define Word32 _CXM_UNKNOW_SERVICE = 3. -public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4. -public define Word32 _CXM_FORBIDDEN = 5. -public define Word32 _CXM_BAD_AUTHENTICATION = 6. -public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later - -public type ProtocolResult: - failure, - timeout, - unknow_cmd, - error, - error(Word32, String), - ok, - ok_msg(Message). - -public define One - send_ACK_error - ( - MessageQueue queue, - Word32 cmd_id, - Word32 error_code, - String error_string, - )= - with err_msg = message(_CXM_ACK), - forget(add_int32(err_msg, "CMD", cmd_id)); - forget(add_int32(err_msg, "STATUS", error_code)); - (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string)) - else unique); - forget(queue.add_Message_to_send(err_msg)). - -public define One - send_ACK_error - ( - MessageQueue queue, - Word32 cmd_id - )= - send_ACK_error(queue, cmd_id, _CXM_ERROR, ""). - -public define One - send_ACK_ok - ( - MessageQueue queue, - Word32 cmd_id - )= - with ok_msg = message(_CXM_ACK), - forget(add_int32(ok_msg, "CMD", cmd_id)); - forget(add_int32(ok_msg, "STATUS", _CXM_OK)); - forget(queue.add_Message_to_send(ok_msg)) - . - -public define One - send_ACK_ok - ( - MessageQueue queue, - Word32 cmd_id, - Message result - )= - with ok_msg = message(_CXM_ACK), - forget(add_int32(ok_msg, "CMD", cmd_id)); - forget(add_int32(ok_msg, "STATUS", _CXM_OK)); - forget(add_message(ok_msg, "RESULT", result)); - forget(queue.add_Message_to_send(ok_msg)) - . - -public define One - send_result - ( - MessageQueue queue, - Word32 cmd_id, - Maybe(Message) mb_msg - )= - if mb_msg is - { - failure then send_ACK_error(queue, cmd_id), - success(msg) then send_ACK_ok(queue, cmd_id, msg) - }. - -public define One - send_result - ( - MessageQueue queue, - Word32 cmd_id, - Result((Word32, String), Message) mb_msg - )= - if mb_msg is - { - error(err) then - if err is (err_code, err_string) then - send_ACK_error(queue, cmd_id, err_code, err_string), - ok(msg) then send_ACK_ok(queue, cmd_id, msg) - }. - -public define One - send_result - ( - MessageQueue queue, - Word32 cmd_id, - Bool result - )= - if result then - send_ACK_ok(queue, cmd_id) - else - send_ACK_error(queue, cmd_id). - -public define ProtocolResult - wait_for_reply - ( - MessageQueue mQ, - Word32 wait_cmd, - Int t_out - ) = - if mQ.get_next_received_Message(t_out) is - { - timeout then timeout, - closed then failure, //println("wait_for_reply closed"); - - msg(_msg) then - if *_msg.what = _CXM_ACK then - if find_int32(_msg, "CMD") is - { - failure then failure, //println("wait_for_reply CMD"); - success(cmd) then -// println("wait_for_reply CMD="+to_hexa(cmd)); - if find_int32(_msg, "STATUS") is - { - failure then failure, //println("wait_for_reply STATUS"); - success(status) then - if cmd = wait_cmd then - ( - if status = _CXM_OK then - if find_message(_msg, "RESULT") is - { - failure then ok, - success(ok_message) then ok_msg(ok_message) - } - else if status = _CXM_ERROR then - error - else if status = _CXM_UNKNOW_CMD then - unknow_cmd - else - error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "") - ) - else - failure //println("wait_for_reply "); - } - } - else - failure //println("wait_for_reply not ACK"); - }. - +/* + * + * User: David RENE + * Date: 25/04/2007 + * Time: 16:20 + * (c) Calexium + * + */ + +read system/muscle.anubis +read system/data_io.anubis +read system/string.anubis +read tools/basis.anubis +read system/message_queue.anubis +read calexium_lib/CXM_message_constants.anubis + +public define Word32 _CXM_OK = 0. +public define Word32 _CXM_ERROR = 1. +public define Word32 _CXM_UNKNOW_CMD = 2. +public define Word32 _CXM_UNKNOW_SERVICE = 3. +public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4. +public define Word32 _CXM_FORBIDDEN = 5. +public define Word32 _CXM_BAD_AUTHENTICATION = 6. +public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later + +public type ProtocolResult: + failure, + timeout, + unknow_cmd, + error, + error(Word32, String), + ok, + ok_msg(Message). + +public define One + send_ACK_error + ( + MessageQueue queue, + Word32 cmd_id, + Word32 error_code, + String error_string, + )= + with err_msg = message(_CXM_ACK), + forget(add_int32(err_msg, "CMD", cmd_id)); + forget(add_int32(err_msg, "STATUS", error_code)); + (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string)) + else unique); + forget(queue.add_Message_to_send(err_msg)). + +public define One + send_ACK_error + ( + MessageQueue queue, + Word32 cmd_id + )= + send_ACK_error(queue, cmd_id, _CXM_ERROR, ""). + +public define One + send_ACK_ok + ( + MessageQueue queue, + Word32 cmd_id + )= + with ok_msg = message(_CXM_ACK), + forget(add_int32(ok_msg, "CMD", cmd_id)); + forget(add_int32(ok_msg, "STATUS", _CXM_OK)); + forget(queue.add_Message_to_send(ok_msg)) + . + +public define One + send_ACK_ok + ( + MessageQueue queue, + Word32 cmd_id, + Message result + )= + with ok_msg = message(_CXM_ACK), + forget(add_int32(ok_msg, "CMD", cmd_id)); + forget(add_int32(ok_msg, "STATUS", _CXM_OK)); + forget(add_message(ok_msg, "RESULT", result)); + forget(queue.add_Message_to_send(ok_msg)) + . + +public define One + send_result + ( + MessageQueue queue, + Word32 cmd_id, + Maybe(Message) mb_msg + )= + if mb_msg is + { + failure then send_ACK_error(queue, cmd_id), + success(msg) then send_ACK_ok(queue, cmd_id, msg) + }. + +public define One + send_result + ( + MessageQueue queue, + Word32 cmd_id, + Result((Word32, String), Message) mb_msg + )= + if mb_msg is + { + error(err) then + if err is (err_code, err_string) then + send_ACK_error(queue, cmd_id, err_code, err_string), + ok(msg) then send_ACK_ok(queue, cmd_id, msg) + }. + +public define One + send_result + ( + MessageQueue queue, + Word32 cmd_id, + Bool result + )= + if result then + send_ACK_ok(queue, cmd_id) + else + send_ACK_error(queue, cmd_id). + +public define ProtocolResult + wait_for_reply + ( + MessageQueue mQ, + Word32 wait_cmd, + Int t_out + ) = + if mQ.get_next_received_Message(t_out) is + { + timeout then timeout, + closed then failure, //println("wait_for_reply closed"); + + msg(_msg) then + if *_msg.what = _CXM_ACK then + if find_int32(_msg, "CMD") is + { + failure then failure, //println("wait_for_reply CMD"); + success(cmd) then +// println("wait_for_reply CMD="+to_hexa(cmd)); + if find_int32(_msg, "STATUS") is + { + failure then failure, //println("wait_for_reply STATUS"); + success(status) then + if cmd = wait_cmd then + ( + if status = _CXM_OK then + if find_message(_msg, "RESULT") is + { + failure then ok, + success(ok_message) then ok_msg(ok_message) + } + else if status = _CXM_ERROR then + error + else if status = _CXM_UNKNOW_CMD then + unknow_cmd + else + error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "") + ) + else + failure //println("wait_for_reply "); + } + } + else + failure //println("wait_for_reply not ACK"); + }. + +public define Bool + simple_wait_for_reply + ( + MessageQueue mQ, + Word32 wait_cmd, + Int t_out + ) = + if wait_for_reply(mQ, wait_cmd, t_out) is + { + failure then false, + timeout then false, + unknow_cmd then false, + error then false, + error(_, _) then false, + ok then true, + ok_msg(msg) then true + }. -- libgit2 0.21.4