NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Revision:
4:fd826cad83c0
Parent:
0:632c9925f013
diff -r 95e0bc00a1bb -r fd826cad83c0 lwip/netif/ppp/ppp_oe.c
--- a/lwip/netif/ppp/ppp_oe.c	Fri Jun 18 10:38:57 2010 +0000
+++ b/lwip/netif/ppp/ppp_oe.c	Fri Jul 09 14:46:47 2010 +0000
@@ -72,98 +72,17 @@
 
 #if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
 
+#include "netif/ppp_oe.h"
+
 #include "ppp.h"
 #include "pppdebug.h"
 
 #include "lwip/timers.h"
-
-#include "netif/ppp_oe.h"
+#include "lwip/memp.h"
 
 #include <string.h>
 #include <stdio.h>
 
-/** @todo Replace this part with a simple list like other lwIP lists */
-#ifndef _SYS_QUEUE_H_
-#define _SYS_QUEUE_H_
-
-/*
- * A list is headed by a single forward pointer (or an array of forward
- * pointers for a hash table header). The elements are doubly linked
- * so that an arbitrary element can be removed without a need to
- * traverse the list. New elements can be added to the list before
- * or after an existing element or at the head of the list. A list
- * may only be traversed in the forward direction.
- *
- * For details on the use of these macros, see the queue(3) manual page.
- */
-
-/*
- * List declarations.
- */
-#define  LIST_HEAD(name, type)                                                 \
-struct name {                                                                  \
-  struct type *lh_first;  /* first element */                                  \
-}
-
-#define  LIST_HEAD_INITIALIZER(head)                                           \
-  { NULL }
-
-#define  LIST_ENTRY(type)                                                      \
-struct {                                                                       \
-  struct type *le_next;  /* next element */                                    \
-  struct type **le_prev; /* address of previous next element */                \
-}
-
-/*
- * List functions.
- */
-
-#define  LIST_EMPTY(head)  ((head)->lh_first == NULL)
-
-#define  LIST_FIRST(head)  ((head)->lh_first)
-
-#define  LIST_FOREACH(var, head, field)                                        \
-  for ((var) = LIST_FIRST((head));                                             \
-      (var);                                                                   \
-      (var) = LIST_NEXT((var), field))
-
-#define  LIST_INIT(head) do {                                                  \
-  LIST_FIRST((head)) = NULL;                                                   \
-} while (0)
-
-#define  LIST_INSERT_AFTER(listelm, elm, field) do {                           \
-  if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)         \
-    LIST_NEXT((listelm), field)->field.le_prev =                               \
-        &LIST_NEXT((elm), field);                                              \
-  LIST_NEXT((listelm), field) = (elm);                                         \
-  (elm)->field.le_prev = &LIST_NEXT((listelm), field);                         \
-} while (0)
-
-#define  LIST_INSERT_BEFORE(listelm, elm, field) do {                          \
-  (elm)->field.le_prev = (listelm)->field.le_prev;                             \
-  LIST_NEXT((elm), field) = (listelm);                                         \
-  *(listelm)->field.le_prev = (elm);                                           \
-  (listelm)->field.le_prev = &LIST_NEXT((elm), field);                         \
-} while (0)
-
-#define  LIST_INSERT_HEAD(head, elm, field) do {                               \
-  if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)                  \
-    LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);              \
-  LIST_FIRST((head)) = (elm);                                                  \
-  (elm)->field.le_prev = &LIST_FIRST((head));                                  \
-} while (0)
-
-#define  LIST_NEXT(elm, field)  ((elm)->field.le_next)
-
-#define  LIST_REMOVE(elm, field) do {                                          \
-  if (LIST_NEXT((elm), field) != NULL)                                         \
-    LIST_NEXT((elm), field)->field.le_prev =                                   \
-        (elm)->field.le_prev;                                                  \
-  *(elm)->field.le_prev = LIST_NEXT((elm), field);                             \
-} while (0)
-
-#endif /* !_SYS_QUEUE_H_ */
-
 
 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
 #define PPPOE_ADD_16(PTR, VAL) \
@@ -188,27 +107,11 @@
 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
 #endif
 
-struct pppoe_softc {
-  LIST_ENTRY(pppoe_softc) sc_list;
-  struct netif *sc_ethif;      /* ethernet interface we are using */
-  int sc_pd;                   /* ppp unit number */
-  void (*sc_linkStatusCB)(int pd, int up);
-
-  int sc_state;                /* discovery phase or session connected */
-  struct eth_addr sc_dest;     /* hardware address of concentrator */
-  u16_t sc_session;            /* PPPoE session id */
+#ifndef PPPOE_ERRORSTRING_LEN
+#define PPPOE_ERRORSTRING_LEN     64
+#endif
+static char pppoe_error_tmp[PPPOE_ERRORSTRING_LEN];
 
-  char *sc_service_name;       /* if != NULL: requested name of service */
-  char *sc_concentrator_name;  /* if != NULL: requested concentrator id */
-  u8_t *sc_ac_cookie;          /* content of AC cookie we must echo back */
-  size_t sc_ac_cookie_len;     /* length of cookie data */
-#ifdef PPPOE_SERVER
-  u8_t *sc_hunique;            /* content of host unique we must echo back */
-  size_t sc_hunique_len;       /* length of host unique */
-#endif
-  int sc_padi_retried;         /* number of PADI retries already done */
-  int sc_padr_retried;         /* number of PADR retries already done */
-};
 
 /* input routines */
 static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);
@@ -234,21 +137,16 @@
 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);
 static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);
 
-static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
-
-void
-pppoe_init(void)
-{
-  LIST_INIT(&pppoe_softc_list);
-}
+/** linked list of created pppoe interfaces */
+static struct pppoe_softc *pppoe_softc_list;
 
 err_t
 pppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr)
 {
   struct pppoe_softc *sc;
 
-  sc = mem_malloc(sizeof(struct pppoe_softc));
-  if(!sc) {
+  sc = (struct pppoe_softc *)memp_malloc(MEMP_PPPOE_IF);
+  if (sc == NULL) {
     *scptr = NULL;
     return ERR_MEM;
   }
@@ -261,7 +159,9 @@
   sc->sc_linkStatusCB = linkStatusCB;
   sc->sc_ethif = ethif;
 
-  LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
+  /* put the new interface at the head of the list */
+  sc->next = pppoe_softc_list;
+  pppoe_softc_list = sc;
 
   *scptr = sc;
 
@@ -271,9 +171,9 @@
 err_t
 pppoe_destroy(struct netif *ifp)
 {
-  struct pppoe_softc * sc;
+  struct pppoe_softc *sc, *prev = NULL;
 
-  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
+  for (sc = pppoe_softc_list; sc != NULL; prev = sc, sc = sc->next) {
     if (sc->sc_ethif == ifp) {
       break;
     }
@@ -284,18 +184,23 @@
   }
 
   sys_untimeout(pppoe_timeout, sc);
-  LIST_REMOVE(sc, sc_list);
+  if (prev == NULL) {
+    /* remove sc from the head of the list */
+    pppoe_softc_list = sc->next;
+  } else {
+    /* remove sc from the list */
+    prev->next = sc->next;
+  }
 
+#ifdef PPPOE_TODO
   if (sc->sc_concentrator_name) {
     mem_free(sc->sc_concentrator_name);
   }
   if (sc->sc_service_name) {
     mem_free(sc->sc_service_name);
   }
-  if (sc->sc_ac_cookie) {
-    mem_free(sc->sc_ac_cookie);
-  }
-  mem_free(sc);
+#endif /* PPPOE_TODO */
+  memp_free(MEMP_PPPOE_IF, sc);
 
   return ERR_OK;
 }
@@ -315,7 +220,7 @@
     return NULL;
   }
 
-  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
+  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
     if (sc->sc_state == PPPOE_STATE_SESSION
         && sc->sc_session == session) {
       if (sc->sc_ethif == rcvif) {
@@ -335,7 +240,7 @@
 {
   struct pppoe_softc *sc, *t;
 
-  if (LIST_EMPTY(&pppoe_softc_list)) {
+  if (pppoe_softc_list == NULL) {
     return NULL;
   }
 
@@ -344,7 +249,7 @@
   }
   MEMCPY(&t, token, len);
 
-  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
+  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
     if (sc == t) {
       break;
     }
@@ -384,7 +289,6 @@
   struct pppoe_softc *sc;
   const char *err_msg;
   char devname[6];
-  char *error;
   u8_t *ac_cookie;
   u16_t ac_cookie_len;
 #ifdef PPPOE_SERVER
@@ -487,17 +391,11 @@
         break;
     }
     if (err_msg) {
-      error = NULL;
       if (errortag && len) {
-        error = mem_malloc(len+1);
-        if (error) {
-          strncpy(error, (char*)pb->payload + off + sizeof(pt), len);
-          error[len-1] = '\0';
-        }
-      }
-      if (error) {
-        printf("%s: %s: %s\n", devname, err_msg, error);
-        mem_free(error);
+        u16_t error_len = LWIP_MIN(len, sizeof(pppoe_error_tmp)-1);
+        strncpy(pppoe_error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
+        pppoe_error_tmp[error_len-1] = '\0';
+        printf("%s: %s: %s\n", devname, err_msg, pppoe_error_tmp);
       } else {
         printf("%s: %s\n", devname, err_msg);
       }
@@ -594,7 +492,7 @@
     case PPPOE_CODE_PADO:
       if (sc == NULL) {
         /* be quiet if there is not a single pppoe instance */
-        if (!LIST_EMPTY(&pppoe_softc_list)) {
+        if (pppoe_softc_list != NULL) {
           printf("pppoe: received PADO but could not find request for it\n");
         }
         goto done;
@@ -604,13 +502,6 @@
         goto done;
       }
       if (ac_cookie) {
-        if (sc->sc_ac_cookie) {
-          mem_free(sc->sc_ac_cookie);
-        }
-        sc->sc_ac_cookie = mem_malloc(ac_cookie_len);
-        if (sc->sc_ac_cookie == NULL) {
-          goto done;
-        }
         sc->sc_ac_cookie_len = ac_cookie_len;
         MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
       }
@@ -659,7 +550,7 @@
 pppoe_disc_input(struct netif *netif, struct pbuf *p)
 {
   /* avoid error messages if there is not a single pppoe instance */
-  if (!LIST_EMPTY(&pppoe_softc_list)) {
+  if (pppoe_softc_list != NULL) {
     pppoe_dispatch_disc_pkt(netif, p);
   } else {
     pbuf_free(p);
@@ -778,7 +669,10 @@
 {
   struct pbuf *pb;
   u8_t *p;
-  int len, l1 = 0, l2 = 0; /* XXX: gcc */
+  int len;
+#ifdef PPPOE_TODO
+  int l1 = 0, l2 = 0; /* XXX: gcc */
+#endif /* PPPOE_TODO */
 
   if (sc->sc_state >PPPOE_STATE_PADI_SENT) {
     PPPDEBUG(LOG_ERR, ("ERROR: pppoe_send_padi in state %d", sc->sc_state));
@@ -786,6 +680,7 @@
 
   /* calculate length of frame (excluding ethernet header + pppoe header) */
   len = 2 + 2 + 2 + 2 + sizeof sc;  /* service name tag is required, host unique is send too */
+#ifdef PPPOE_TODO
   if (sc->sc_service_name != NULL) {
     l1 = (int)strlen(sc->sc_service_name);
     len += l1;
@@ -794,6 +689,7 @@
     l2 = (int)strlen(sc->sc_concentrator_name);
     len += 2 + 2 + l2;
   }
+#endif /* PPPOE_TODO */
   LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
     sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
 
@@ -808,19 +704,24 @@
   /* fill in pkt */
   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
   PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
+#ifdef PPPOE_TODO
   if (sc->sc_service_name != NULL) {
     PPPOE_ADD_16(p, l1);
     MEMCPY(p, sc->sc_service_name, l1);
     p += l1;
-  } else {
+  } else
+#endif /* PPPOE_TODO */
+  {
     PPPOE_ADD_16(p, 0);
   }
+#ifdef PPPOE_TODO
   if (sc->sc_concentrator_name != NULL) {
     PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
     PPPOE_ADD_16(p, l2);
     MEMCPY(p, sc->sc_concentrator_name, l2);
     p += l2;
   }
+#endif /* PPPOE_TODO */
   PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
   PPPOE_ADD_16(p, sizeof(sc));
   MEMCPY(p, &sc, sizeof sc);
@@ -954,10 +855,6 @@
   /* cleanup softc */
   sc->sc_state = PPPOE_STATE_INITIAL;
   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
-  if (sc->sc_ac_cookie) {
-    mem_free(sc->sc_ac_cookie);
-    sc->sc_ac_cookie = NULL;
-  }
   sc->sc_ac_cookie_len = 0;
 #ifdef PPPOE_SERVER
   if (sc->sc_hunique) {
@@ -993,17 +890,22 @@
 {
   struct pbuf *pb;
   u8_t *p;
-  size_t len, l1 = 0; /* XXX: gcc */
+  size_t len;
+#ifdef PPPOE_TODO
+  size_t l1 = 0; /* XXX: gcc */
+#endif /* PPPOE_TODO */
 
   if (sc->sc_state != PPPOE_STATE_PADR_SENT) {
     return ERR_CONN;
   }
 
   len = 2 + 2 + 2 + 2 + sizeof(sc);    /* service name, host unique */
+#ifdef PPPOE_TODO
   if (sc->sc_service_name != NULL) {    /* service name tag maybe empty */
     l1 = strlen(sc->sc_service_name);
     len += l1;
   }
+#endif /* PPPOE_TODO */
   if (sc->sc_ac_cookie_len > 0) {
     len += 2 + 2 + sc->sc_ac_cookie_len;  /* AC cookie */
   }
@@ -1017,11 +919,14 @@
   p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
   PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
   PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
+#ifdef PPPOE_TODO
   if (sc->sc_service_name != NULL) {
     PPPOE_ADD_16(p, l1);
     MEMCPY(p, sc->sc_service_name, l1);
     p += l1;
-  } else {
+  } else
+#endif /* PPPOE_TODO */
+  {
     PPPOE_ADD_16(p, 0);
   }
   if (sc->sc_ac_cookie_len > 0) {
@@ -1053,7 +958,7 @@
   LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
 
   ethhdr = (struct eth_hdr *)pb->payload;
-  ethhdr->type = htons(ETHTYPE_PPPOEDISC);
+  ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
   MEMCPY(ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
   MEMCPY(ethhdr->src.addr, ((struct eth_addr *)outgoing_if->hwaddr)->addr, sizeof(ethhdr->src.addr));
 
@@ -1219,10 +1124,6 @@
 
   /* clean up softc */
   MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
-  if (sc->sc_ac_cookie) {
-    mem_free(sc->sc_ac_cookie);
-    sc->sc_ac_cookie = NULL;
-  }
   sc->sc_ac_cookie_len = 0;
   sc->sc_session = 0;
 }