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

📄 fileio.c

📁 mobile ip 在linux下的一种实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: fileio.c,v 1.25 2001/07/26 17:31:57 jm Exp $ * Configuration file I/O functions * * 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. */#include "owntypes.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#include <sys/types.h> /* required for netinet/in.h in AMIGAOS */#include <netinet/in.h>#include <arpa/inet.h>#include <syslog.h>#include "fileio.h"#include "message.h"#include "dyn_ip.h"#include "util.h"#ifndef LOG_FTP#define LOG_FTP         (11<<3) /* ftp daemon */#endif/* defines */#define ASSERT assert#define BUFLEN 128                   /* input buffer length to start with */#define DEBUGSAVE#define IP_ADDR_LEN 4/* Load data from an ASCII file * * Gets the keyword and data data from each line and calls a user-specified * function to process the line * * INPUT VALUES: * *    ) Userdata may be anything, just passed to the processing function *    ) File must not be NULL, internal error, ASSERT used * M01) Not enough memory for buffer * M02) Blank lines * M03) Comment in the beginning of the line * M04) Comment not in the beginning of the line * M05) Configuration entry before a comment * M06) The line is longer than the default buffer size * M07) Spaces before the keyword * */int load_data(void *userdata, FILE *file,	      int (*process) (void *, char *, char *)){	int i, result, len, flag, bufsize;	char *buf, *key, *data, *temp;	ASSERT(file != NULL);	bufsize = BUFLEN;	buf = malloc(bufsize);	if (buf == NULL) {		fprintf(stderr,			"load_data: Not enough memory for buffer "			"(%d bytes)!\n", bufsize);		return FALSE;	}	do {		if (fgets(buf, bufsize, file) == NULL) break;		len = strlen(buf);		ASSERT(len <= bufsize - 1);		while (buf[len - 1] != '\n') {			bufsize *= 2;			temp = realloc(buf, bufsize);			if (temp == NULL) {				fprintf(stderr,					"load_data: Not enough memory for "					"buffer (%d bytes)!\n", bufsize);				free(buf);				return FALSE;			}			buf = temp;			if (fgets(buf + len, bufsize - len, file) == NULL) {				break;			}			len = strlen(buf);			ASSERT(len <= bufsize - 1);			if (len < bufsize - 1 && buf[len - 1] != '\n') {				fprintf(stderr, "load_data: Binary file "					"containing string terminator or too "					"long line\n");				free(buf);				return FALSE;			}		}		if (buf[len - 1] != '\n') {			fprintf(stderr,				"load_data: Line is not terminated with a "				"linefeed\n");			free(buf);			return FALSE;		}		buf[len - 1] = '\0'; /* remove trailing newline */		data = NULL;		flag = FALSE;		for (i = 0; i < strlen(buf); i++) {			if (buf[i] != ' ') break;		}		key = buf + i;		for (i = 0; i < strlen(key); i++) {			if (key[i] == '"') {				flag = !flag;			}			if (key[i] == '#' && flag == FALSE) {				key[i] = '\0';				break;			}		}		for (i = 0; i < strlen(key); i++) {			if (key[i] == ' ' || key[i] == '\t') {				key[i] = '\0';				data = key + i + 1;				break;			}		}		if (strlen(key) != 0) {			result = (*process)(userdata, key, data);			if (result < 0) {				fprintf(stderr,					"load_data: key = %s, data = %s : ",					key, data);				fprintf(stderr, "result = %d\n", result);				break;			}			if (result > 0) {				free(buf);				return TRUE;			}		}	} while (1);	free(buf);	return FALSE;}/* Load integer * * Integers starting with: * - "0x" are considered to be hexadecimal. * - "0" are considered to be octal. * - anything else is considered to be decimal. * * INPUT VALUES: * *    ) data must not be NULL *    ) x must not be NULL * M08) Hexadecimal number * M09) Octal number * M10) Decimal number * M11) Something else */int load_int(const char *data, int *x){	const char *pos = data;	ASSERT(data != NULL);	ASSERT(x != NULL);	*x = 0;	while (*pos == ' ' || *pos == '\t')		pos++;	if (strncmp(pos, "0x", 2) == 0) {		if (sscanf(pos, "%x", x) != 1) return FALSE;	} else if (strncmp(pos, "0", 1) == 0) {		if (sscanf(pos, "%o", x) != 1) return FALSE;	} else {		if (sscanf(pos, "%d", x) != 1) return FALSE;	}	return TRUE;}/* Load boolean * * Boolean can be either "TRUE" or "FALSE" * * INPUT VALUES: * *    ) data must not be NULL *    ) x must not be NULL * M12) TRUE * M13) FALSE * M14) Something else */int load_bool(const char *data, int *x){	const char *pos = data;	ASSERT(data != NULL);	ASSERT(x != NULL);	*x = FALSE;	while (*pos == ' ' || *pos == '\t')		pos++;	if (strcmp(pos, "TRUE") == 0) {		*x = TRUE;		return TRUE;	}	if (strcmp(pos, "FALSE") == 0) {		return TRUE;	}	return FALSE;}/* Load ip address * * INPUT VALUES: * * IP address is in the IPv4 format, a.b.c.d * *    ) data must not be NULL *    ) addr must not be NULL * M15) Valid IP address * M16) IP byte is not a number * M17) IP byte higher than 255 * M18) IP byte lower than 0 * M19) No separating dot * M20) Too few IP bytes * M21) Too many IP bytes * M22) Something else */int load_ip_address(const char *data, struct in_addr *addr){	int i, tmp;	unsigned int ip;	const char *pos;	ASSERT(data != NULL);	ASSERT(addr != NULL);	pos = data;	while (*pos == ' ' || *pos == '\t')		pos++;	ip = 0;	for (i = 0; ; i++) {		if (sscanf(pos, "%d", &tmp) != 1) {			fprintf(stderr,				"load_ip_address: Unable to parse IP byte "				"from string '%s'\n", pos);			return FALSE;		}		if (tmp > 255) {			fprintf(stderr,				"load_ip_address: Invalid IP byte %d higher "				"than 255\n", tmp);			return FALSE;		}		if (tmp < 0) {			fprintf(stderr,				"load_ip_address: Invalid IP byte %d lower "				"than 0\n", tmp);			return FALSE;		}		ASSERT(tmp >= 0);		ASSERT(tmp <= 255);		ip = (ip << 8) | tmp;		pos = pos + strspn(pos, "0123456789");		if (*pos == '\0' || *pos == ' ' || *pos == '\t') {			if (i == IP_ADDR_LEN - 1) break;			fprintf(stderr,				"load_ip_address: Separating dot not found\n");			return FALSE;		}		if (*pos == '.') {			if (i == IP_ADDR_LEN - 1) {				fprintf(stderr,					"load_ip_address: Too many IP "					"bytes, only %u expected\n",					IP_ADDR_LEN);				return FALSE;			}			pos++;		} else {			fprintf(stderr,				"load_ip_address: IP byte is not a number\n");			return FALSE;		}	}	addr->s_addr = htonl(ip);	return TRUE;}/** * load_net_address: * @char: Any IP address with or without a mask. Valid input string is any *        of the following:  *          a.b.c.d   *          a.b.c.d/p.q.r.s *          a.b.c.d/n   (0 <= n <= 32) * @addr: * @mask: */int load_net_address(char *data, struct in_addr *addr,		     struct in_addr *mask){	char *pos;	int tmp;	int masktable[33] = { 0x00000000,			      0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,			      0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,			      0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,			      0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,			      0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,			      0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,			      0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,			      0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,	};	pos = strchr(data, '/');	if (pos == NULL) {		if (load_ip_address(data, addr) != TRUE) {			fprintf(stderr,				"load_net_address: invalid IP address\n");			return FALSE;		}		mask->s_addr = htonl(0xffffffff);	} else {		*pos = '\0';		if (load_ip_address(data, addr) != TRUE) {			fprintf(stderr,				"load_net_address: invalid IP address\n");			return FALSE;		}		pos++;		if (strchr(pos, '.') != NULL) {			if (load_ip_address(pos, mask) != TRUE) {				fprintf(stderr,				"load_net_address: invalid network mask\n");				return FALSE;			}		} else {			if (sscanf(pos, "%d", &tmp) == 1 &&			    tmp >= 0 && tmp <= 32) {				ASSERT(tmp >= 0);				ASSERT(tmp <= 32);				mask->s_addr = htonl(masktable[tmp]);			} else {				fprintf(stderr,					"load_net_address: invalid network "					"mask\n");				return FALSE;

⌨️ 快捷键说明

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