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

📄 common.c

📁 hostapd源代码
💻 C
字号:
/* * wpa_supplicant/hostapd / common helper functions, etc. * Copyright (c) 2002-2006, Jouni Malinen <jkmaline@cc.hut.fi> * * 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. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. */#include "includes.h"#include "common.h"int wpa_debug_level = MSG_INFO;int wpa_debug_show_keys = 0;int wpa_debug_timestamp = 0;static int hex2num(char c){	if (c >= '0' && c <= '9')		return c - '0';	if (c >= 'a' && c <= 'f')		return c - 'a' + 10;	if (c >= 'A' && c <= 'F')		return c - 'A' + 10;	return -1;}static int hex2byte(const char *hex){	int a, b;	a = hex2num(*hex++);	if (a < 0)		return -1;	b = hex2num(*hex++);	if (b < 0)		return -1;	return (a << 4) | b;}/** * hwaddr_aton - Convert ASCII string to MAC address * @txt: MAC address as a string (e.g., "00:11:22:33:44:55") * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes) * Returns: 0 on success, -1 on failure (e.g., string not a MAC address) */int hwaddr_aton(const char *txt, u8 *addr){	int i;	for (i = 0; i < 6; i++) {		int a, b;		a = hex2num(*txt++);		if (a < 0)			return -1;		b = hex2num(*txt++);		if (b < 0)			return -1;		*addr++ = (a << 4) | b;		if (i < 5 && *txt++ != ':')			return -1;	}	return 0;}/** * hexstr2bin - Convert ASCII hex string into binary data * @hex: ASCII hex string (e.g., "01ab") * @buf: Buffer for the binary data * @len: Length of the text to convert in bytes (of buf); hex will be double * this size * Returns: 0 on success, -1 on failure (invalid hex string) */int hexstr2bin(const char *hex, u8 *buf, size_t len){	size_t i;	int a;	const char *ipos = hex;	u8 *opos = buf;	for (i = 0; i < len; i++) {		a = hex2byte(ipos);		if (a < 0)			return -1;		*opos++ = a;		ipos += 2;	}	return 0;}/** * inc_byte_array - Increment arbitrary length byte array by one * @counter: Pointer to byte array * @len: Length of the counter in bytes * * This function increments the last byte of the counter by one and continues * rolling over to more significant bytes if the byte was incremented from * 0xff to 0x00. */void inc_byte_array(u8 *counter, size_t len){	int pos = len - 1;	while (pos >= 0) {		counter[pos]++;		if (counter[pos] != 0)			break;		pos--;	}}void wpa_get_ntp_timestamp(u8 *buf){	struct os_time now;	u32 sec, usec;	/* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */	os_get_time(&now);	sec = host_to_be32(now.sec + 2208988800U); /* Epoch to 1900 */	/* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */	usec = now.usec;	usec = host_to_be32(4295 * usec - (usec >> 5) - (usec >> 9));	memcpy(buf, (u8 *) &sec, 4);	memcpy(buf + 4, (u8 *) &usec, 4);}#ifndef CONFIG_NO_STDOUT_DEBUGvoid wpa_debug_print_timestamp(void){	struct os_time tv;	if (!wpa_debug_timestamp)		return;	os_get_time(&tv);	printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);}/** * wpa_printf - conditional printf * @level: priority level (MSG_*) of the message * @fmt: printf format string, followed by optional arguments * * This function is used to print conditional debugging and error messages. The * output may be directed to stdout, stderr, and/or syslog based on * configuration. * * Note: New line '\n' is added to the end of the text when printing to stdout. */void wpa_printf(int level, char *fmt, ...){	va_list ap;	va_start(ap, fmt);	if (level >= wpa_debug_level) {		wpa_debug_print_timestamp();		vprintf(fmt, ap);		printf("\n");	}	va_end(ap);}static void _wpa_hexdump(int level, const char *title, const u8 *buf,			 size_t len, int show){	size_t i;	if (level < wpa_debug_level)		return;	wpa_debug_print_timestamp();	printf("%s - hexdump(len=%lu):", title, (unsigned long) len);	if (buf == NULL) {		printf(" [NULL]");	} else if (show) {		for (i = 0; i < len; i++)			printf(" %02x", buf[i]);	} else {		printf(" [REMOVED]");	}	printf("\n");}void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len){	_wpa_hexdump(level, title, buf, len, 1);}void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len){	_wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);}static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,			       size_t len, int show){	size_t i, llen;	const u8 *pos = buf;	const size_t line_len = 16;	if (level < wpa_debug_level)		return;	wpa_debug_print_timestamp();	if (!show) {		printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",		       title, (unsigned long) len);		return;	}	if (buf == NULL) {		printf("%s - hexdump_ascii(len=%lu): [NULL]\n",		       title, (unsigned long) len);		return;	}	printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);	while (len) {		llen = len > line_len ? line_len : len;		printf("    ");		for (i = 0; i < llen; i++)			printf(" %02x", pos[i]);		for (i = llen; i < line_len; i++)			printf("   ");		printf("   ");		for (i = 0; i < llen; i++) {			if (isprint(pos[i]))				printf("%c", pos[i]);			else				printf("_");		}		for (i = llen; i < line_len; i++)			printf(" ");		printf("\n");		pos += llen;		len -= llen;	}}void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len){	_wpa_hexdump_ascii(level, title, buf, len, 1);}void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,			   size_t len){	_wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);}#endif /* CONFIG_NO_STDOUT_DEBUG */static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,				    size_t len, int uppercase){	size_t i;	char *pos = buf, *end = buf + buf_size;	for (i = 0; i < len; i++) {		pos += snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",				data[i]);	}	return pos - buf;}/** * wpa_snprintf_hex - Print data as a hex string into a buffer * @buf: Memory area to use as the output buffer * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1) * @data: Data to be printed * @len: Length of data in bytes */int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len){	return _wpa_snprintf_hex(buf, buf_size, data, len, 0);}/** * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf * @buf: Memory area to use as the output buffer * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1) * @data: Data to be printed * @len: Length of data in bytes */int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,			       size_t len){	return _wpa_snprintf_hex(buf, buf_size, data, len, 1);}#ifdef CONFIG_ANSI_C_EXTRA/* * Extremely simple (and likely inefficient) example implementation of some C * library functions */#ifndef _MSC_VER#undef memcpyvoid *memcpy(void *dest, const void *src, size_t n){	unsigned char *d = dest;	const unsigned char *s = src;	while (n--)		*d++ = *s++;	return dest;}#endif#undef memmovevoid *memmove(void *dest, const void *src, size_t n){	if (dest < src)		memcpy(dest, src, n);	else {		/* overlapping areas */		unsigned char *d = (unsigned char *) dest + n;		const unsigned char *s = (const unsigned char *) src + n;		while (n--)			*--d = *--s;	}	return dest;}#ifndef _MSC_VER#undef memsetvoid *memset(void *s, int c, size_t n){	unsigned char *p = s;	while (n--)		*p++ = c;	return s;}#endif#ifndef _MSC_VER#undef memcmpint memcmp(const void *s1, const void *s2, size_t n){	const unsigned char *p1 = s1, *p2 = s2;	if (n == 0)		return 0;	while (*p1 == *p2) {		p1++;		p2++;		n--;		if (n == 0)			return 0;	}	return *p1 - *p2;}#endif#undef strchrchar *strchr(const char *s, int c){	while (*s) {		if (*s == c)			return (char *) s;		s++;	}	return NULL;}#undef strrchrchar *strrchr(const char *s, int c){	const char *p = s;	while (*p)		p++;	p--;	while (p >= s) {		if (*p == c)			return (char *) p;		p--;	}	return NULL;}#ifndef _MSC_VER#undef strcmpint strcmp(const char *s1, const char *s2){	while (*s1 == *s2) {		if (*s1 == '\0')			break;		s1++;		s2++;	}	return *s1 - *s2;}#endif#undef strncmpint strncmp(const char *s1, const char *s2, size_t n){	if (n == 0)		return 0;	while (*s1 == *s2) {		if (*s1 == '\0')			break;		s1++;		s2++;		n--;		if (n == 0)			return 0;	}	return *s1 - *s2;}#ifndef _MSC_VER#undef strlensize_t strlen(const char *s){	const char *p = s;	while (*p)		p++;	return p - s;}#endif#undef strncpychar *strncpy(char *dest, const char *src, size_t n){	char *d = dest;	while (n--) {		*d = *src;		if (*src == '\0')			break;		d++;		src++;	}	return dest;}#undef strstrchar *strstr(const char *haystack, const char *needle){	size_t len = strlen(needle);	while (*haystack) {		if (strncmp(haystack, needle, len) == 0)			return (char *) haystack;		haystack++;	}	return NULL;}#undef strdupchar * strdup(const char *s){	char *res;	size_t len;	if (s == NULL)		return NULL;	len = strlen(s);	res = malloc(len + 1);	if (res)		memcpy(res, s, len + 1);	return res;}#ifdef _WIN32_WCEvoid perror(const char *s){	wpa_printf(MSG_ERROR, "%s: GetLastError: %d",		   s, (int) GetLastError());}#endif /* _WIN32_WCE */int optind = 1;int optopt;char *optarg;int getopt(int argc, char *const argv[], const char *optstring){	static int optchr = 1;	char *cp;	if (optchr == 1) {		if (optind >= argc) {			/* all arguments processed */			return EOF;		}		if (argv[optind][0] != '-' || argv[optind][1] == '\0') {			/* no option characters */			return EOF;		}	}	if (strcmp(argv[optind], "--") == 0) {		/* no more options */		optind++;		return EOF;	}	optopt = argv[optind][optchr];	cp = strchr(optstring, optopt);	if (cp == NULL || optopt == ':') {		if (argv[optind][++optchr] == '\0') {			optchr = 1;			optind++;		}		return '?';	}	if (cp[1] == ':') {		/* Argument required */		optchr = 1;		if (argv[optind][optchr + 1]) {			/* No space between option and argument */			optarg = &argv[optind++][optchr + 1];		} else if (++optind >= argc) {			/* option requires an argument */			return '?';		} else {			/* Argument in the next argv */			optarg = argv[optind++];		}	} else {		/* No argument */		if (argv[optind][++optchr] == '\0') {			optchr = 1;			optind++;		}		optarg = NULL;	}	return *cp;}#endif /* CONFIG_ANSI_C_EXTRA *//** * wpa_zalloc - Allocate and zero memory * @size: Number of bytes to allocate * Returns: Pointer to allocated and zeroed memory or %NULL on failure */void *wpa_zalloc(size_t size){	void *b = malloc(size);	if (b)		memset(b, 0, size);	return b;}

⌨️ 快捷键说明

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