From 653657ae43be92b402bc082917ffb35416e19204 Mon Sep 17 00:00:00 2001 From: Sylvain Rochet Date: Sun, 22 Feb 2015 16:45:38 +0100 Subject: [PATCH] PPP, code cleaning --- src/include/netif/ppp/ppp_impl.h | 50 ++++++++++++++++++-------------- src/netif/ppp/ppp.c | 49 +++++++++++++++---------------- src/netif/ppp/pppos.c | 10 +++---- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/include/netif/ppp/ppp_impl.h b/src/include/netif/ppp/ppp_impl.h index f6cee65a..cf04c0f8 100644 --- a/src/include/netif/ppp/ppp_impl.h +++ b/src/include/netif/ppp/ppp_impl.h @@ -380,20 +380,31 @@ struct pppd_stats { #endif /* PPP_STATS_SUPPORT */ -/* PPP functions +/* + * PPP private functions */ + +/* + * Functions called from lwIP core. + */ + /* initialize the PPP subsystem */ int ppp_init(void); + +/* + * Functions called from PPP link protocols. + */ + /* Create a new PPP control block */ ppp_pcb *ppp_new(struct netif *pppif, ppp_link_status_cb_fn link_status_cb, void *ctx_cb); -/* Set a PPP PCB to its initial state */ -void ppp_clear(ppp_pcb *pcb); - /* Set link callback functions */ void ppp_link_set_callbacks(ppp_pcb *pcb, const struct link_callbacks *callbacks, void *ctx); +/* Set a PPP PCB to its initial state */ +void ppp_clear(ppp_pcb *pcb); + /* Initiate LCP open request */ void ppp_start(ppp_pcb *pcb); @@ -403,22 +414,23 @@ void ppp_link_failed(ppp_pcb *pcb); /* Called when link is normally down (i.e. it was asked to end) */ void ppp_link_end(ppp_pcb *pcb); -/* function called by pppoe.c */ +/* function called to process input packet */ void ppp_input(ppp_pcb *pcb, struct pbuf *pb); +/* helper function, merge a pbuf chain into one pbuf */ +struct pbuf *ppp_singlebuf(struct pbuf *p); + + +/* + * Functions called by PPP protocols. + */ + /* function called by all PPP subsystems to send packets */ int ppp_write(ppp_pcb *pcb, struct pbuf *p); /* functions called by auth.c link_terminated() */ void ppp_link_terminated(ppp_pcb *pcb); -/* merge a pbuf chain into one pbuf */ -struct pbuf * ppp_singlebuf(struct pbuf *p); - - -/* Functions called by various PPP subsystems to configure - * the PPP interface or change the PPP phase. - */ void new_phase(ppp_pcb *pcb, int p); int ppp_send_config(ppp_pcb *pcb, int mtu, u32_t accm, int pcomp, int accomp); @@ -456,15 +468,13 @@ int get_idle_time(ppp_pcb *pcb, struct ppp_idle *ip); int get_loop_output(void); -u32_t get_mask (u32_t addr); - +u32_t get_mask(u32_t addr); /* Optional protocol names list, to make our messages a little more informative. */ #if PPP_PROTOCOLNAME const char * protocol_name(int proto); #endif /* PPP_PROTOCOLNAME */ - /* Optional stats support, to get some statistics on the PPP interface */ #if PPP_STATS_SUPPORT void print_link_stats(void); /* Print stats, if available */ @@ -486,8 +496,6 @@ void update_link_stats(int u); /* Get stats at link termination */ #define PUTCHAR(c, cp) { \ *(cp)++ = (u_char) (c); \ } - - #define GETSHORT(s, cp) { \ (s) = *(cp)++ << 8; \ (s) |= *(cp)++; \ @@ -496,7 +504,6 @@ void update_link_stats(int u); /* Get stats at link termination */ *(cp)++ = (u_char) ((s) >> 8); \ *(cp)++ = (u_char) (s); \ } - #define GETLONG(l, cp) { \ (l) = *(cp)++ << 8; \ (l) |= *(cp)++; (l) <<= 8; \ @@ -516,9 +523,9 @@ void update_link_stats(int u); /* Get stats at link termination */ /* * System dependent definitions for user-level 4.3BSD UNIX implementation. */ -#define TIMEOUT(f, a, t) do { sys_untimeout((f), (a)); sys_timeout((t)*1000, (f), (a)); } while(0) -#define TIMEOUTMS(f, a, t) do { sys_untimeout((f), (a)); sys_timeout((t), (f), (a)); } while(0) -#define UNTIMEOUT(f, a) sys_untimeout((f), (a)) +#define TIMEOUT(f, a, t) do { sys_untimeout((f), (a)); sys_timeout((t)*1000, (f), (a)); } while(0) +#define TIMEOUTMS(f, a, t) do { sys_untimeout((f), (a)); sys_timeout((t), (f), (a)); } while(0) +#define UNTIMEOUT(f, a) sys_untimeout((f), (a)) #define BZERO(s, n) memset(s, 0, n) #define BCMP(s1, s2, l) memcmp(s1, s2, l) @@ -610,5 +617,4 @@ void ppp_dump_packet(const char *tag, unsigned char *p, int len); #endif /* PPP_IMP_H_ */ - #endif /* PPP_SUPPORT */ diff --git a/src/netif/ppp/ppp.c b/src/netif/ppp/ppp.c index 0834f84d..204abd8d 100644 --- a/src/netif/ppp/ppp.c +++ b/src/netif/ppp/ppp.c @@ -590,6 +590,11 @@ ppp_pcb *ppp_new(struct netif *pppif, ppp_link_status_cb_fn link_status_cb, void return pcb; } +void ppp_link_set_callbacks(ppp_pcb *pcb, const struct link_callbacks *callbacks, void *ctx) { + pcb->link_cb = callbacks; + pcb->link_ctx_cb = ctx; +} + /* Set a PPP PCB to its initial state */ void ppp_clear(ppp_pcb *pcb) { const struct protent *protp; @@ -616,11 +621,6 @@ void ppp_clear(ppp_pcb *pcb) { new_phase(pcb, PPP_PHASE_INITIALIZE); } -void ppp_link_set_callbacks(ppp_pcb *pcb, const struct link_callbacks *callbacks, void *ctx) { - pcb->link_cb = callbacks; - pcb->link_ctx_cb = ctx; -} - /** Initiate LCP open request */ void ppp_start(ppp_pcb *pcb) { PPPDEBUG(LOG_DEBUG, ("ppp_start: unit %d\n", pcb->netif->num)); @@ -780,27 +780,8 @@ out: return; } - -/* - * Write a pbuf to a ppp link, only used from PPP functions - * to send PPP packets. - * - * IPv4 and IPv6 packets from lwIP are sent, respectively, - * with ppp_netif_output_ip4() and ppp_netif_output_ip6() - * functions (which are callbacks of the netif PPP interface). - * - * RETURN: >= 0 Number of characters written - * -1 Failed to write to device - */ -int ppp_write(ppp_pcb *pcb, struct pbuf *p) { -#if PRINTPKT_SUPPORT - ppp_dump_packet("sent", (unsigned char *)p->payload+2, p->len-2); -#endif /* PRINTPKT_SUPPORT */ - return pcb->link_cb->write(pcb, pcb->link_ctx_cb, p); -} - /* merge a pbuf chain into one pbuf */ -struct pbuf * ppp_singlebuf(struct pbuf *p) { +struct pbuf *ppp_singlebuf(struct pbuf *p) { struct pbuf *q, *b; u_char *pl; @@ -825,6 +806,24 @@ struct pbuf * ppp_singlebuf(struct pbuf *p) { return q; } +/* + * Write a pbuf to a ppp link, only used from PPP functions + * to send PPP packets. + * + * IPv4 and IPv6 packets from lwIP are sent, respectively, + * with ppp_netif_output_ip4() and ppp_netif_output_ip6() + * functions (which are callbacks of the netif PPP interface). + * + * RETURN: >= 0 Number of characters written + * -1 Failed to write to device + */ +int ppp_write(ppp_pcb *pcb, struct pbuf *p) { +#if PRINTPKT_SUPPORT + ppp_dump_packet("sent", (unsigned char *)p->payload+2, p->len-2); +#endif /* PRINTPKT_SUPPORT */ + return pcb->link_cb->write(pcb, pcb->link_ctx_cb, p); +} + void ppp_link_terminated(ppp_pcb *pcb) { PPPDEBUG(LOG_DEBUG, ("ppp_link_terminated: unit %d\n", pcb->netif->num)); pcb->link_cb->disconnect(pcb, pcb->link_ctx_cb); diff --git a/src/netif/ppp/pppos.c b/src/netif/ppp/pppos.c index b51c4a2f..50aed3b8 100644 --- a/src/netif/ppp/pppos.c +++ b/src/netif/ppp/pppos.c @@ -244,8 +244,8 @@ pppos_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) * Otherwise send it. */ if (!tail) { PPPDEBUG(LOG_WARNING, - ("ppp_write[%d]: Alloc err - dropping pbuf len=%d\n", ppp->netif->num, head->len)); - /*"ppp_write[%d]: Alloc err - dropping %d:%.*H", pd, head->len, LWIP_MIN(head->len * 2, 40), head->payload)); */ + ("pppos_write[%d]: Alloc err - dropping pbuf len=%d\n", ppp->netif->num, head->len)); + /*"pppos_write[%d]: Alloc err - dropping %d:%.*H", pd, head->len, LWIP_MIN(head->len * 2, 40), head->payload)); */ pbuf_free(head); LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.proterr); @@ -254,8 +254,8 @@ pppos_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) return ERR_MEM; } - PPPDEBUG(LOG_INFO, ("ppp_write[%d]: len=%d\n", ppp->netif->num, head->len)); - /* "ppp_write[%d]: %d:%.*H", pd, head->len, LWIP_MIN(head->len * 2, 40), head->payload)); */ + PPPDEBUG(LOG_INFO, ("pppos_write[%d]: len=%d\n", ppp->netif->num, head->len)); + /* "pppos_write[%d]: %d:%.*H", pd, head->len, LWIP_MIN(head->len * 2, 40), head->payload)); */ pppos_xmit(pppos, head); pbuf_free(p); return ERR_OK; @@ -426,7 +426,7 @@ pppos_disconnect(ppp_pcb *ppp, void *ctx) { LWIP_UNUSED_ARG(ctx); - /* We cannot call ppp_free_current_input_packet() here because + /* We cannot call pppos_free_current_input_packet() here because * rx thread might still call pppos_input() */ ppp_link_end(ppp); /* notify upper layers */