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

📄 device_info_query.c

📁 mobile ip 在linux下的一种实现
💻 C
字号:
/* $Id: device_info_query.c,v 1.8 2001/08/04 20:55:56 jm Exp $ * Device information query * * 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. */#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <time.h>#include <getopt.h>#include <unistd.h>#include <signal.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>#include <sys/param.h>#include <sys/ioctl.h>#include <linux/if.h>#include "util.h"#include "dyn_mnlib.h"#include "agentapi.h"#include "fixed_fd_zero.h"#include "device_info.h"#include "dyn_ip.h"static char *default_path = DEFAULT_DEVICE_INFO_PATH;static char *usage = "device_info_query -p <socket path>] [-d device] [-h help]\n";static int open_socket(void){	int sock, r;	struct sockaddr_un addr;	socklen_t addrlen;		sock = socket(AF_LOCAL, SOCK_DGRAM, 0);	if (sock < 0)		return sock;		memset(&addr, 0, sizeof(struct sockaddr_un));	addr.sun_family = AF_LOCAL;	addr.sun_path[0] = '\0';	snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1,		 "dynamics-%i-%li", getpid(), random());	addrlen = sizeof(addr.sun_family) + 1 + strlen(addr.sun_path + 1);		r = bind(sock, (struct sockaddr *) &addr, addrlen);	if (r < 0) {		fprintf(stderr, "open_socket: bind failed: %s\n",			strerror(errno));		return -1;	}	return sock;}static int send_query(int s, struct sockaddr_un addr, 		      char *path, struct device_info_query *msg){	int n;	if (!path || !msg) {		fprintf(stderr, "send_query: NULL argument\n");		return -1;	}	n = sendto(s, msg, sizeof(struct device_info_query), 0, 		   (struct sockaddr *)&addr, sizeof(addr));	if (n != sizeof(struct device_info_query)) {		fprintf(stderr, "send_query: sendto %d/%d bytes: %s\n",			n, sizeof(struct device_info_query), strerror(errno));	}	return 0;}static int process_reply(int s, struct device_info_query *msg){	fd_set set;	int len, n;	struct sockaddr_un addr;		FD_ZERO(&set);	FD_SET(s, &set);	select(FD_SETSIZE, &set, NULL, NULL, NULL);	if (!(FD_ISSET(s, &set)))		return 0;		len = sizeof(addr);	n = recvfrom(s, msg, sizeof(struct device_info_query), 0,		     (struct sockaddr *)&addr, (unsigned int *)&len);	if (n != sizeof(struct device_info_query)) {		fprintf(stderr, "process_reply: couldn't receive whole "			"message %d/%d\n", 			n, sizeof(struct device_info_query));		return -1;	}	return 0;}int main(int argc, char *argv[]){	char *path = NULL;	char *ifname = NULL;	int c, s, r;	struct device_info_query msg;	struct idxmap *idx = NULL, *idxmap = NULL, *prev;	struct sockaddr_un serv_addr;	if (argc < 2) {		fprintf(stderr, usage);		exit(1);	}	while (1) {		int option_index = 0;				c = getopt_long (argc, argv, "hd:p:", NULL, &option_index);		if (c == -1)			break;				switch (c) {		case 'd':			ifname = malloc(IFNAMSIZ);			if (ifname == NULL) {				fprintf(stderr, "malloc for device: %s\n",					strerror(errno));				exit(1);			}			memset(ifname, 0, IFNAMSIZ);			dynamics_strlcpy(ifname, optarg, IFNAMSIZ);			break;		case 'p':			path = malloc(MAXPATHLEN);			if (path == NULL) {				fprintf(stderr, "malloc for path: %s\n",					strerror(errno));				exit(1);			}			memset(path, 0, MAXPATHLEN);			dynamics_strlcpy(path, optarg, MAXPATHLEN);			break;		case '?':		case 'h':			fprintf(stderr, usage);			exit(1);		default:			printf ("?? getopt returned character code "				"0%o ??\n", c);		}	}	if (path == NULL)		path = default_path;	if (ifname == NULL)		printf("Querying all interfaces\n");	idxmap = dyn_ip_get_interface_map();	idx = idxmap;	s = open_socket();	if (s < 0) {		fprintf(stderr, "socket open failed\n");		exit(1);	}	memset(&serv_addr, 0, sizeof(serv_addr));	serv_addr.sun_family = AF_LOCAL;	dynamics_strlcpy(serv_addr.sun_path, path,			 sizeof(serv_addr.sun_path));	while (idx != NULL) {		if (ifname != NULL && strncmp(idx->name, ifname, 					      sizeof(ifname))) {			idx = idx->next;			continue;		}		msg.device_index = idx->index;		msg.priority = -1;		r = send_query(s, serv_addr, path, &msg);		if (r == 0)			process_reply(s, &msg);		else {			fprintf(stderr, "send_query failed\n");			continue;		}		printf("interface %s:\n", idx->name);		if (msg.priority != -1)			printf("\tpriority %d\n", msg.priority);		idx = idx->next;	}	idx = idxmap;	while (idx != NULL) {		prev = idx;		idx = idx->next;		free(prev);	}	close(s);	return 0;}

⌨️ 快捷键说明

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