2017-01-24 10 views
1

esp8266 용 OS가 아닌 SDK v2.0을 사용하고 있습니다. esp8266의 멀티 캐스트 메시지에서 원본 주소를 찾으십니까?

나는 멀티 캐스트 그룹에 가입 할 수 있습니다.

I는 다음 메시지 (즉, 멀티 캐스트로 전송 된)를 수신한다.

발신자에게 응답하고 싶지만 struct espconn에있는 udp.remote_ip은 발신자가 아닌 멀티 캐스트 그룹의 주소입니다.

어떻게 보낸 사람의 IP 주소를받을 수 있나요?

편집 : 수신 함수는 struct espconn으로 캐스팅 된 void* arg 인수를가집니다.

답변

1

오래된 방법은 더 이상 일을하지 않습니다,하지만 난 원격 IP와 포트를 찾을 수있는 방법을 발견했다.

새로운 코드 :

struct espconn* udp_ch; 
    remot_info *premot = NULL; 
    udp_ch = arg; 

    if (espconn_get_connection_info(udp_ch,&premot,0) == ESPCONN_OK){ 
     os_printf("%d.%d.%d.%d:%d\n", premot->remote_ip[0], premot->remote_ip[1], premot->remote_ip[2], premot->remote_ip[3], premot->remote_port); 
    } 
    else{ 
     os_printf("Get info fail\n"); 
    } 

이것은 내가 전에 검색 정확히 것입니다. 지금은 볼 수있는 한 잘 작동합니다.

OLD는 :

는 내가 IP를 찾을 수있는 방법을 발견,하지만 난 이런 식으로 일을해야한다고 생각하지 않습니다. 내가 더 잘 찾을 때까지, 나는 이것을 사용할 것이다.

은 내가 한 첫 번째 일은 void* arg에서, 처음 256 16 진수 값을 인쇄했다. 제 주소가 여러개의 제로 앞에 나오기 시작했습니다.

uint32_t udp_get_addr(void* arg){ 
    uint32_t adr = 0; 

    uint16_t pos; 
    uint8_t* data = (uint8_t*) arg; 

    //unicast? 
    for(pos = 128; pos<144; pos++){ 
     if(data[pos] != 0){ 
      adr = 1; 
      break; 
     } 
    } 

    //multicast 
    if(adr == 1) 
     pos = 172; 
    else 
     pos = 124; 

    adr = data[pos]<<24 | data[pos+1]<<16 | data[pos+2]<<8 | data[pos+3]; 

    return adr; 
} 

나는이 방법이 나쁜 알고, 여러 가지가 그 수 있습니다 :

는 유니 캐스트에서 0의 시작 위치는 나는 현재이 기능을 사용하고 128

했다 더 나은 것을 위해 바뀌지 만, 당분간, 이것은 할 것입니다.

편집 2 :

원본 포트도 필요했습니다. 주소 앞에 4 바이트가 있습니다. 현재 사용중인 새로운 기능 :

#define SRC_ADDR_U 120 
#define SRC_ADDR_M 168 
uint32_t udp_src_addr(void* arg, uint8_t isMulticast){ 
    uint32_t res; 
    uint8_t* tmp = (uint8_t*) arg; 
    uint16_t pos; 
    if(isMulticast) pos = SRC_ADDR_M+4; 
    else pos = SRC_ADDR_U+4; 

    res = (tmp[pos+3] << 24) | (tmp[pos+2] << 16) | (tmp[pos+1] << 8) | tmp[pos]; 
    return res; 
} 
uint16_t udp_src_port(void* arg, uint8_t isMulticast){ 
    uint32_t res; 
    uint8_t* tmp = (uint8_t*) arg; 
    uint16_t pos; 
    if(isMulticast) pos = SRC_ADDR_M; 
    else pos = SRC_ADDR_U; 

    res = (tmp[pos+1] << 8) | tmp[pos]; 
    return res; 
}