PPP: removed ppp_new(), merged with ppp_over_X_create()

The only benefit of ppp_new() call was about having a persistent netif
interface. netif was moved out of PPP pcb so we don't need ppp_new()
anymore, first step in simplifying the weird new/open/free/delete PPP
API.
This commit is contained in:
Sylvain Rochet 2015-02-14 22:10:28 +01:00
parent 3dcfd7ba41
commit 318e752fd6
4 changed files with 128 additions and 135 deletions

View File

@ -38,31 +38,6 @@
#include "lwip/pppapi.h"
#include "lwip/tcpip.h"
/**
* Call ppp_new() inside the tcpip_thread context.
*/
static void
pppapi_do_ppp_new(struct pppapi_msg_msg *msg)
{
msg->ppp = ppp_new(msg->msg.pppnew.pppif);
TCPIP_PPPAPI_ACK(msg);
}
/**
* Call ppp_new() in a thread-safe way by running that function inside the
* tcpip_thread context.
*/
ppp_pcb*
pppapi_new(struct netif *pppif)
{
struct pppapi_msg msg;
msg.function = pppapi_do_ppp_new;
msg.msg.msg.pppnew.pppif = pppif;
TCPIP_PPPAPI(&msg);
return msg.msg.ppp;
}
/**
* Call ppp_set_default() inside the tcpip_thread context.
*/
@ -149,7 +124,7 @@ pppapi_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_pha
static void
pppapi_do_ppp_over_serial_create(struct pppapi_msg_msg *msg)
{
msg->err = ppp_over_serial_create(msg->ppp, msg->msg.serialcreate.fd,
msg->ppp = ppp_over_serial_create(msg->msg.serialcreate.pppif, msg->msg.serialcreate.fd,
msg->msg.serialcreate.link_status_cb, msg->msg.serialcreate.ctx_cb);
TCPIP_PPPAPI_ACK(msg);
}
@ -158,18 +133,18 @@ pppapi_do_ppp_over_serial_create(struct pppapi_msg_msg *msg)
* Call ppp_over_serial_create() in a thread-safe way by running that function inside the
* tcpip_thread context.
*/
int
pppapi_over_serial_create(ppp_pcb *pcb, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb,
ppp_pcb*
pppapi_over_serial_create(struct netif *pppif, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb,
void *ctx_cb)
{
struct pppapi_msg msg;
msg.function = pppapi_do_ppp_over_serial_create;
msg.msg.ppp = pcb;
msg.msg.msg.serialcreate.pppif = pppif;
msg.msg.msg.serialcreate.fd = fd;
msg.msg.msg.serialcreate.link_status_cb = link_status_cb;
msg.msg.msg.serialcreate.ctx_cb = ctx_cb;
TCPIP_PPPAPI(&msg);
return msg.msg.err;
return msg.msg.ppp;
}
#endif /* PPPOS_SUPPORT */
@ -182,7 +157,7 @@ static void
pppapi_do_ppp_over_ethernet_create(struct pppapi_msg_msg *msg)
{
msg->err = ppp_over_ethernet_create(msg->ppp, msg->msg.ethernetcreate.ethif,
msg->ppp = ppp_over_ethernet_create(msg->msg.ethernetcreate.pppif, msg->msg.ethernetcreate.ethif,
msg->msg.ethernetcreate.service_name, msg->msg.ethernetcreate.concentrator_name,
msg->msg.ethernetcreate.link_status_cb, msg->msg.ethernetcreate.ctx_cb);
TCPIP_PPPAPI_ACK(msg);
@ -192,21 +167,21 @@ pppapi_do_ppp_over_ethernet_create(struct pppapi_msg_msg *msg)
* Call ppp_over_ethernet_create() in a thread-safe way by running that function inside the
* tcpip_thread context.
*/
int
pppapi_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *service_name,
ppp_pcb*
pppapi_over_ethernet_create(struct netif *pppif, struct netif *ethif, const char *service_name,
const char *concentrator_name, ppp_link_status_cb_fn link_status_cb,
void *ctx_cb)
{
struct pppapi_msg msg;
msg.function = pppapi_do_ppp_over_ethernet_create;
msg.msg.ppp = pcb;
msg.msg.msg.ethernetcreate.pppif = pppif;
msg.msg.msg.ethernetcreate.ethif = ethif;
msg.msg.msg.ethernetcreate.service_name = service_name;
msg.msg.msg.ethernetcreate.concentrator_name = concentrator_name;
msg.msg.msg.ethernetcreate.link_status_cb = link_status_cb;
msg.msg.msg.ethernetcreate.ctx_cb = ctx_cb;
TCPIP_PPPAPI(&msg);
return msg.msg.err;
return msg.msg.ppp;
}
#endif /* PPPOE_SUPPORT */
@ -218,7 +193,7 @@ pppapi_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *servi
static void
pppapi_do_ppp_over_l2tp_create(struct pppapi_msg_msg *msg)
{
msg->err = ppp_over_l2tp_create(msg->ppp,
msg->ppp = ppp_over_l2tp_create(msg->msg.l2tpcreate.pppif,
msg->msg.l2tpcreate.netif, msg->msg.l2tpcreate.ipaddr, msg->msg.l2tpcreate.port,
#if PPPOL2TP_AUTH_SUPPORT
msg->msg.l2tpcreate.secret,
@ -234,14 +209,14 @@ pppapi_do_ppp_over_l2tp_create(struct pppapi_msg_msg *msg)
* Call ppp_over_l2tp_create() in a thread-safe way by running that function inside the
* tcpip_thread context.
*/
int
pppapi_over_l2tp_create(ppp_pcb *pcb, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
ppp_pcb*
pppapi_over_l2tp_create(struct netif *pppif, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
{
struct pppapi_msg msg;
msg.function = pppapi_do_ppp_over_l2tp_create;
msg.msg.ppp = pcb;
msg.msg.msg.l2tpcreate.pppif = pppif;
msg.msg.msg.l2tpcreate.netif = netif;
msg.msg.msg.l2tpcreate.ipaddr = ipaddr;
msg.msg.msg.l2tpcreate.port = port;
@ -252,7 +227,7 @@ pppapi_over_l2tp_create(ppp_pcb *pcb, struct netif *netif, ip_addr_t *ipaddr, u1
msg.msg.msg.l2tpcreate.link_status_cb = link_status_cb;
msg.msg.msg.l2tpcreate.ctx_cb = ctx_cb;
TCPIP_PPPAPI(&msg);
return msg.msg.err;
return msg.msg.ppp;
}
#endif /* PPPOL2TP_SUPPORT */

View File

@ -33,6 +33,7 @@
#if LWIP_PPP_API /* don't build if not configured for use in lwipopts.h */
#include "lwip/sys.h"
#include "lwip/netif.h"
#include "netif/ppp/ppp.h"
#ifdef __cplusplus
@ -46,9 +47,6 @@ struct pppapi_msg_msg {
int err;
ppp_pcb *ppp;
union {
struct {
struct netif *pppif;
} pppnew;
struct {
u8_t authtype;
const char *user;
@ -61,6 +59,7 @@ struct pppapi_msg_msg {
#endif /* PPP_NOTIFY_PHASE */
#if PPPOS_SUPPORT
struct {
struct netif *pppif;
sio_fd_t fd;
ppp_link_status_cb_fn link_status_cb;
void *ctx_cb;
@ -68,6 +67,7 @@ struct pppapi_msg_msg {
#endif /* PPPOS_SUPPORT */
#if PPPOE_SUPPORT
struct {
struct netif *pppif;
struct netif *ethif;
const char *service_name;
const char *concentrator_name;
@ -77,6 +77,7 @@ struct pppapi_msg_msg {
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
struct {
struct netif *pppif;
struct netif *netif;
ip_addr_t *ipaddr;
u16_t port;
@ -114,22 +115,21 @@ struct pppapi_msg {
};
/* API for application */
ppp_pcb *pppapi_new(struct netif *pppif);
void pppapi_set_default(ppp_pcb *pcb);
void pppapi_set_auth(ppp_pcb *pcb, u8_t authtype, const char *user, const char *passwd);
#if PPP_NOTIFY_PHASE
void pppapi_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_phase_cb);
#endif /* PPP_NOTIFY_PHASE */
#if PPPOS_SUPPORT
int pppapi_over_serial_create(ppp_pcb *pcb, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
ppp_pcb *pppapi_over_serial_create(struct netif *pppif, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
#endif /* PPPOS_SUPPORT */
#if PPPOE_SUPPORT
int pppapi_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *service_name,
ppp_pcb *pppapi_over_ethernet_create(struct netif *pppif, struct netif *ethif, const char *service_name,
const char *concentrator_name, ppp_link_status_cb_fn link_status_cb,
void *ctx_cb);
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
int pppapi_over_l2tp_create(ppp_pcb *pcb, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
ppp_pcb *pppapi_over_l2tp_create(struct netif *pppif, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
#endif /* PPPOL2TP_SUPPORT */

View File

@ -469,17 +469,6 @@ struct ppp_pcb_s {
*** PUBLIC FUNCTIONS ***
************************/
/*
* Create a new PPP session.
*
* This initializes the PPP control block but does not
* attempt to negotiate the LCP session.
*
* Return a new PPP connection control block pointer
* on success or a null pointer on failure.
*/
ppp_pcb *ppp_new(struct netif *pppif);
/*
* Set a PPP interface as the default network interface
* (used to output all packets for which no specific route is found).
@ -540,7 +529,7 @@ typedef void (*ppp_link_status_cb_fn)(ppp_pcb *pcb, int err_code, void *ctx);
*
* Return 0 on success, an error code on failure.
*/
int ppp_over_serial_create(ppp_pcb *pcb, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
ppp_pcb *ppp_over_serial_create(struct netif *pppif, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
#endif /* PPPOS_SUPPORT */
#if PPPOE_SUPPORT
@ -549,7 +538,7 @@ int ppp_over_serial_create(ppp_pcb *pcb, sio_fd_t fd, ppp_link_status_cb_fn link
*
* Return 0 on success, an error code on failure.
*/
int ppp_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *service_name, const char *concentrator_name,
ppp_pcb *ppp_over_ethernet_create(struct netif *pppif, struct netif *ethif, const char *service_name, const char *concentrator_name,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
#endif /* PPPOE_SUPPORT */
@ -557,7 +546,7 @@ int ppp_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *serv
/*
* Create a new PPP Over L2TP (PPPoL2TP) connection.
*/
int ppp_over_l2tp_create(ppp_pcb *pcb, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
ppp_pcb *ppp_over_l2tp_create(struct netif *pppif, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb);
#endif /* PPPOL2TP_SUPPORT */

View File

@ -183,6 +183,7 @@ const struct protent* const protocols[] = {
};
/* Prototypes for procedures local to this file. */
static ppp_pcb *ppp_new(struct netif *pppif);
static void ppp_clear(ppp_pcb *pcb);
static void ppp_do_open(void *arg);
static void ppp_start(ppp_pcb *pcb); /** Initiate LCP open request */
@ -236,69 +237,6 @@ int ppp_init(void) {
return 0;
}
/* Create a new PPP session. */
ppp_pcb *ppp_new(struct netif *pppif) {
ppp_pcb *pcb;
pcb = (ppp_pcb*)memp_malloc(MEMP_PPP_PCB);
if (pcb == NULL) {
return NULL;
}
memset(pcb, 0, sizeof(ppp_pcb));
#if PPP_DEBUG
pcb->num = ppp_num++;
#endif /* PPP_DEBUG */
/* default configuration */
pcb->settings.usepeerdns = 1;
#if PAP_SUPPORT
pcb->settings.pap_timeout_time = UPAP_DEFTIMEOUT;
pcb->settings.pap_max_transmits = UPAP_DEFTRANSMITS;
#if PPP_SERVER
pcb->settings.pap_req_timeout = UPAP_DEFREQTIME;
#endif /* PPP_SERVER */
#endif /* PAP_SUPPORT */
#if CHAP_SUPPORT
pcb->settings.chap_timeout_time = CHAP_DEFTIMEOUT;
pcb->settings.chap_max_transmits = CHAP_DEFTRANSMITS;
#if PPP_SERVER
pcb->settings.chap_rechallenge_time = CHAP_DEFREQTIME;
#endif /* PPP_SERVER */
#endif /* CHAP_SUPPPORT */
#if EAP_SUPPORT
pcb->settings.eap_req_time = EAP_DEFREQTIME;
pcb->settings.eap_allow_req = EAP_DEFALLOWREQ;
#if PPP_SERVER
pcb->settings.eap_timeout_time = EAP_DEFTIMEOUT;
pcb->settings.eap_max_transmits = EAP_DEFTRANSMITS;
#endif /* PPP_SERVER */
#endif /* EAP_SUPPORT */
pcb->settings.lcp_loopbackfail = LCP_DEFLOOPBACKFAIL;
pcb->settings.lcp_echo_interval = LCP_ECHOINTERVAL;
pcb->settings.lcp_echo_fails = LCP_MAXECHOFAILS;
pcb->settings.fsm_timeout_time = FSM_DEFTIMEOUT;
pcb->settings.fsm_max_conf_req_transmits = FSM_DEFMAXCONFREQS;
pcb->settings.fsm_max_term_transmits = FSM_DEFMAXTERMREQS;
pcb->settings.fsm_max_nak_loops = FSM_DEFMAXNAKLOOPS;
pcb->netif = pppif;
if (!netif_add(pcb->netif, &pcb->addrs.our_ipaddr, &pcb->addrs.netmask,
&pcb->addrs.his_ipaddr, (void *)pcb, ppp_netif_init_cb, NULL)) {
memp_free(MEMP_PPP_PCB, pcb);
PPPDEBUG(LOG_ERR, ("ppp_new[%d]: netif_add failed\n", pcb->num));
return NULL;
}
new_phase(pcb, PPP_PHASE_DEAD);
return pcb;
}
void ppp_set_default(ppp_pcb *pcb) {
netif_set_default(pcb->netif);
}
@ -355,18 +293,24 @@ void ppp_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_p
#endif /* PPP_NOTIFY_PHASE */
#if PPPOS_SUPPORT
int ppp_over_serial_create(ppp_pcb *pcb, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
ppp_pcb *ppp_over_serial_create(struct netif *pppif, sio_fd_t fd, ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
ppp_pcb *pcb;
/* PPP is single-threaded: without a callback,
* there is no way to know when the link is up. */
if (link_status_cb == NULL) {
return PPPERR_PARAM;
return NULL;
}
pcb = ppp_new(pppif);
if (pppif == NULL) {
return NULL;
}
pcb->fd = fd;
pcb->link_status_cb = link_status_cb;
pcb->ctx_cb = ctx_cb;
return PPPERR_NONE;
return pcb;
}
/*
@ -386,8 +330,9 @@ void ppp_set_xaccm(ppp_pcb *pcb, ext_accm *accm) {
#if PPPOE_SUPPORT
static void ppp_over_ethernet_link_status_cb(ppp_pcb *pcb, int state);
int ppp_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *service_name, const char *concentrator_name,
ppp_pcb *ppp_over_ethernet_create(struct netif *pppif, struct netif *ethif, const char *service_name, const char *concentrator_name,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
ppp_pcb *pcb;
LWIP_UNUSED_ARG(service_name);
LWIP_UNUSED_ARG(concentrator_name);
@ -395,41 +340,54 @@ int ppp_over_ethernet_create(ppp_pcb *pcb, struct netif *ethif, const char *serv
/* PPP is single-threaded: without a callback,
* there is no way to know when the link is up. */
if (link_status_cb == NULL) {
return PPPERR_PARAM;
return NULL;
}
pcb = ppp_new(pppif);
if (pppif == NULL) {
return NULL;
}
pcb->link_status_cb = link_status_cb;
pcb->ctx_cb = ctx_cb;
if (pppoe_create(ethif, pcb, ppp_over_ethernet_link_status_cb, &pcb->pppoe_sc) != ERR_OK) {
return PPPERR_OPEN;
ppp_delete(pcb);
return NULL;
}
return PPPERR_NONE;
return pcb;
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static void ppp_over_l2tp_link_status_cb(ppp_pcb *pcb, int state);
int ppp_over_l2tp_create(ppp_pcb *pcb, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
ppp_pcb *ppp_over_l2tp_create(struct netif *pppif, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
ppp_pcb *pcb;
/* PPP is single-threaded: without a callback,
* there is no way to know when the link is up. */
if (link_status_cb == NULL) {
return PPPERR_PARAM;
return NULL;
}
pcb = ppp_new(pppif);
if (pppif == NULL) {
return NULL;
}
pcb->link_status_cb = link_status_cb;
pcb->ctx_cb = ctx_cb;
if (pppol2tp_create(pcb, ppp_over_l2tp_link_status_cb, &pcb->l2tp_pcb, netif, ipaddr, port, secret, secret_len) != ERR_OK) {
return PPPERR_OPEN;
ppp_delete(pcb);
return NULL;
}
return PPPERR_NONE;
return pcb;
}
#endif /* PPPOL2TP_SUPPORT */
@ -596,6 +554,77 @@ int ppp_delete(ppp_pcb *pcb) {
/*** PRIVATE FUNCTION DEFINITIONS ***/
/************************************/
/*
* Create a new PPP session.
*
* This initializes the PPP control block but does not
* attempt to negotiate the LCP session.
*
* Return a new PPP connection control block pointer
* on success or a null pointer on failure.
*/
static ppp_pcb *ppp_new(struct netif *pppif) {
ppp_pcb *pcb;
pcb = (ppp_pcb*)memp_malloc(MEMP_PPP_PCB);
if (pcb == NULL) {
return NULL;
}
memset(pcb, 0, sizeof(ppp_pcb));
#if PPP_DEBUG
pcb->num = ppp_num++;
#endif /* PPP_DEBUG */
/* default configuration */
pcb->settings.usepeerdns = 1;
#if PAP_SUPPORT
pcb->settings.pap_timeout_time = UPAP_DEFTIMEOUT;
pcb->settings.pap_max_transmits = UPAP_DEFTRANSMITS;
#if PPP_SERVER
pcb->settings.pap_req_timeout = UPAP_DEFREQTIME;
#endif /* PPP_SERVER */
#endif /* PAP_SUPPORT */
#if CHAP_SUPPORT
pcb->settings.chap_timeout_time = CHAP_DEFTIMEOUT;
pcb->settings.chap_max_transmits = CHAP_DEFTRANSMITS;
#if PPP_SERVER
pcb->settings.chap_rechallenge_time = CHAP_DEFREQTIME;
#endif /* PPP_SERVER */
#endif /* CHAP_SUPPPORT */
#if EAP_SUPPORT
pcb->settings.eap_req_time = EAP_DEFREQTIME;
pcb->settings.eap_allow_req = EAP_DEFALLOWREQ;
#if PPP_SERVER
pcb->settings.eap_timeout_time = EAP_DEFTIMEOUT;
pcb->settings.eap_max_transmits = EAP_DEFTRANSMITS;
#endif /* PPP_SERVER */
#endif /* EAP_SUPPORT */
pcb->settings.lcp_loopbackfail = LCP_DEFLOOPBACKFAIL;
pcb->settings.lcp_echo_interval = LCP_ECHOINTERVAL;
pcb->settings.lcp_echo_fails = LCP_MAXECHOFAILS;
pcb->settings.fsm_timeout_time = FSM_DEFTIMEOUT;
pcb->settings.fsm_max_conf_req_transmits = FSM_DEFMAXCONFREQS;
pcb->settings.fsm_max_term_transmits = FSM_DEFMAXTERMREQS;
pcb->settings.fsm_max_nak_loops = FSM_DEFMAXNAKLOOPS;
pcb->netif = pppif;
if (!netif_add(pcb->netif, &pcb->addrs.our_ipaddr, &pcb->addrs.netmask,
&pcb->addrs.his_ipaddr, (void *)pcb, ppp_netif_init_cb, NULL)) {
memp_free(MEMP_PPP_PCB, pcb);
PPPDEBUG(LOG_ERR, ("ppp_new[%d]: netif_add failed\n", pcb->num));
return NULL;
}
new_phase(pcb, PPP_PHASE_DEAD);
return pcb;
}
/* Set a PPP PCB to its initial state */
static void ppp_clear(ppp_pcb *pcb) {
const struct protent *protp;