如何在esp8266上获取udp头信息

时间:2019-02-01 13:38:48

标签: udp esp8266

我正在使用ESP8266_SDK并尝试回答UDP请求,但无法找到发件人信息(IP和端口)。我可以广播该消息以规避发送方IP的必要性,但不能对端口进行相同操作。而且,虽然我正在侦听的端口是固定的,但我应该将消息发送回的端口随每条消息而变化(它在UDP标头规范中,但是显然,SDK不能让我轻松访问)。

下面是代码的相关部分:

static void ICACHE_FLASH_ATTR udp_recv(void *arg, char *pusrdata, unsigned short length) {
    struct espconn *pespconn = arg;

    os_printf("client %d.%d.%d.%d:%d -> ", pespconn->proto.udp->remote_ip[0],
              pespconn->proto.udp->remote_ip[1], pespconn->proto.udp->remote_ip[2],
              pespconn->proto.udp->remote_ip[3], pespconn->proto.udp->remote_port);
    os_printf("received %d bytes of data\n", length);
    os_printf("server %d.%d.%d.%d:%d\n", pespconn->proto.udp->local_ip[0],
              pespconn->proto.udp->local_ip[1], pespconn->proto.udp->local_ip[2],
              pespconn->proto.udp->local_ip[3], pespconn->proto.udp->local_port);

    uint16_t i;
    for(i = 0; i < length; ++i) {
        os_printf("%02x ", pusrdata[i]); // the IP and port are not in the user data. It should be in the connection
    }
    os_printf("\n");

    sint8_t errcode = espconn_send(pespconn, response, sizeof(response)); //It's being sent to the wrong place because I don'r know the right IP/port pair.
    if(errcode != 0) {
        os_printf("%s\n", toURL(question));
        os_printf("UDP send: ");
        switch(errcode) {
            //case 0: os_printf("ok\n"); break;
            case ESPCONN_ISCONN: os_printf("already connected\n"); break;
            case ESPCONN_MEM: os_printf("lack of memory\n"); break;
            case ESPCONN_IF: os_printf("fail\n"); break;
            default: os_printf("unknown error (%d)\n", errcode);
        }
    }
}

void ICACHE_FLASH_ATTR server_start(void) {
    struct espconn *conn = (struct espconn *) os_zalloc(sizeof(struct espconn));

    if (conn == NULL) return;

    conn->type = ESPCONN_UDP;
    conn->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
    conn->proto.udp->local_port = XX; // port my devices listens to
    conn->proto.udp->remote_port = YYYY; // (????) port I should be sending te messages back

    char tcpserverip[16];
    os_sprintf(tcpserverip, "%s", "255.255.255.255\0"); // I broadcasting, but I should use the right IP address
    uint32_t ip = ipaddr_addr(tcpserverip);
    ip = ipaddr_addr(tcpserverip);
    os_memcpy(conn->proto.udp->remote_ip, &ip, 4);

    espconn_regist_recvcb(conn, udp_recv);

    sint8_t errcode = espconn_create(conn);
    conn->proto.udp->remote_ip[2], conn->proto.udp->remote_ip[3]);
    os_printf("UDP connection: ");
    switch(errcode) {
        case 0: os_printf("created\n"); break;
        case ESPCONN_ISCONN: os_printf("already connected\n"); break;
        case ESPCONN_MEM: os_printf("lack of memory\n"); break;
        case ESPCONN_ARG: os_printf("invalid arguments\n"); break;
        default: os_printf("unknown error (%d)\n", errcode);
    }
}

我尝试设置连接中的所有字段(本地IP,本地端口,远程IP和远程端口),仅设置本地字段(我期望设备会自行填充远程字段) ,只是远程字段(按预期我什至没有收到消息)以及许多不同的组合。我也尝试将命令更改为 espconn_sendto ,而不是 espconn_send ,但这没什么区别,在此论坛的其他地方,有人说 espconn_sendto 有一个错误。

我在官方文档(https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf)中找不到与此相关的任何内容。

如何获取远程信息?有人知道吗?

预先感谢您的任何建议。

0 个答案:

没有答案
相关问题