Commit 49bbfaf1 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Fix mg_lwip_if_get_conn_addr when there's no pcb

Connection may not (yet) have a TCP or UDP PCB associated with it.
mg_lwip_if_get_conn_addr should not crash in this case.

PUBLISHED_FROM=fd0ed683b18b3f549135c9d79eeadfc7348ab05a
parent cd9c7dcb
...@@ -14562,22 +14562,21 @@ void mg_lwip_if_destroy_conn(struct mg_connection *nc) { ...@@ -14562,22 +14562,21 @@ void mg_lwip_if_destroy_conn(struct mg_connection *nc) {
void mg_lwip_if_get_conn_addr(struct mg_connection *nc, int remote, void mg_lwip_if_get_conn_addr(struct mg_connection *nc, int remote,
union socket_address *sa) { union socket_address *sa) {
memset(sa, 0, sizeof(*sa)); memset(sa, 0, sizeof(*sa));
if (nc->sock == INVALID_SOCKET) return; if (nc == NULL || nc->sock == INVALID_SOCKET) return;
struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock; struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
if (nc->flags & MG_F_UDP) { if (nc->flags & MG_F_UDP) {
struct udp_pcb *upcb = cs->pcb.udp; struct udp_pcb *upcb = cs->pcb.udp;
if (remote) { if (remote) {
memcpy(sa, &nc->sa, sizeof(*sa)); memcpy(sa, &nc->sa, sizeof(*sa));
} else { } else if (upcb != NULL) {
sa->sin.sin_port = htons(upcb->local_port); sa->sin.sin_port = htons(upcb->local_port);
SET_ADDR(sa, &upcb->local_ip); SET_ADDR(sa, &upcb->local_ip);
} }
} else { } else {
struct tcp_pcb *tpcb = cs->pcb.tcp; struct tcp_pcb *tpcb = cs->pcb.tcp;
if (remote) { if (remote) {
sa->sin.sin_port = htons(tpcb->remote_port); memcpy(sa, &nc->sa, sizeof(*sa));
SET_ADDR(sa, &tpcb->remote_ip); } else if (tpcb != NULL) {
} else {
sa->sin.sin_port = htons(tpcb->local_port); sa->sin.sin_port = htons(tpcb->local_port);
SET_ADDR(sa, &tpcb->local_ip); SET_ADDR(sa, &tpcb->local_ip);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment