Commit 28ba2aae818a9285d1418220fbf47e402fecad80

Authored by totoro
1 parent 926012e9

add KAIWA_RECEIVE_MESSAGE constant

fix orthograph
add simple_wait_for_reply function for waiting Muscle message answer
CXM_message_constants.anubis
... ... @@ -163,6 +163,7 @@ public define Word32 _CXM_KAIWA_REMOVE_FRIEND = 0x33772804.
163 163 public define Word32 _CXM_KAIWA_BLOCK_FRIEND = 0x33772805.
164 164 public define Word32 _CXM_KAIWA_ONLINE_STATUS = 0x33772806.
165 165 public define Word32 _CXM_KAIWA_SEND_MESSAGE = 0x33772807.
  166 +public define Word32 _CXM_KAIWA_RECEIVE_MESSAGE = 0x33772808.
166 167  
167 168 // *************************************************
168 169 // ************* KERO_PBX API *************
... ...
mail/authentication.anubis
1   -/*
2   - * Created by PyramIDE.
3   - * User: ricard
4   - * Date: 07/03/2009
5   - * Time: 21:42
6   - *
7   - * To change this template use Tools | Options | Coding | Edit Standard Headers.
8   - */
9   -
10   -read tools/basis.anubis
11   -read system/bytearray.anubis
12   -read system/string.anubis
13   -
14   - /** Give a timestamp according to RFC2822. this sort of timestamp
15   - * is use for CRAM authentication (Challenge Reponse Authentication Mechanism)
16   - * this time timestamp must unique and not predictable to ensure security
17   - * mechanism of authentication.
18   - * @param dummy One meaning nothing
19   - * @return Sring of the timestamp
20   - */
21   -public define String
22   - get_time_stamp
23   - (
24   - String domain
25   - )=
26   - with time = (UTime) unow,
27   - "<"+virtual_machine_id+"."+time.seconds+"@mail."+domain+">".
28   -
29   - /**
30   - */
31   -public define Bool
32   - apop_md5
33   - (
34   - String time_stamp,
35   - String password,
36   - String given_hash
37   - ) =
38   - with my_stamp = time_stamp + password,
39   - my_hash = to_lower(to_ascii(md5(to_byte_array(my_stamp)))),
40   -// print("time_stamp "+ time_stamp + "\n");
41   -// print("given_hash " + given_hash + "\n");
42   -// print("my_hash " + my_hash + "\n");
43   - if to_lower(given_hash) = my_hash then
44   - true
45   - else
46   - false.
47   -
48   -
49   -
50   -define String
51   - hmac_md5_compute
52   - (
53   - ByteArray data, //message to be cripted
54   - ByteArray key //this is share secret key (i.e. password)
55   - ) =
56   - with ba_data = constant_byte_array(64, 0),
57   - ba_ipad = constant_byte_array(64, 0x36),
58   - ba_opad = constant_byte_array(64, 0x5c),
59   - //put key into ByteArray
60   - //but if key is longer than 64 bytes, then we must apply md5 on it and put it into ByteArray
61   - with ba_key = fill_ByteArray(ba_data, if length(key) > 64 then md5(key) else key),
62   - key_ipad = ba_key : ba_ipad, // XOR key with ipad
63   - key_opad = ba_key : ba_opad, // XOR key with opad
64   - to_lower(to_ascii(md5(key_opad + (md5(key_ipad + data))))). // md5(K (+) ipad) and merge with data
65   -
66   -
67   -public define Bool
68   - hmac_md5_check
69   - (
70   - ByteArray data, //message to be cripted
71   - ByteArray key, //this is share secret key (i.e. password)
72   - String given_digest //The hash given by the user who want to be authenticated
73   - ) =
74   - if to_lower(given_digest) = hmac_md5_compute(data, key) then
75   -// print("HMAC-MD5 Success \n");
76   - true
77   - else
78   -// print("HMAC-MD5 Failed \n");
79   -// print(" My Hash = " + my_hash + "\n");
80   -// print("User Hash = " + given_digest + "\n");
81   - false.
82   -
83   -public define Bool
84   - hmac_md5_check
85   - (
86   - String data, //message to be cripted
87   - String key, //this is share secret key (i.e. password)
88   - String given_digest //The hash given by the user who want to be authenticated
89   - ) =
90   - hmac_md5_check(to_byte_array(data), to_byte_array(key), given_digest).
91   -
92   -public define String
93   - hmac_md5_compute
94   - (
95   - String data, //message to be cripted
96   - String key //this is share secret key (i.e. password)
97   - ) =
98   - hmac_md5_compute(to_byte_array(data), to_byte_array(key)).
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: ricard
  4 + * Date: 07/03/2009
  5 + * Time: 21:42
  6 + *
  7 + * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8 + */
  9 +
  10 +read tools/basis.anubis
  11 +read system/bytearray.anubis
  12 +read system/string.anubis
  13 +
  14 + /** Give a timestamp according to RFC2822. this sort of timestamp
  15 + * is use for CRAM authentication (Challenge Reponse Authentication Mechanism)
  16 + * this time timestamp must unique and not predictable to ensure security
  17 + * mechanism of authentication.
  18 + * @param dummy One meaning nothing
  19 + * @return Sring of the timestamp
  20 + */
  21 +public define String
  22 + get_time_stamp
  23 + (
  24 + String domain
  25 + )=
  26 + with time = (UTime) unow,
  27 + "<"+virtual_machine_id+"."+time.seconds+"@mail."+domain+">".
  28 +
  29 + /**
  30 + */
  31 +public define Bool
  32 + apop_md5
  33 + (
  34 + String time_stamp,
  35 + String password,
  36 + String given_hash
  37 + ) =
  38 + with my_stamp = time_stamp + password,
  39 + my_hash = to_lower(to_ascii(md5(to_byte_array(my_stamp)))),
  40 +// print("time_stamp "+ time_stamp + "\n");
  41 +// print("given_hash " + given_hash + "\n");
  42 +// print("my_hash " + my_hash + "\n");
  43 + if to_lower(given_hash) = my_hash then
  44 + true
  45 + else
  46 + false.
  47 +
  48 +
  49 +
  50 +define String
  51 + hmac_md5_compute
  52 + (
  53 + ByteArray data, //message to be crypted
  54 + ByteArray key //this is share secret key (i.e. password)
  55 + ) =
  56 + with ba_data = constant_byte_array(64, 0),
  57 + ba_ipad = constant_byte_array(64, 0x36),
  58 + ba_opad = constant_byte_array(64, 0x5c),
  59 + //put key into ByteArray
  60 + //but if key is longer than 64 bytes, then we must apply md5 on it and put it into ByteArray
  61 + with ba_key = fill_ByteArray(ba_data, if length(key) > 64 then md5(key) else key),
  62 + key_ipad = ba_key : ba_ipad, // XOR key with ipad
  63 + key_opad = ba_key : ba_opad, // XOR key with opad
  64 + to_lower(to_ascii(md5(key_opad + (md5(key_ipad + data))))). // md5(K (+) ipad) and merge with data
  65 +
  66 +
  67 +public define Bool
  68 + hmac_md5_check
  69 + (
  70 + ByteArray data, //message to be crypted
  71 + ByteArray key, //this is share secret key (i.e. password)
  72 + String given_digest //The hash given by the user who want to be authenticated
  73 + ) =
  74 + if to_lower(given_digest) = hmac_md5_compute(data, key) then
  75 +// print("HMAC-MD5 Success \n");
  76 + true
  77 + else
  78 +// print("HMAC-MD5 Failed \n");
  79 +// print(" My Hash = " + my_hash + "\n");
  80 +// print("User Hash = " + given_digest + "\n");
  81 + false.
  82 +
  83 +public define Bool
  84 + hmac_md5_check
  85 + (
  86 + String data, //message to be cripted
  87 + String key, //this is share secret key (i.e. password)
  88 + String given_digest //The hash given by the user who want to be authenticated
  89 + ) =
  90 + hmac_md5_check(to_byte_array(data), to_byte_array(key), given_digest).
  91 +
  92 +public define String
  93 + hmac_md5_compute
  94 + (
  95 + String data, //message to be cripted
  96 + String key //this is share secret key (i.e. password)
  97 + ) =
  98 + hmac_md5_compute(to_byte_array(data), to_byte_array(key)).
... ...
net_services/CXM_generic_protocol.anubis
1   -๏ปฟ/*
2   - *
3   - * User: David RENE
4   - * Date: 25/04/2007
5   - * Time: 16:20
6   - * (c) Calexium
7   - *
8   - */
9   -
10   -read system/muscle.anubis
11   -read system/data_io.anubis
12   -read system/string.anubis
13   -read tools/basis.anubis
14   -read system/message_queue.anubis
15   -read calexium_lib/CXM_message_constants.anubis
16   -
17   -public define Word32 _CXM_OK = 0.
18   -public define Word32 _CXM_ERROR = 1.
19   -public define Word32 _CXM_UNKNOW_CMD = 2.
20   -public define Word32 _CXM_UNKNOW_SERVICE = 3.
21   -public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4.
22   -public define Word32 _CXM_FORBIDDEN = 5.
23   -public define Word32 _CXM_BAD_AUTHENTICATION = 6.
24   -public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later
25   -
26   -public type ProtocolResult:
27   - failure,
28   - timeout,
29   - unknow_cmd,
30   - error,
31   - error(Word32, String),
32   - ok,
33   - ok_msg(Message).
34   -
35   -public define One
36   - send_ACK_error
37   - (
38   - MessageQueue queue,
39   - Word32 cmd_id,
40   - Word32 error_code,
41   - String error_string,
42   - )=
43   - with err_msg = message(_CXM_ACK),
44   - forget(add_int32(err_msg, "CMD", cmd_id));
45   - forget(add_int32(err_msg, "STATUS", error_code));
46   - (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string))
47   - else unique);
48   - forget(queue.add_Message_to_send(err_msg)).
49   -
50   -public define One
51   - send_ACK_error
52   - (
53   - MessageQueue queue,
54   - Word32 cmd_id
55   - )=
56   - send_ACK_error(queue, cmd_id, _CXM_ERROR, "").
57   -
58   -public define One
59   - send_ACK_ok
60   - (
61   - MessageQueue queue,
62   - Word32 cmd_id
63   - )=
64   - with ok_msg = message(_CXM_ACK),
65   - forget(add_int32(ok_msg, "CMD", cmd_id));
66   - forget(add_int32(ok_msg, "STATUS", _CXM_OK));
67   - forget(queue.add_Message_to_send(ok_msg))
68   - .
69   -
70   -public define One
71   - send_ACK_ok
72   - (
73   - MessageQueue queue,
74   - Word32 cmd_id,
75   - Message result
76   - )=
77   - with ok_msg = message(_CXM_ACK),
78   - forget(add_int32(ok_msg, "CMD", cmd_id));
79   - forget(add_int32(ok_msg, "STATUS", _CXM_OK));
80   - forget(add_message(ok_msg, "RESULT", result));
81   - forget(queue.add_Message_to_send(ok_msg))
82   - .
83   -
84   -public define One
85   - send_result
86   - (
87   - MessageQueue queue,
88   - Word32 cmd_id,
89   - Maybe(Message) mb_msg
90   - )=
91   - if mb_msg is
92   - {
93   - failure then send_ACK_error(queue, cmd_id),
94   - success(msg) then send_ACK_ok(queue, cmd_id, msg)
95   - }.
96   -
97   -public define One
98   - send_result
99   - (
100   - MessageQueue queue,
101   - Word32 cmd_id,
102   - Result((Word32, String), Message) mb_msg
103   - )=
104   - if mb_msg is
105   - {
106   - error(err) then
107   - if err is (err_code, err_string) then
108   - send_ACK_error(queue, cmd_id, err_code, err_string),
109   - ok(msg) then send_ACK_ok(queue, cmd_id, msg)
110   - }.
111   -
112   -public define One
113   - send_result
114   - (
115   - MessageQueue queue,
116   - Word32 cmd_id,
117   - Bool result
118   - )=
119   - if result then
120   - send_ACK_ok(queue, cmd_id)
121   - else
122   - send_ACK_error(queue, cmd_id).
123   -
124   -public define ProtocolResult
125   - wait_for_reply
126   - (
127   - MessageQueue mQ,
128   - Word32 wait_cmd,
129   - Int t_out
130   - ) =
131   - if mQ.get_next_received_Message(t_out) is
132   - {
133   - timeout then timeout,
134   - closed then failure, //println("wait_for_reply closed");
135   -
136   - msg(_msg) then
137   - if *_msg.what = _CXM_ACK then
138   - if find_int32(_msg, "CMD") is
139   - {
140   - failure then failure, //println("wait_for_reply CMD");
141   - success(cmd) then
142   -// println("wait_for_reply CMD="+to_hexa(cmd));
143   - if find_int32(_msg, "STATUS") is
144   - {
145   - failure then failure, //println("wait_for_reply STATUS");
146   - success(status) then
147   - if cmd = wait_cmd then
148   - (
149   - if status = _CXM_OK then
150   - if find_message(_msg, "RESULT") is
151   - {
152   - failure then ok,
153   - success(ok_message) then ok_msg(ok_message)
154   - }
155   - else if status = _CXM_ERROR then
156   - error
157   - else if status = _CXM_UNKNOW_CMD then
158   - unknow_cmd
159   - else
160   - error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "")
161   - )
162   - else
163   - failure //println("wait_for_reply ");
164   - }
165   - }
166   - else
167   - failure //println("wait_for_reply not ACK");
168   - }.
169   -
  1 +๏ปฟ/*
  2 + *
  3 + * User: David RENE
  4 + * Date: 25/04/2007
  5 + * Time: 16:20
  6 + * (c) Calexium
  7 + *
  8 + */
  9 +
  10 +read system/muscle.anubis
  11 +read system/data_io.anubis
  12 +read system/string.anubis
  13 +read tools/basis.anubis
  14 +read system/message_queue.anubis
  15 +read calexium_lib/CXM_message_constants.anubis
  16 +
  17 +public define Word32 _CXM_OK = 0.
  18 +public define Word32 _CXM_ERROR = 1.
  19 +public define Word32 _CXM_UNKNOW_CMD = 2.
  20 +public define Word32 _CXM_UNKNOW_SERVICE = 3.
  21 +public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4.
  22 +public define Word32 _CXM_FORBIDDEN = 5.
  23 +public define Word32 _CXM_BAD_AUTHENTICATION = 6.
  24 +public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later
  25 +
  26 +public type ProtocolResult:
  27 + failure,
  28 + timeout,
  29 + unknow_cmd,
  30 + error,
  31 + error(Word32, String),
  32 + ok,
  33 + ok_msg(Message).
  34 +
  35 +public define One
  36 + send_ACK_error
  37 + (
  38 + MessageQueue queue,
  39 + Word32 cmd_id,
  40 + Word32 error_code,
  41 + String error_string,
  42 + )=
  43 + with err_msg = message(_CXM_ACK),
  44 + forget(add_int32(err_msg, "CMD", cmd_id));
  45 + forget(add_int32(err_msg, "STATUS", error_code));
  46 + (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string))
  47 + else unique);
  48 + forget(queue.add_Message_to_send(err_msg)).
  49 +
  50 +public define One
  51 + send_ACK_error
  52 + (
  53 + MessageQueue queue,
  54 + Word32 cmd_id
  55 + )=
  56 + send_ACK_error(queue, cmd_id, _CXM_ERROR, "").
  57 +
  58 +public define One
  59 + send_ACK_ok
  60 + (
  61 + MessageQueue queue,
  62 + Word32 cmd_id
  63 + )=
  64 + with ok_msg = message(_CXM_ACK),
  65 + forget(add_int32(ok_msg, "CMD", cmd_id));
  66 + forget(add_int32(ok_msg, "STATUS", _CXM_OK));
  67 + forget(queue.add_Message_to_send(ok_msg))
  68 + .
  69 +
  70 +public define One
  71 + send_ACK_ok
  72 + (
  73 + MessageQueue queue,
  74 + Word32 cmd_id,
  75 + Message result
  76 + )=
  77 + with ok_msg = message(_CXM_ACK),
  78 + forget(add_int32(ok_msg, "CMD", cmd_id));
  79 + forget(add_int32(ok_msg, "STATUS", _CXM_OK));
  80 + forget(add_message(ok_msg, "RESULT", result));
  81 + forget(queue.add_Message_to_send(ok_msg))
  82 + .
  83 +
  84 +public define One
  85 + send_result
  86 + (
  87 + MessageQueue queue,
  88 + Word32 cmd_id,
  89 + Maybe(Message) mb_msg
  90 + )=
  91 + if mb_msg is
  92 + {
  93 + failure then send_ACK_error(queue, cmd_id),
  94 + success(msg) then send_ACK_ok(queue, cmd_id, msg)
  95 + }.
  96 +
  97 +public define One
  98 + send_result
  99 + (
  100 + MessageQueue queue,
  101 + Word32 cmd_id,
  102 + Result((Word32, String), Message) mb_msg
  103 + )=
  104 + if mb_msg is
  105 + {
  106 + error(err) then
  107 + if err is (err_code, err_string) then
  108 + send_ACK_error(queue, cmd_id, err_code, err_string),
  109 + ok(msg) then send_ACK_ok(queue, cmd_id, msg)
  110 + }.
  111 +
  112 +public define One
  113 + send_result
  114 + (
  115 + MessageQueue queue,
  116 + Word32 cmd_id,
  117 + Bool result
  118 + )=
  119 + if result then
  120 + send_ACK_ok(queue, cmd_id)
  121 + else
  122 + send_ACK_error(queue, cmd_id).
  123 +
  124 +public define ProtocolResult
  125 + wait_for_reply
  126 + (
  127 + MessageQueue mQ,
  128 + Word32 wait_cmd,
  129 + Int t_out
  130 + ) =
  131 + if mQ.get_next_received_Message(t_out) is
  132 + {
  133 + timeout then timeout,
  134 + closed then failure, //println("wait_for_reply closed");
  135 +
  136 + msg(_msg) then
  137 + if *_msg.what = _CXM_ACK then
  138 + if find_int32(_msg, "CMD") is
  139 + {
  140 + failure then failure, //println("wait_for_reply CMD");
  141 + success(cmd) then
  142 +// println("wait_for_reply CMD="+to_hexa(cmd));
  143 + if find_int32(_msg, "STATUS") is
  144 + {
  145 + failure then failure, //println("wait_for_reply STATUS");
  146 + success(status) then
  147 + if cmd = wait_cmd then
  148 + (
  149 + if status = _CXM_OK then
  150 + if find_message(_msg, "RESULT") is
  151 + {
  152 + failure then ok,
  153 + success(ok_message) then ok_msg(ok_message)
  154 + }
  155 + else if status = _CXM_ERROR then
  156 + error
  157 + else if status = _CXM_UNKNOW_CMD then
  158 + unknow_cmd
  159 + else
  160 + error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "")
  161 + )
  162 + else
  163 + failure //println("wait_for_reply ");
  164 + }
  165 + }
  166 + else
  167 + failure //println("wait_for_reply not ACK");
  168 + }.
  169 +
  170 +public define Bool
  171 + simple_wait_for_reply
  172 + (
  173 + MessageQueue mQ,
  174 + Word32 wait_cmd,
  175 + Int t_out
  176 + ) =
  177 + if wait_for_reply(mQ, wait_cmd, t_out) is
  178 + {
  179 + failure then false,
  180 + timeout then false,
  181 + unknow_cmd then false,
  182 + error then false,
  183 + error(_, _) then false,
  184 + ok then true,
  185 + ok_msg(msg) then true
  186 + }.
... ...