Skip to content

Commit

Permalink
feat(ppp): Improve PPP server + client support
Browse files Browse the repository at this point in the history
Make it easier to run a PPP server and client on different interfaces by
adding the interface name to logs and expose the PPP passive option.

The addition of IP_EVENT_PPPD_{GOT,LOST}_IP events make it possible
differentiate between client and server events.
  • Loading branch information
gonzzor committed Feb 24, 2025
1 parent f6ab11e commit 7326fe3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
1 change: 1 addition & 0 deletions components/esp_netif/include/esp_netif_ppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct esp_netif_ppp_config {
esp_ip4_addr_t ppp_their_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */
esp_ip4_addr_t ppp_dns1_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */
esp_ip4_addr_t ppp_dns2_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */
bool ppp_passive; /**< Try once to initiate connection, stay silent if it fails, typically used when we're the PPP server */
#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT
} esp_netif_ppp_config_t;

Expand Down
2 changes: 2 additions & 0 deletions components/esp_netif/include/esp_netif_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ typedef enum {
IP_EVENT_PPP_GOT_IP, /*!< PPP interface got IP */
IP_EVENT_PPP_LOST_IP, /*!< PPP interface lost IP */
IP_EVENT_TX_RX, /*!< transmitting/receiving data packet */
IP_EVENT_PPPD_GOT_IP, /*!< PPP server interface got IP */
IP_EVENT_PPPD_LOST_IP, /*!< PPP server interface lost IP */
} ip_event_t;

/** @brief IP event base declaration */
Expand Down
69 changes: 41 additions & 28 deletions components/esp_netif/lwip/esp_netif_lwip_ppp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct lwip_peer2peer_ctx {
esp_ip4_addr_t ppp_their_ip4_addr; // their desired IP (IPADDR_ANY if no preference)
esp_ip4_addr_t ppp_dns1_addr; // dns server 1 IP (IPADDR_ANY if no preference)
esp_ip4_addr_t ppp_dns2_addr; // dns server 2 IP (IPADDR_ANY if no preference)
bool ppp_passive; // Set ppp_passive() and use ppp_listen()
#endif
ppp_pcb *ppp;
} lwip_peer2peer_ctx_t;
Expand All @@ -50,6 +51,7 @@ typedef struct lwip_peer2peer_ctx {
static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
{
esp_netif_t *netif = ctx;
const char *name = netif->if_desc ? netif->if_desc : "";
ip_event_got_ip_t evt = {
.esp_netif = netif,
};
Expand All @@ -58,59 +60,59 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
assert(obj->base.netif_type == PPP_LWIP_NETIF);
switch (err_code) {
case PPPERR_NONE:
ESP_LOGI(TAG, "Connected");
ESP_LOGI(TAG, "%s: Connected", name);
break;
case PPPERR_PARAM:
ESP_LOGE(TAG, "Invalid parameter");
ESP_LOGE(TAG, "%s: Invalid parameter", name);
break;
case PPPERR_OPEN:
ESP_LOGE(TAG, "Unable to open PPP session");
ESP_LOGE(TAG, "%s: Unable to open PPP session", name);
break;
case PPPERR_DEVICE:
ESP_LOGE(TAG, "Invalid I/O device for PPP");
ESP_LOGE(TAG, "%s: Invalid I/O device for PPP", name);
break;
case PPPERR_ALLOC:
ESP_LOGE(TAG, "Unable to allocate resources");
ESP_LOGE(TAG, "%s: Unable to allocate resources", name);
break;
case PPPERR_USER: /* User interrupt */
ESP_LOGI(TAG, "User interrupt");
ESP_LOGI(TAG, "%s: User interrupt", name);
break;
case PPPERR_CONNECT: /* Connection lost */
ESP_LOGI(TAG, "Connection lost");
ESP_LOGI(TAG, "%s: Connection lost", name);
esp_netif_update_default_netif(netif, ESP_NETIF_LOST_IP);
err = esp_event_post(IP_EVENT, netif->lost_ip_event, &evt, sizeof(evt), 0);

if (ESP_OK != err) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}
return;

case PPPERR_AUTHFAIL:
ESP_LOGE(TAG, "Failed authentication challenge");
ESP_LOGE(TAG, "%s: Failed authentication challenge", name);
break;
case PPPERR_PROTOCOL:
ESP_LOGE(TAG, "Failed to meet protocol");
ESP_LOGE(TAG, "%s: Failed to meet protocol", name);
break;
case PPPERR_PEERDEAD:
ESP_LOGE(TAG, "Connection timeout");
ESP_LOGE(TAG, "%s: Connection timeout", name);
break;
case PPPERR_IDLETIMEOUT:
ESP_LOGE(TAG, "Idle Timeout");
ESP_LOGE(TAG, "%s: Idle Timeout", name);
break;
case PPPERR_CONNECTTIME:
ESP_LOGE(TAG, "Max connect time reached");
ESP_LOGE(TAG, "%s: Max connect time reached", name);
break;
case PPPERR_LOOPBACK:
ESP_LOGE(TAG, "Loopback detected");
ESP_LOGE(TAG, "%s: Loopback detected", name);
break;
default:
ESP_LOGE(TAG, "Unknown error code %d", err_code);
ESP_LOGE(TAG, "%s: Unknown error code %d", name, err_code);
break;
}
if (obj->ppp_error_event_enabled) {
err = esp_event_post(NETIF_PPP_STATUS, err_code, &netif, sizeof(netif), 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}

}
Expand All @@ -126,42 +128,44 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
*/
static void on_ppp_notify_phase(ppp_pcb *pcb, u8_t phase, void *ctx)
{
esp_netif_t *netif = ctx;
const char *name = netif->if_desc ? netif->if_desc : "";

switch (phase) {
case PPP_PHASE_DEAD:
ESP_LOGD(TAG, "Phase Dead");
ESP_LOGD(TAG, "%s: Phase Dead", name);
break;
case PPP_PHASE_INITIALIZE:
ESP_LOGD(TAG, "Phase Start");
ESP_LOGD(TAG, "%s: Phase Start", name);
break;
case PPP_PHASE_ESTABLISH:
ESP_LOGD(TAG, "Phase Establish");
ESP_LOGD(TAG, "%s: Phase Establish", name);
break;
case PPP_PHASE_AUTHENTICATE:
ESP_LOGD(TAG, "Phase Authenticate");
ESP_LOGD(TAG, "%s: Phase Authenticate", name);
break;
case PPP_PHASE_NETWORK:
ESP_LOGD(TAG, "Phase Network");
ESP_LOGD(TAG, "%s: Phase Network", name);
break;
case PPP_PHASE_RUNNING:
ESP_LOGD(TAG, "Phase Running");
ESP_LOGD(TAG, "%s: Phase Running", name);
break;
case PPP_PHASE_TERMINATE:
ESP_LOGD(TAG, "Phase Terminate");
ESP_LOGD(TAG, "%s: Phase Terminate", name);
break;
case PPP_PHASE_DISCONNECT:
ESP_LOGD(TAG, "Phase Disconnect");
ESP_LOGD(TAG, "%s: Phase Disconnect", name);
break;
default:
ESP_LOGW(TAG, "Phase Unknown: %d", phase);
ESP_LOGW(TAG, "%s: Phase Unknown: %d", name, phase);
break;
}
esp_netif_t *netif = ctx;
lwip_peer2peer_ctx_t *obj = (lwip_peer2peer_ctx_t *)netif->related_data;
assert(obj->base.netif_type == PPP_LWIP_NETIF);
if (obj && obj->ppp_phase_event_enabled) {
esp_err_t err = esp_event_post(NETIF_PPP_STATUS, NETIF_PP_PHASE_OFFSET + phase, &netif, sizeof(netif), 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}
}
}
Expand Down Expand Up @@ -281,6 +285,8 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif)
// No need to request DNS servers from peer when providing DNS servers.
ppp_set_usepeerdns(ppp_ctx->ppp, 0);
}

ppp_set_passive(ppp_ctx->ppp, ppp_ctx->ppp_passive);
#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT

#if ESP_IPV6_AUTOCONFIG
Expand All @@ -289,7 +295,12 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif)

ESP_LOGD(TAG, "%s: Starting PPP connection: %p", __func__, ppp_ctx->ppp);
#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT
esp_err_t err = ppp_listen(ppp_ctx->ppp);
esp_err_t err;
if (ppp_ctx->ppp_passive) {
err = ppp_listen(ppp_ctx->ppp);
} else {
err = ppp_connect(ppp_ctx->ppp, 0);
}
#else
err_t err = ppp_connect(ppp_ctx->ppp, 0);
#endif
Expand Down Expand Up @@ -353,6 +364,7 @@ esp_err_t esp_netif_ppp_set_params(esp_netif_t *netif, const esp_netif_ppp_confi
obj->ppp_their_ip4_addr = config->ppp_their_ip4_addr;
obj->ppp_dns1_addr = config->ppp_dns1_addr;
obj->ppp_dns2_addr = config->ppp_dns2_addr;
obj->ppp_passive = config->ppp_passive;
#endif
return ESP_OK;
}
Expand All @@ -374,6 +386,7 @@ esp_err_t esp_netif_ppp_get_params(esp_netif_t *netif, esp_netif_ppp_config_t *c
config->ppp_their_ip4_addr = obj->ppp_their_ip4_addr;
config->ppp_dns1_addr = obj->ppp_dns1_addr;
config->ppp_dns2_addr = obj->ppp_dns2_addr;
config->ppp_passive = obj->ppp_passive;
#endif

return ESP_OK;
Expand Down

0 comments on commit 7326fe3

Please sign in to comment.