ActiveSync Application Layer Program protocol analysis-rapi handshake process

Source: Internet
Author: User
Indicate the source and author's contact information during reprinting.
Author's contact information: the communication protocol between ActiveSync and Window Mobile is not complex, and TCP/IP is run on RNDIS + USBNET, the application layer protocols on TCP/IP include RAPI and RRAC. Some time ago, I completed the implementation of the RAPI and RRAC protocols on the PC end and the device end. In this series, we will analyze their principles and implementations for the reference of friends who want to do similar work. This article describes the handshake process of RAPI. The PC-side ActiveSync listens to port 990. During synchronization, the device connects to this port and then begins to shake hands: 1. the device sends four bytes of data (0x00) to request a handshake. 2. The PC side responds to four bytes of data (0x03), indicating that the handshake is accepted and the device information is required. 3. The device responds to four bytes of data (0x04), indicating that the device information is to be uploaded and followed by the device information. 4. The PC side reads the device information. If the argument is not required, the handshake ends here (the handshake is not clear yet ). The device information structure is as follows: typedef struct _ RapiDeviceInfo {RapiDeviceGuid guid; unsigned int identifier; unsigned int OS _version_minor; WStr * name; unsigned int dev_version; unsigned int cpu_type; unsigned int current_partner; unsigned int dev_id; char * platform; char * model; unsigned int components_nr; RapiComponent * components; unsigned int pw_key;} RapiDeviceInfo; the first four bytes of this packet are in this structure. The following data is not directly mapped by the structure memory layout, but is specially encoded. The main difference is that all integers are stored in the small-end format. Before name, there are four bytes that represent the number of characters of name (excluding empty characters ), four bytes before platform represent the number of bytes of platform (including null characters), and four bytes before model represent the number of bytes of model (this does not include null words ). From this structure, we can see that Microsoft defined the Protocol too poorly: name is a wide character, while platform and model are multi-byte strings, which are not consistent with the previous name, we still cannot know their encoding methods. What's worse is that the meaning of the length before platform and model is different. The PC code is similar to: static AsmRet rapi_host_connection_device_handle_hand_shake (AsmConnection * thiz) {AsmRet ret = ASM_RET_FAIL; asm_return_val_if_fail (thiz! = NULL, ASM_RET_FAIL); PrivInfo * priv = (PrivInfo *) thiz-> priv; asm_return_val_if_fail (priv-> stream! = NULL, ASM_RET_ OK); int length = 0; unsigned int cmd = 0; unsigned int resp = 0; AsmInputBuffer * input = NULL; do {ret = asm_stream_read (priv-> stream, & cmd, sizeof (cmd), & length); if (ret! = ASM_RET_ OK | cmd! = RAPI_COMMAND_HAND_SHAKE) break; resp = RAPI_RESP_HAND_SHAKE; ret = asm_stream_write (priv-> stream, & resp, sizeof (resp), & length); if (ret! = ASM_RET_ OK) break; ret = asm_stream_read (priv-> stream, & cmd, sizeof (cmd), & length); if (ret! = ASM_RET_ OK | cmd! = RAPI_RESP_GET_INFO) break; input = asm_input_buffer_create (NULL, 0, ASM_ENDIAN_LITTLE, NULL); ret = rapi_stream_read (priv-> stream, input); if (ret! = ASM_RET_ OK) break; ret = rapi_host_connection_device_parse_device_info (thiz, input);} while (0); if (ret! = ASM_RET_ OK) {asm_stream_destroy (priv-> stream); priv-> stream = NULL; printf ("% s: % d hand shake failed. /n ", _ func __, _ LINE _);} asm_input_buffer_destroy (input); return ASM_RET_ OK;} the device code is similar to: static AsmRet handle (AsmConnection * thiz) {AsmRet ret = ASM_RET_FAIL; asm_return_val_if_fail (thiz! = NULL, ret); PrivInfo * priv = (PrivInfo *) thiz-> priv; RapiDeviceInfo info = {0}; size_t length = 0; unsigned int cmd = 0; unsigned int resp = 0; trim (priv-> output); cmd = uint32_to_endian (values, ASM_ENDIAN_LITTLE); ret = asm_stream_write (priv-> stream, & cmd, sizeof (cmd ), & length); assert (length = sizeof (cmd); ret = asm_stream_read (priv-> stream, & resp, sizeof (resp), & length ); assert (length = sizeof (resp); assert (resp = bytes); cmd = sums (RAPI_RESP_GET_INFO, ASM_ENDIAN_LITTLE); ret = asm_stream_write (priv-> stream, & cmd, sizeof (cmd), & length); assert (length = sizeof (cmd); if (rapi_device_get_info (priv-> device, & info) = ASM_RET_ OK) {if (rapi_buffer_write_info (priv-> output, & info) = ASM_RET_ OK) {ret = rapi_stream_write (priv-> stream, priv-> output) ;}} return ret ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.