报文头程序

报文头程序

方法一:

/*

 * add_elink_header - add elink header for the package

 */

uint8_t *sendbuf = NULL;

static inline void add_elink_header(uint8_t *sendbuf, int text_len)
{
uint32_t *content_len = (uint32_t *)(sendbuf + ELINK_HEADER_FIXPART_LEN);
sendbuf[0] = 0x3f;
sendbuf[1] = 0x72;
sendbuf[2] = 0x1f;
sendbuf[3] = 0xb5;
*content_len = htonl((uint32_t)text_len);

}

方法二:

void sendPacket(char *msg, int msglen, int crypto)
{
    char msgBuf[MAX_PKT_LEN] = {0};
    char sendBuf[MAX_PKT_LEN] = {0};
    int lenth = 0;
    lenth = msglen;

    if (crypto)
    {
        if (msglen % AES_BLOCK_SIZE != 0)
        {
            lenth = (msglen / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
        }
        do_encrypt((unsigned char *)msg, lenth, msgBuf);
    }
    else
    {
        strncpy(msgBuf, msg, lenth);
    }
    sendBuf[0] = 0x3f;
    sendBuf[1] = 0x72;
    sendBuf[2] = 0x1f;
    sendBuf[3] = 0xb5;
    sendBuf[4] = 0x00;
    sendBuf[5] = 0x00;
    sendBuf[6] = lenth >> 8;
    sendBuf[7] = lenth & 0xff;
    memcpy(&sendBuf[8], msgBuf, lenth);
    send(g_fd, sendBuf, lenth + 8, 0);
    return;
}