⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgparser.c

📁 mobile ip 在linux下的一种实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: msgparser.c,v 1.59 2001/09/21 16:16:09 jm Exp $ * Parsing of Registration Request and Reply Messages * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#define DEBUG_FLAG 'p'#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <string.h>#include <assert.h>#include "message.h"#include "msgparser.h"#include "debug.h"#include "util.h"static void print_reg_req(const struct reg_req *req){#ifndef NODEBUGMODE	char home_addr[16];	char ha_addr[16];	char co_addr[16];	/* multiple inet_ntoas cannot be used in a single printf function */	dynamics_strlcpy(home_addr, inet_ntoa(req->home_addr), 16);	dynamics_strlcpy(ha_addr, inet_ntoa(req->ha_addr), 16);	dynamics_strlcpy(co_addr, inet_ntoa(req->co_addr), 16);	DEBUG(DEBUG_FLAG, "Registration Request\n"	      "\ttype %u, opts %x, lifetime %d\n"	      "\thome_addr %s, ha_addr %s\n"	      "\tco_addr %s, id %08x, %08x\n",	      req->type, req->opts, ntohs(req->lifetime),	      home_addr, ha_addr,	      co_addr, (u32) ntohl(req->id[0]), (u32) ntohl(req->id[1]));#endif}static void print_reg_rep(const struct reg_rep *rep){#ifndef NODEBUGMODE	char home_addr[16];	char ha_addr[16];	/* multiple inet_ntoas cannot be used in a single printf function */	dynamics_strlcpy(home_addr, inet_ntoa(rep->home_addr), 16);	dynamics_strlcpy(ha_addr, inet_ntoa(rep->ha_addr), 16);	DEBUG(DEBUG_FLAG, "Registration Reply\n"	      "\ttype %u, code %u, lifetime %d\n"	      "\thome_addr %s, ha_addr %s\n"	      "\tid %08x, %08x\n",	      rep->type, rep->code, ntohs(rep->lifetime),	      home_addr, ha_addr,	      (u32) ntohl(rep->id[0]), (u32) ntohl(rep->id[1]));#endif}static void show_nonce(char *txt, const __u8 *nonce){	int i;	DEBUG(DEBUG_FLAG, txt);	for (i = 0; i < FA_REG_NONCE_LEN; i++)		DEBUG(DEBUG_FLAG, "%02X", nonce[i]);}static void print_fa_reg_req(const struct fa_reg_req *req){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG, "FA Registration Request\n"	      "\ttype %u, opts 0x%02x, lifetime %u\n",	      req->type, req->opts, ntohs(req->lifetime));	show_nonce("\tup_nonce ", req->up_nonce);	show_nonce("\n\tdown_nonce ", req->down_nonce);	DEBUG(DEBUG_FLAG, "\n");#endif}static void print_fa_reg_rep(const struct fa_reg_rep *rep){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG, "FA Registration Reply\n"	      "\ttype %u, code %u\n", rep->type, rep->code);	show_nonce("\tup_nonce ", rep->up_nonce);	show_nonce("\n\tdown_nonce ", rep->down_nonce);	DEBUG(DEBUG_FLAG, "\n");#endif}static void print_msg_auth(const struct msg_auth *msg, char *extension){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG, "\t%s: type %d, length %d, spi %d, auth len %d\n",	      extension, msg->type, msg->length, (u32) ntohl(msg->spi),	      GET_AUTH_LEN(msg));#endif}static void print_msg_auth_vendor(const struct vendor_msg_auth *msg,				  char *extension){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG,	      "\t%s: type %d, length %d, vendor_id %i, sub_type %i,\n"	      "\t\tspi %d, auth len %d\n",	      extension, msg->type, msg->length, (u32) ntohl(msg->vendor_id),	      ntohs(msg->sub_type), (u32) ntohl(msg->spi),	      GET_VENDOR_AUTH_LEN(msg));#endif}static void print_msg_auth_gen(const struct generalized_auth_ext *msg,			       char *extension){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG, "\t%s: type %d, subtype %d, length %d, spi %d, "	      "auth len %d\n",	      extension, msg->type, msg->subtype, ntohs(msg->length),	      (u32) ntohl(msg->spi), GET_GEN_AUTH_LEN(msg));#endif}static void print_msg_key(const struct msg_key *msg, char *extension){#ifndef NODEBUGMODE	DEBUG(DEBUG_FLAG,	      "\t%s: type %i, length %i, vendor_id %i, sub_type %i,\n"	      "\t\tspi %d, key len %d\n",	      extension, msg->type, msg->length, (u32) ntohl(msg->vendor_id),	      ntohs(msg->sub_type), (u32) ntohl(msg->spi), GET_KEY_LEN(msg));#endif}static void print_nai_ext(const struct fa_nai_ext *msg, char *extension){	int i;	unsigned char *c;	DEBUG(DEBUG_FLAG,	      "\t%s: type %i, length %i, vendor_id %i, sub_type %i,\n"	      "\t\tnai:",	      extension, msg->type, msg->length, (u32) ntohl(msg->vendor_id),	      ntohs(msg->sub_type));	c = MSG_NAI_DATA(msg);	for (i = 0; i < GET_NAI_LEN(msg); i++) {		if (*c < 32 || *c > 126)			DEBUG(DEBUG_FLAG, "<%i>", *c);		else			DEBUG(DEBUG_FLAG, "%c", *c);		c++;	}	DEBUG(DEBUG_FLAG, "\n");}static void print_mn_nai(const struct mn_nai_ext *msg){	int i;	unsigned char *c;	DEBUG(DEBUG_FLAG, "\tmn_nai: type %i, length %i, nai: ",	      msg->type, msg->length);	c = MSG_MN_NAI_DATA(msg);	for (i = 0; i < GET_MN_NAI_LEN(msg); i++) {		if (*c < 32 || *c > 126)			DEBUG(DEBUG_FLAG, "<%i>", *c);		else			DEBUG(DEBUG_FLAG, "%c", *c);		c++;	}	DEBUG(DEBUG_FLAG, "\n");}static void print_challenge(const struct challenge_ext *msg){	int i;	unsigned char *c;	DEBUG(DEBUG_FLAG, "\tchallenge: type %i, length %i, challenge: ",	      msg->type, msg->length);	c = MSG_MN_NAI_DATA(msg);	for (i = 0; i < GET_CHALLENGE_LEN(msg); i++) {		DEBUG(DEBUG_FLAG, "%02X", *c);		c++;	}	DEBUG(DEBUG_FLAG, "\n");}static void print_gre_key(const struct gre_key_ext *msg){	DEBUG(DEBUG_FLAG,	      "\tgre_key: type %i, length %i, vendor_id %u, sub_type %i,\n"	      "\t\tkey: %u\n", msg->type, msg->length,	      (u32) ntohl(msg->vendor_id),	      ntohs(msg->sub_type), (u32) ntohl(msg->key));}static void print_sfa_debug(const struct sfa_debug_ext *msg){	DEBUG(DEBUG_FLAG,	      "\tsfa_debug: type %i, length %i, vendor_id %u, sub_type %i,\n"	      "\t\tSFA addr: %s\n", msg->type, msg->length,	      (u32) ntohl(msg->vendor_id), ntohs(msg->sub_type),	      inet_ntoa(msg->sfa_addr));}static void print_priv_ha(const struct priv_ha_ext *msg){	DEBUG(DEBUG_FLAG,	      "\tpriv_ha: type %i, length %i, vendor_id %u, sub_type %i,\n"	      "\t\tpriv_ha: %u\n", msg->type, msg->length,	      (u32) ntohl(msg->vendor_id), ntohs(msg->sub_type),	      (u32) ntohl(msg->priv_ha));}static void print_nonce(const struct nonce_ext *msg){	DEBUG(DEBUG_FLAG,	      "\tnonce: type %i, length %i, vendor_id %u, sub_type %i,\n"	      "\t\tnonce: 0x%08x\n", msg->type, msg->length,	      (u32) ntohl(msg->vendor_id), ntohs(msg->sub_type),	      (u32) ntohl(msg->nonce));}static void print_gen_key(const char *extname, void *ext){	struct generalized_mn_fa_key_rep_ext *msg = ext;	DEBUG(DEBUG_FLAG,	      "\t%s: type %i, subtype %i, length %i, keylen %i\n",	      extname, msg->type, msg->subtype, ntohs(msg->length),	      GET_GEN_MN_FA_KEY_REP_LEN(msg));}static void print_gen_key_spi(const char *extname, const char *spitype,			      void *ext){	struct generalized_mn_fa_key_req_ext *msg = ext;	DEBUG(DEBUG_FLAG,	      "\t%s: type %i, subtype %i, length %i, %s SPI %u (0x%08x), "	      "keylen %i\n",	      extname, msg->type, msg->subtype, ntohs(msg->length),	      spitype, (u32) ntohl(msg->mn_spi), (u32) ntohl(msg->mn_spi),	      GET_GEN_MN_FA_KEY_REQ_LEN(msg));}static void print_gen_key_lifetime(const char *extname, void *ext){	struct generalized_mn_ha_key_rep_ext *msg = ext;	DEBUG(DEBUG_FLAG,	      "\t%s: type %i, subtype %i, length %i, lifetime %u, "	      "keylen %i\n",	      extname, msg->type, msg->subtype, ntohs(msg->length),	      (u32) ntohl(msg->lifetime), GET_GEN_MN_HA_KEY_REP_LEN(msg));}static int get_key_ext(struct msg_key **key, char *name, char **pos){	if (*key != NULL) {		DEBUG(DEBUG_FLAG, "get_key_ext: double %s\n", name);		return 1;	}	*key = (struct msg_key *) *pos;	if ((*key)->length < MIN_KEY_EXT_LEN) {		DEBUG(DEBUG_FLAG, "get_key_ext: %s too short\n", name);		return 1;	}	print_msg_key(*key, name);	*pos += GET_KEY_EXT_LEN((*key));	return 0;}static int handle_gen_auth_ext(char **msg_pos, struct msg_extensions *ext){	struct generalized_auth_ext *auth =		(struct generalized_auth_ext *) *msg_pos;	if (GET_GEN_AUTH_LEN(auth) < 16) {		DEBUG(DEBUG_FLAG, "Too short generalized authentication "		      "extension data\n");		return -2;	}	switch (auth->subtype) {	case GENERALIZED_AUTH_MN_AAA:		if (ext->mn_aaa_auth != NULL) {			DEBUG(DEBUG_FLAG, "Duplicate mn_aaa_auth\n");			return -2;		}		ext->mn_aaa_auth = auth;		print_msg_auth_gen(auth, "gen_auth(MN-AAA)");		break;	default:		DEBUG(DEBUG_FLAG, "Unknown generalized authentication "		      "extension subtype\n");		return -1;	}	*msg_pos += GET_GEN_AUTH_EXT_LEN(auth);	return 0;}static int handle_gen_mn_fa_key_req_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_mn_fa_key_req_ext *key =		(struct generalized_mn_fa_key_req_ext *) *msg_pos;	switch (key->subtype) {	case GEN_MN_FA_KEY_REQ_FROM_AAA:		if (ext->mn_fa_key_req_aaa != NULL) {			DEBUG(DEBUG_FLAG, "Duplicate mn_fa_key_req_aaa\n");			return -2;		}		ext->mn_fa_key_req_aaa = key;		print_gen_key_spi("MN_FA_KEY_REQ_AAA", "MN",				  ext->mn_fa_key_req_aaa);		break;	default:		DEBUG(DEBUG_FLAG, "Unknown generalized MN-FA key request "		      "extension subtype\n");		print_gen_key_spi("MN_FA_KEY_REQ(unknown)", "MN", key);		return -1;	}	*msg_pos += GET_GEN_MN_FA_KEY_REQ_EXT_LEN(key);	return 0;}static int handle_gen_mn_fa_key_rep_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_mn_fa_key_rep_ext *key =		(struct generalized_mn_fa_key_rep_ext *) *msg_pos;	struct mn_fa_key_material_from_aaa *aaa;	switch (key->subtype) {	case GEN_MN_FA_KEY_REP_KEY_MATERIAL_FROM_AAA:		if (ext->mn_fa_key_material_aaa != NULL) {			DEBUG(DEBUG_FLAG,			      "Duplicate mn_fa_key_material_aaa\n");			return -2;		}		if (GET_GEN_MN_FA_KEY_REP_LEN(key) <		    sizeof(struct mn_fa_key_material_from_aaa)) {			DEBUG(DEBUG_FLAG,			      "Too short mn_fa_key_material_aaa\n");			return -2;		}		ext->mn_fa_key_material_aaa = key;		print_gen_key("MN_FA_KEY_MATERIAL_AAA",			      ext->mn_fa_key_material_aaa);		aaa = (struct mn_fa_key_material_from_aaa *) (key + 1);		DEBUG(DEBUG_FLAG, "\t\tlifetime=%u, aaa_spi=%u, fa_spi=%u, "		      "alg_id=%i\n", (u32) ntohl(aaa->lifetime),		      (u32) ntohl(aaa->aaa_spi),		      (u32) ntohl(aaa->fa_spi), ntohs(aaa->alg_id));		break;	default:		DEBUG(DEBUG_FLAG, "Unknown generalized MN-FA key reply "		      "extension subtype\n");		print_gen_key("MN_FA_KEY_REP(unknown)", key);		return -1;	}	*msg_pos += GET_GEN_MN_FA_KEY_REP_EXT_LEN(key);	return 0;}static int handle_gen_mn_ha_key_req_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_mn_ha_key_req_ext *key =		(struct generalized_mn_ha_key_req_ext *) *msg_pos;	switch (key->subtype) {	case GEN_MN_HA_KEY_REQ_FROM_AAA:		if (ext->mn_ha_key_req_aaa != NULL) {			DEBUG(DEBUG_FLAG, "Duplicate mn_ha_key_req_aaa\n");			return -2;		}		ext->mn_ha_key_req_aaa = key;		print_gen_key_spi("MN_HA_KEY_REQ_AAA", "MN",				  ext->mn_ha_key_req_aaa);		break;	default:		DEBUG(DEBUG_FLAG, "Unknown generalized MN-HA key request "		      "extension subtype\n");		print_gen_key_spi("MN_HA_KEY_REQ(unknown)", "MN", key);		return -1;	}	*msg_pos += GET_GEN_MN_HA_KEY_REQ_EXT_LEN(key);	return 0;}static int handle_gen_mn_ha_key_rep_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_mn_ha_key_rep_ext *key =		(struct generalized_mn_ha_key_rep_ext *) *msg_pos;	struct mn_ha_key_material_from_aaa *aaa;	switch (key->subtype) {	case GEN_MN_HA_KEY_REP_KEY_MATERIAL_FROM_AAA:		if (ext->mn_ha_key_material_aaa != NULL) {			DEBUG(DEBUG_FLAG,			      "Duplicate mn_ha_key_material_aaa\n");			return -2;		}		if (GET_GEN_MN_HA_KEY_REP_LEN(key) <		    sizeof(struct mn_ha_key_material_from_aaa)) {			DEBUG(DEBUG_FLAG,			      "Too short mn_ha_key_material_aaa\n");			return -2;		}		ext->mn_ha_key_material_aaa = key;		print_gen_key_lifetime("MN_HA_KEY_MATERIAL_AAA",				       ext->mn_ha_key_material_aaa);		aaa = (struct mn_ha_key_material_from_aaa *) (key + 1);		DEBUG(DEBUG_FLAG, "\t\taaa_spi=%u, ha_spi=%u, alg_id=%i, "		      "replay_method=%i\n", (u32) ntohl(aaa->aaa_spi),		      (u32) ntohl(aaa->ha_spi), ntohs(aaa->alg_id),		      ntohs(aaa->replay_method));		break;	default:		DEBUG(DEBUG_FLAG, "Unknown generalized MN-HA key reply "		      "extension subtype\n");		print_gen_key_lifetime("MN_HA_KEY_REP(unknown)", key);		return -1;	}	*msg_pos += GET_GEN_MN_HA_KEY_REP_EXT_LEN(key);	return 0;}static int handle_gen_fa_ha_key_rep_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_fa_ha_key_rep_ext *key =		(struct generalized_fa_ha_key_rep_ext *) *msg_pos;	switch (key->subtype) {	default:		DEBUG(DEBUG_FLAG, "Unknown generalized FA-HA key reply "		      "extension subtype\n");		print_gen_key_lifetime("FA_HA_KEY_REP(unknown)", key);		return -1;	}	*msg_pos += GET_GEN_FA_HA_KEY_REP_EXT_LEN(key);	return 0;}static int handle_gen_fa_fa_key_rep_ext(char **msg_pos,					struct msg_extensions *ext){	struct generalized_fa_fa_key_rep_ext *key =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -