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

📄 dyn_mnlib.c

📁 mobile ip 在linux下的一种实现
💻 C
字号:
/* $Id: dyn_mnlib.c,v 1.28 2001/07/11 15:12:43 jm Exp $ * Dynamics Mobile Node API support library implementation * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, 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. */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <assert.h>#include "lib_api.h"#include "dyn_mnlib.h"#define TMP_PREFIX   "mnlib"#define TMP_DIR       NULL#define SOCK_PERM     0600#define DEFAULT_PATH  "/var/run/dynamics_mn_admin"#define MIN(x, y) (((x) < (y)) ? (x) : (y))/** * dynamics_mn_init: * @agent: path to mobility agent * * Initialize library * Parameters: *   agent - path to mobility agent * * Returns: *  API_FAILED: error in intialization, *  API_SUCCESS: success */int dynamics_mn_init(char *agent){	return lib_init((agent != NULL ? agent : DEFAULT_PATH), TMP_DIR,			TMP_PREFIX, SOCK_PERM);}/** * dynamics_mn_get_tunneling_mode * @tunneling_mode:  * @timeout: * *  * Returns: */int dynamics_mn_get_tunneling_mode(int *tunneling_mode, int timeout){	struct api_msg msg;	int r;	/* check parameters */	if (tunneling_mode == NULL || timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_GET_TUNNELING_MODE, NULL, 0, timeout, &msg);	if (r != API_SUCCESS) return r;	if (msg.length != sizeof(int)) return API_FAILED;	memcpy(tunneling_mode, msg.params, sizeof(int));	return API_SUCCESS;}int dynamics_mn_get_care_of_addr(struct in_addr * care_of_addr, int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (care_of_addr == NULL || timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_GET_CAREOF_ADDR, NULL, 0, timeout, &msg);	if (r != API_SUCCESS) return r;	if (msg.length != sizeof(struct in_addr))		return API_FAILED;	memcpy(care_of_addr, msg.params, msg.length);	return API_SUCCESS;}int dynamics_mn_get_status(struct dynamics_mobile_status *status, int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (status == NULL || timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_GET_STATUS, NULL, 0, timeout, &msg);	if (r != API_SUCCESS) return r;	if (msg.length != sizeof(struct dynamics_mobile_status))		return API_FAILED;	memcpy(status, msg.params, msg.length);	return API_SUCCESS;}const char *dynamics_mn_tunnel_mode_desc(enum tunneling_modes mode) {	switch (mode) {	case API_TUNNEL_NONE: return "no tunnel";	case API_TUNNEL_FULL: return "full tunnel";	case API_TUNNEL_TRIANGLE: return "triangle tunnel";	case API_TUNNEL_FULL_HA: return "full tunnel direct to HA";	}	assert(0);	return NULL;}		int dynamics_mn_connect(int tunneling_mode, int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_CONNECT, &tunneling_mode, sizeof(int),			 timeout, &msg);	return r;}int dynamics_mn_disconnect(int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_DISCONNECT, NULL, 0, timeout, &msg);	return r;}static int upd_loc(struct in_addr *new_addr, int timeout, int block){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1 || new_addr == NULL)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call((block ? API_UPDATE_LOCATION_BLOCK :			  API_UPDATE_LOCATION), new_addr,			 sizeof(struct in_addr), timeout, &msg);	return r;}int dynamics_mn_update_location(struct in_addr *new_addr, int timeout){	return upd_loc(new_addr, timeout, 0);}int dynamics_mn_update_location_block(struct in_addr *new_addr, int timeout){	return upd_loc(new_addr, timeout, 1);}static int upd_loc_dev(char *device, int timeout, int block){	int r;	struct api_msg msg;	char tmp[IFNAMSIZ + 5];	/* check parameters */	if (timeout < -1 || (device != NULL && strlen(device) >= IFNAMSIZ))		return API_ILLEGAL_PARAMETERS;	if (device == NULL) {		r = perform_call((block ? API_UPDATE_LOCATION_BLOCK :				  API_UPDATE_LOCATION), NULL, 0, timeout,				 &msg);	} else {		memset(tmp, 0, 4);		memcpy(tmp + 4, device,		       (strlen(device) < IFNAMSIZ ? strlen(device) + 1 :			IFNAMSIZ));		tmp[sizeof(tmp) - 1] = '\0';		r = perform_call((block ? API_UPDATE_LOCATION_BLOCK :				  API_UPDATE_LOCATION), tmp,				 4 + strlen(device), timeout, &msg);	}	return r;}int dynamics_mn_update_location_dev(char *device, int timeout){	return upd_loc_dev(device, timeout, 0);}int dynamics_mn_update_location_dev_block(char *device, int timeout){	return upd_loc_dev(device, timeout, 1);}int dynamics_mn_confirm(int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_CONFIRM, NULL, 0, timeout, &msg);	return r;}/* Return a pointer to string representation of error */char *dynamics_mn_get_error_string(int err){	return get_error_string(err);}/* Set force address for FA. */int dynamics_mn_force_fa(struct in_addr *fa_addr, int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1 || fa_addr == NULL)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_FORCE_FA, fa_addr,			 sizeof(struct in_addr), timeout, &msg);	return r;}int dynamics_mn_get_fa_list(int *fa_count,			    struct dynamics_mobile_fa_list *data,			    int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1 || data == NULL)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_GET_FA_LIST, NULL, 0, timeout, &msg);	if (r != API_SUCCESS) return r;	r = MIN(msg.length / sizeof(struct dynamics_mobile_fa_list),		*fa_count);	*fa_count = msg.length / sizeof(struct dynamics_mobile_fa_list);	memcpy(data, msg.params, r * sizeof(struct dynamics_mobile_fa_list));	return (r == *fa_count)? API_SUCCESS : API_INSUFFICIENT_SPACE;}int dynamics_mn_get_fa_info(struct dynamics_mobile_fa_info *data,			    int timeout){	int r;	struct api_msg msg;	/* check parameters */	if (timeout < -1 || data == NULL)		return API_ILLEGAL_PARAMETERS;	/* do the call */	r = perform_call(API_GET_FA_INFO, data,			 sizeof(struct dynamics_mobile_fa_info), 			 timeout, &msg);	if (msg.length != sizeof(struct dynamics_mobile_fa_info))		return API_FAILED;	memcpy(data, msg.params, msg.length);	return r;}int dynamics_mn_iw_set_ch(int channel, char *ifname, int timeout){	int r;	struct api_msg msg;	struct dynamics_mn_iw_ch_info info;	if (channel < 0 || channel > 16 || ifname == NULL)		return API_ILLEGAL_PARAMETERS;	info.channel = channel;	memcpy(info.ifname, ifname, sizeof(info.ifname));	r = perform_call(API_IW_SET_CH, &info,			 sizeof(struct dynamics_mn_iw_ch_info),			 timeout, &msg);	return r;}int dynamics_mn_iw_get_ch(int *max_num_if,			  struct dynamics_mn_iw_ch_info *info,			  int timeout){	int r;	struct api_msg msg;	if (info == NULL || *max_num_if < 0)		return API_ILLEGAL_PARAMETERS;	r = perform_call(API_IW_GET_CH, NULL, 0, timeout, &msg);	if (r != API_SUCCESS)		return r;	r = MIN(msg.length / sizeof(struct dynamics_mn_iw_ch_info),		*max_num_if);	*max_num_if = (int)(msg.length /			    sizeof(struct dynamics_mn_iw_ch_info));	memcpy((char *)info, (char *)msg.params,	       r * sizeof(struct dynamics_mn_iw_ch_info));	return (r == *max_num_if) ? API_SUCCESS : API_INSUFFICIENT_SPACE;}int dynamics_mn_rescan(int timeout){	struct api_msg msg;	return (perform_call(API_RESCAN, NULL, 0, timeout, &msg));}int dynamics_mn_register_dev_info_socket(char *path, int timeout){	struct api_msg msg;	if (path == NULL) return API_ILLEGAL_PARAMETERS;	return perform_call(API_REGISTER_DEV_INFO_SOCKET, path,			    strlen(path) + 1, timeout, &msg);}int dynamics_mn_policy_on(char *policy, int timeout) {	struct api_msg msg;		if (policy == NULL) return API_ILLEGAL_PARAMETERS;	return perform_call(API_POLICY_ON, policy, strlen(policy) + 1, 			    timeout, &msg);}int dynamics_mn_policy_off(char *policy, int timeout){	struct api_msg msg;		if (policy == NULL) return API_ILLEGAL_PARAMETERS;	return perform_call(API_POLICY_OFF, policy, strlen(policy) + 1, 			    timeout, &msg);}int dynamics_mn_get_policy(char *buffer, int len, int timeout){	struct api_msg msg;	int r, l;		msg.length = sizeof(msg.params) - 1;	r = perform_call(API_GET_POLICY, msg.params, msg.length,  			 timeout, &msg);	if (r != API_SUCCESS)		return r;	l = strlen((char *)msg.params);	if (l > 1024)		l = 1024;	memcpy(buffer, msg.params, l);	return API_SUCCESS;}int dynamics_mn_get_mon_conf(char *buffer, int len, int timeout){	struct api_msg msg;	int r, l;	memset(&msg, 0, sizeof(msg));	msg.length = sizeof(msg.params) - 1;	r = perform_call(API_GET_MON_CONF, msg.params, msg.length,			 timeout, &msg);	if (r != API_SUCCESS)		return r;		l = strlen((char *)msg.params);	if (l > 1024)		l = 1024;	memcpy(buffer, msg.params, l);		return API_SUCCESS;}int dynamics_mn_get_mon_conf_var(char *variable, int varlen, char *buffer, 				 int buflen, int timeout){	int l, r;	struct api_msg msg;	memset(&msg, 0, sizeof(msg));	msg.length = sizeof(msg.params) - 1;	l = varlen < MAX_MON_CONF_VAR_LEN ? varlen : MAX_MON_CONF_VAR_LEN;	strncpy((char *)msg.params, variable, l);	msg.params[l] = '\0';	r = perform_call(API_GET_MON_CONF_VAR, msg.params, msg.length,			 timeout, &msg);	if (r != API_SUCCESS)		return r;	l = strlen((char *)msg.params);	if (l > 1024)		l = 1024;	memcpy(buffer, msg.params, l);	return API_SUCCESS;}int dynamics_mn_set_mon_conf_var(char *variable, int len, int value, 				 int timeout){	int l, r;	struct api_msg msg;	struct dynamics_mn_mon_conf_var reply;	if (value < 0)		return API_FAILED;	reply.value = value;	l = len < MAX_MON_CONF_VAR_LEN ? len : MAX_MON_CONF_VAR_LEN;	strncpy(reply.name, variable, l);	reply.name[l] = '\0';	r = perform_call(API_SET_MON_CONF_VAR, &reply, sizeof(reply),			 timeout, &msg);	if (r != API_SUCCESS)		return r;	return API_SUCCESS;}/* Get current agent address */char *dynamics_mn_get_agent_path(void){	return get_agent_path();}

⌨️ 快捷键说明

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