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

📄 iscsi_config.c

📁 iscsi源代码 UNH的progect 有initiator端和target端的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
				goto out;			}			ret = create_device_nodes(target_dir, host_no,					target_no, lun_no);			if (ret) {				printf("Unable to create device nodes!\n");			}		} else {			sprintf(cmd, "/bin/rm -rf %s/%d",				UNH_ISCSI_DEV_DIR, target_no);			ret = system(cmd);			if (ret)				printf("Removal device files failed!\n");		}	}  out:	close(fd);	TRACE(TRACE_DEBUG, "Leaving do_inquiry\n");}/* * Connects to a remote target by writing an ip address to a /proc file. * If the ip address is 0, a disconnect will result. * Returns	=0 if connect/disconnect worked ok for this target * <0 if connect/disconnect failed * >0 if just adding/removing another lun from this target*/intdo_connect(char *ip_address_str, int port_no, int host_no,		   int target_no, int lun_no, int cid_no){	int fd, n, len, result;	static char file_name[64];	static char text_line[128];	TRACE(TRACE_DEBUG, "Entering do_connect\n");	sprintf(file_name, "/proc/scsi/iscsi_initiator/%d", host_no);	result = -1;	if ((fd = open(file_name, O_RDWR)) < 0) {		perror(file_name);	} else { len =		sprintf(text_line,"iscsi_initiator ip %s port %d target %d "			"lun %d cid %d", ip_address_str, port_no, target_no,			lun_no, cid_no);		TRACE(TRACE_DEBUG, "writing string: %s of length: %d\n",			  text_line, len);		if ((n = write(fd, text_line, len)) < 0) {			perror(file_name);		} else if (n != len && n != 0)			printf("%s: wrote %d bytes, expected %d\n",				file_name, n, len);		else			result = n;		close(fd);	}	TRACE(TRACE_DEBUG, "Leaving do_connect, result %d\n", result);	return result;}int do_server(int action, int tag, char *ip_address, int port_no, int host_no) {	int fd, n, len, result;	static char file_name[64];	static char text_line[128];	TRACE(TRACE_DEBUG, "Entering do_server\n");	sprintf(file_name, "/proc/scsi_target/iscsi_target/%d", host_no);	result = -1;	if ((fd = open(file_name, O_RDWR)) < 0) {		perror(file_name);	} else { len =		sprintf(text_line,"iscsi_target ip %s port %d tag %d action %d",			ip_address, port_no, tag, action);		TRACE(TRACE_DEBUG, "writing string: %s of length: %d\n",			  text_line, len);		if ((n = write(fd, text_line, len)) < 0) {			perror(file_name);		} else if (n != len && n != 0)			printf("%s: wrote %d bytes, expected %d\n",				file_name, n, len);		else			result = n;		close(fd);	}	TRACE(TRACE_DEBUG, "Leaving do_server, result %d\n", result);	return result;}voidprint_ip_address(int address, char *message){	struct in_addr my_in_addr;	TRACE(TRACE_DEBUG, "Entering print_ip_address\n");	my_in_addr.s_addr = htonl(address);	printf("%13s address: %#010x -- %s\n",		   message, address, inet_ntoa(my_in_addr));	TRACE(TRACE_DEBUG, "Leaving print_ip_address\n");}/* converts from a dns name to the ip address as a string * returns address family (AF_INET or AF_INET6) if ok, 0 on error */intcnv_from_dns_name(char *dns_name, char *ip_address_string, int length){	struct hostent *node_ptr;	TRACE(TRACE_DEBUG, "Entering cnv_from_dns_name, dns_name %s\n", dns_name);	/*      get structure for host node with dns name */	if ((node_ptr = gethostbyname(dns_name)) == NULL) {		herror(dns_name);		return 0;	}	strcpy(server_node, node_ptr->h_name);	if (inet_ntop(node_ptr->h_addrtype, node_ptr->h_addr, ip_address_string,															length) == NULL) {		printf("Unable to convert IP address of \"%s\"\n",															node_ptr->h_name);		return 0;	}	TRACE(TRACE_DEBUG, "Leaving cnv_from_dns_name, ip address %s, family %d\n",									ip_address_string, node_ptr->h_addrtype);	return node_ptr->h_addrtype;}intcnv_from_ip_address(int ip_address){	struct hostent *node_ptr;	struct in_addr my_in_addr;	TRACE(TRACE_DEBUG, "Entering cnv_from_ip_address\n");	my_in_addr.s_addr = htonl(ip_address);	/*      get structure for host node with ip address */	if ((node_ptr = gethostbyaddr((char *) &my_in_addr, 4,		AF_INET)) == NULL) {			/* this ip address is not known to the Name service.			 * So just use the dotted-decimal notation as			 * the name.			 */		sprintf(server_node, "%s", inet_ntoa(my_in_addr));	} else {		/* found a DNS name for this ip address, use it. */		strcpy(server_node, node_ptr->h_name);	}	TRACE(TRACE_DEBUG, "Leaving cnv_from_ip_address, server_node: %s\n",		  server_node);	return ip_address;}#define	UP	0#define	DOWN	1/* returns 1 if ip_address_str is a valid ipv6 address,		   0 if ip_address_str has wrong form for ipv6 address   does not return if ip_address_str is invalid ipv6 address*/intis_ipv6(char *ip_address_str, char *ipv6_address){	int len, k;	char *ptr = NULL;	len = strlen(ip_address_str);	if( *ip_address_str == '['  && *(ip_address_str+len-1) == ']') {		/* have bracketed address, check only what is inside the brackets */		ip_address_str++;		len -= 2;		if (strspn(ip_address_str, "0123456789abcdefABCDEF.:") != len)			return 0;		if ((ptr = malloc(len+2)) == NULL)			return 0;		strcpy(ptr, ip_address_str);		*(ptr + len) = '\0';		TRACE(TRACE_DEBUG, "scanning for IPV6 address\n");		k = inet_pton(AF_INET6, ptr, ipv6_address);	} else if (strspn(ip_address_str, "0123456789abcdefABCDEF.:") != len) {		return 0;	} else {		TRACE(TRACE_DEBUG, "scanning for IPV6 address\n");		k = inet_pton(AF_INET6, ip_address_str, ipv6_address);	}	if (k == 0) {		printf("illegal ipv6 address \"%s\"\n", ip_address_str);		exit(EXIT_FAILURE);	} else if (k < 0) {		printf("%s: %s\n", ip_address_str, strerror(errno));		exit(EXIT_FAILURE);	}	if (ptr != NULL)		free(ptr);	return 1;}intmain(int argc, char *argv[]){	int tag;	int ip_address, host_no, target_no, lun_no, port_no, cid_no;	int updown, i, k, server;	char *endptr, *ip_address_str = NULL;	char ipv4_address[4];	char ipv6_address[16];	char ip_address_string[INET6_ADDRSTRLEN];	if (argc < 2) {		printf			("Usage: iscsi_config up/down [server] [ip=address_or_name] "			 "[host=number] [port=number] [target=number] [lun=number] "			 "[cid=number] [tag=number]\n");		exit(EXIT_FAILURE);	}	if (strcmp(argv[1], "up") == 0)		updown = UP;	else if (strcmp(argv[1], "down") == 0)		updown = DOWN;	else {		printf			("parameter 1 must be either \"up\" or \"down\", not \"%s\"\n",			 argv[1]);		exit(EXIT_FAILURE);	}	ip_address = 0;	/*get the DNS name of the local host node on which we are running */	/*****	if( gethostname(local_node, MAXHOSTNAMELEN) < 0 )		{		perror("gethostname");		exit(EXIT_FAILURE);		}	*****/	port_no = ISCSI_WKP;	host_no = DEFAULT_HOST_NO;	target_no = DEFAULT_TARGET_NO;	lun_no = DEFAULT_LUN_NO;	cid_no = DEFAULT_CID_NO;	tag = 0;	server = 0;	for (i = 2; i < argc; i++) {		if (strncmp(argv[i], "ip=", 3) == 0) {			/* dns name or ip address of remote server */			ip_address_str = &argv[i][3];			TRACE(TRACE_DEBUG, "scanning ip=\"%s\"\n", ip_address_str);			if (strspn(ip_address_str, "0123456789.")==strlen(ip_address_str)) {				/* contains only dots and decimal digits, assume ipv4 address */				TRACE(TRACE_DEBUG, "scanning for IPV4 address\n");				k = inet_pton(AF_INET, ip_address_str, ipv4_address);				if (k == 0) {					printf("illegal ipv4 address \"%s\"\n", ip_address_str);					exit(EXIT_FAILURE);				} else if (k < 0) {					printf("%s: %s\n", ip_address_str, strerror(errno));					exit(EXIT_FAILURE);				}				TRACE(TRACE_DEBUG, "valid IPV4 address\n");			} else if (is_ipv6(ip_address_str, ipv6_address)) {				TRACE(TRACE_DEBUG, "valid IPV6 address\n");			} else {				/* must be dns name, look it up to get ip address as a string */				TRACE(TRACE_DEBUG, "looking up DNS name\n");				if ((k = cnv_from_dns_name(ip_address_str, ip_address_string,							INET6_ADDRSTRLEN)) != AF_INET && k != AF_INET6) {					exit(EXIT_FAILURE);				}				ip_address_str = ip_address_string;				if (k == AF_INET) {					TRACE(TRACE_DEBUG, "valid IPV4 address\n");				} else {					TRACE(TRACE_DEBUG, "valid IPV6 address\n");				}			}		} else if (strncmp(argv[i], "port=", 5) == 0) {			/* port number of target */			errno = 0;			port_no = strtoul(&argv[i][5], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) != strlen(endptr)				|| port_no == 0 || port_no > MASK_16_BITS) {				printf("illegal port number \"%s\"\n", &argv[i][5]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "host=", 5) == 0) {			/* number of host adaptor */			errno = 0;			host_no = strtoul(&argv[i][5], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) !=				strlen(endptr)) {				printf("illegal host number \"%s\"\n", &argv[i][5]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "target=", 7) == 0) {			/* id number of target */			errno = 0;			target_no = strtoul(&argv[i][7], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) !=				strlen(endptr)) {				printf("illegal target number \"%s\"\n", &argv[i][7]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "lun=", 4) == 0) {			/* number of lun */			errno = 0;			lun_no = strtoul(&argv[i][4], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) !=				strlen(endptr)				|| lun_no > MAX_LUNS) {				printf("illegal lun number \"%s\"\n", &argv[i][4]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "cid=", 4) == 0) {			/* number of cid */			errno = 0;			cid_no = strtoul(&argv[i][4], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) != strlen(endptr)) {				printf("illegal cid number \"%s\"\n", &argv[i][4]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "tag=", 4) == 0) {			/* number of portal group tag */			errno = 0;			tag = strtoul(&argv[i][4], &endptr, 0);			if (errno != 0 || strspn(endptr, WHITE_SPACE) != strlen(endptr)) {				printf("illegal tag number \"%s\"\n", &argv[i][4]);				exit(EXIT_FAILURE);			}		} else if (strncmp(argv[i], "server", 6) == 0) {			server = 1;		} else {			printf("illegal parameter \"%s\"\n", argv[i]);			exit(EXIT_FAILURE);		}	}	if (server == 1) {		do_server(updown, tag, ip_address_str, port_no, host_no);	} else if (updown == UP) {		if (lun_no == DEFAULT_LUN_NO) {			/* no LUN is specified, bring up all LUNs */			for (i = 0; i < MAX_LUNS; i++) {				if (do_connect(ip_address_str, port_no, host_no, target_no,												i, cid_no) > 0) {					do_inquiry(1, host_no, target_no, i);				}			}		} else {			/* LUN is specified, bring just that LUN up */			if (do_connect(ip_address_str, port_no, host_no, target_no,											lun_no, cid_no) > 0) {				do_inquiry(1, host_no, target_no, lun_no);			}		}	} else {		if (lun_no != DEFAULT_LUN_NO) {			/* LUN is specified, bring just that LUN down */			if (do_connect(ip_address_str, 0, host_no, target_no,											lun_no, cid_no) > 0) {				do_inquiry(0, host_no, target_no, lun_no);			}		} else {			/* Process all LUNs for connection tear down */			do_connect(ip_address_str, 0, host_no, target_no, 0, cid_no);			for (i = 0; i < MAX_LUNS; i++) {				do_inquiry(0, host_no, target_no, i);			}		}	}	exit(EXIT_SUCCESS);}

⌨️ 快捷键说明

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