PPP, switched chap_digests linked list to a const table in .rodata/flash, saving about 100 bytes in .data segment if chap(md5) and mschap is enabled

This commit is contained in:
Sylvain Rochet 2012-10-14 02:04:36 +02:00
parent e5a554f0b5
commit 7f97e354b4
6 changed files with 28 additions and 47 deletions

View File

@ -31,6 +31,6 @@
#include "lwip/opt.h"
#if PPP_SUPPORT && CHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
extern void chap_md5_init(void);
extern const struct chap_digest_type md5_digest;
#endif /* PPP_SUPPORT && CHAP_SUPPORT */

View File

@ -143,8 +143,6 @@ struct chap_digest_type {
unsigned char *priv);
int (*check_success)(unsigned char *pkt, int len, unsigned char *priv);
void (*handle_failure)(unsigned char *pkt, int len);
struct chap_digest_type *next;
};
/*
@ -154,7 +152,7 @@ struct chap_digest_type {
typedef struct chap_client_state {
u8_t flags;
char *name;
struct chap_digest_type *digest;
const struct chap_digest_type *digest;
unsigned char priv[64]; /* private area for digest's use */
} chap_client_state;
@ -163,7 +161,7 @@ typedef struct chap_server_state {
u8_t flags;
int id;
char *name;
struct chap_digest_type *digest;
const struct chap_digest_type *digest;
int challenge_xmits;
int challenge_pktlen;
unsigned char challenge[CHAL_MAX_PKTLEN];
@ -175,14 +173,11 @@ typedef struct chap_server_state {
#if 0 /* UNUSED */
/* Hook for a plugin to validate CHAP challenge */
extern int (*chap_verify_hook)(char *name, char *ourname, int id,
struct chap_digest_type *digest,
const struct chap_digest_type *digest,
unsigned char *challenge, unsigned char *response,
char *message, int message_space);
#endif /* UNUSED */
/* Called by digest code to register a digest type */
extern void chap_register_digest(struct chap_digest_type *);
#if PPP_SERVER
/* Called by authentication code to start authenticating the peer. */
extern void chap_auth_peer(ppp_pcb *pcb, char *our_name, int digest_code);

View File

@ -106,7 +106,8 @@ void GenerateAuthenticatorResponse(u_char PasswordHashHash[MD4_SIGNATURE_SIZE],
u_char *rchallenge, char *username,
u_char authResponse[MS_AUTH_RESPONSE_LENGTH+1]);
void chapms_init(void);
extern const struct chap_digest_type chapms_digest;
extern const struct chap_digest_type chapms2_digest;
#define __CHAPMS_INCLUDE__
#endif /* __CHAPMS_INCLUDE__ */

View File

@ -107,7 +107,7 @@ static void chap_md5_make_response(unsigned char *response, int id, char *our_na
response[0] = MD5_HASH_SIZE;
}
static struct chap_digest_type md5_digest = {
const struct chap_digest_type md5_digest = {
CHAP_MD5, /* code */
#if PPP_SERVER
chap_md5_generate_challenge,
@ -118,8 +118,4 @@ static struct chap_digest_type md5_digest = {
NULL, /* handle_failure */
};
void chap_md5_init(void) {
chap_register_digest(&md5_digest);
}
#endif /* PPP_SUPPORT && CHAP_SUPPORT */

View File

@ -50,7 +50,7 @@
/* Hook for a plugin to validate CHAP challenge */
int (*chap_verify_hook)(char *name, char *ourname, int id,
struct chap_digest_type *digest,
const struct chap_digest_type *digest,
unsigned char *challenge, unsigned char *response,
char *message, int message_space) = NULL;
@ -90,7 +90,7 @@ static void chap_generate_challenge(ppp_pcb *pcb);
static void chap_handle_response(ppp_pcb *pcb, int code,
unsigned char *pkt, int len);
static int chap_verify_response(char *name, char *ourname, int id,
struct chap_digest_type *digest,
const struct chap_digest_type *digest,
unsigned char *challenge, unsigned char *response,
char *message, int message_space);
#endif /* PPP_SERVER */
@ -106,7 +106,14 @@ static int chap_print_pkt(unsigned char *p, int plen,
#endif /* PRINTPKT_SUPPORT */
/* List of digest types that we know about */
static struct chap_digest_type *chap_digests;
const static struct chap_digest_type* const chap_digests[] = {
&md5_digest,
#if MSCHAP_SUPPORT
&chapms_digest,
&chapms2_digest,
#endif /* MSCHAP_SUPPORT */
NULL
};
/*
* chap_init - reset to initial state.
@ -117,19 +124,6 @@ static void chap_init(ppp_pcb *pcb) {
#if PPP_SERVER
memset(&pcb->chap_server, 0, sizeof(chap_server_state));
#endif /* PPP_SERVER */
chap_md5_init();
#if MSCHAP_SUPPORT
chapms_init();
#endif
}
/*
* Add a new digest type to the list.
*/
void chap_register_digest(struct chap_digest_type *dp) {
dp->next = chap_digests;
chap_digests = dp;
}
/*
@ -163,13 +157,14 @@ static void chap_lowerdown(ppp_pcb *pcb) {
*/
void chap_auth_peer(ppp_pcb *pcb, char *our_name, int digest_code) {
struct chap_server_state *ss = &pcb->chap_server;
struct chap_digest_type *dp;
const struct chap_digest_type *dp;
int i;
if (pcb->chap_server.flags & AUTH_STARTED) {
ppp_error("CHAP: peer authentication already started!");
return;
}
for (dp = chap_digests; dp != NULL; dp = dp->next)
for (i = 0; (dp = chap_digests[i]) != NULL; ++i)
if (dp->code == digest_code)
break;
if (dp == NULL)
@ -191,7 +186,8 @@ void chap_auth_peer(ppp_pcb *pcb, char *our_name, int digest_code) {
* There isn't much to do until we receive a challenge.
*/
void chap_auth_with_peer(ppp_pcb *pcb, char *our_name, int digest_code) {
struct chap_digest_type *dp;
const struct chap_digest_type *dp;
int i;
if(NULL == our_name)
return;
@ -200,9 +196,10 @@ void chap_auth_with_peer(ppp_pcb *pcb, char *our_name, int digest_code) {
ppp_error("CHAP: authentication with peer already started!");
return;
}
for (dp = chap_digests; dp != NULL; dp = dp->next)
for (i = 0; (dp = chap_digests[i]) != NULL; ++i)
if (dp->code == digest_code)
break;
if (dp == NULL)
ppp_fatal("CHAP digest 0x%x requested but not available",
digest_code);
@ -283,7 +280,7 @@ static void chap_handle_response(ppp_pcb *pcb, int id,
unsigned char *response, *outp;
struct pbuf *p;
char *name = NULL; /* initialized to shut gcc up */
int (*verifier)(char *, char *, int, struct chap_digest_type *,
int (*verifier)(char *, char *, int, const struct chap_digest_type *,
unsigned char *, unsigned char *, char *, int);
char rname[MAXNAMELEN+1];
@ -398,7 +395,7 @@ static void chap_handle_response(ppp_pcb *pcb, int id,
* succeeded), or 0 if it doesn't.
*/
static int chap_verify_response(char *name, char *ourname, int id,
struct chap_digest_type *digest,
const struct chap_digest_type *digest,
unsigned char *challenge, unsigned char *response,
char *message, int message_space) {
int ok;

View File

@ -879,7 +879,7 @@ void set_mppe_enc_types(int policy, int types) {
}
#endif /* MPPE */
static struct chap_digest_type chapms_digest = {
const struct chap_digest_type chapms_digest = {
CHAP_MICROSOFT, /* code */
#if PPP_SERVER
chapms_generate_challenge,
@ -890,7 +890,7 @@ static struct chap_digest_type chapms_digest = {
chapms_handle_failure,
};
static struct chap_digest_type chapms2_digest = {
const struct chap_digest_type chapms2_digest = {
CHAP_MICROSOFT_V2, /* code */
#if PPP_SERVER
chapms2_generate_challenge,
@ -901,12 +901,4 @@ static struct chap_digest_type chapms2_digest = {
chapms_handle_failure,
};
void chapms_init(void) {
chap_register_digest(&chapms_digest);
chap_register_digest(&chapms2_digest);
#if PPP_OPTIONS
add_options(chapms_option_list);
#endif /* PPP_OPTIONS */
}
#endif /* PPP_SUPPORT && MSCHAP_SUPPORT */