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

📄 iscsi_test.c

📁 iscsi源代码 UNH的progect 有initiator端和target端的源码
💻 C
字号:
/*	initiator/iscsi_test.c	vi: set autoindent tabstop=4 shiftwidth=4 :	This is the user-level interface used to trigger tests 	on the iSCSI initiator and iSCSI target implementations	Assumptions:	requires the initiator and target modules to be loaded before	the program is invoked. (Uses the proc interface provided by the modules).*//*	Copyright (C) 2001-2003 InterOperability Lab (IOL)	University of New Hampshier (UNH)	Durham, NH 03824	This program is free software; you can redistribute it and/or modify	it under the terms of the GNU General Public License as published by	the Free Software Foundation; either version 2, or (at your option)	any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,	USA.	The name of IOL and/or UNH may not be used to endorse or promote products	derived from this software without specific prior written permission.*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <fcntl.h>/* from common/iscsi_common.h */#define INITIATOR			1#define TARGET				2#define MANAGEMENT			4#define DEFAULT_HOST_NO		0#define DEFAULT_TARGET_NO	0#define DEFAULT_CID_NO		0#define	WHITE_SPACE			" \t\v\f\n\r"/*	test number is the index into this table of the corresponding test name */char *test_table[] = {	"t",						/* 0 - send text request */	"ti",						/* 1 - send text request as immediate cmd */	"n",						/* 2 - send nop */	"ni",						/* 3 - send nop as an immediate command */	"p",						/* 4 - send nop ping */	"pi",						/* 5 - send nop ping as an immediate command */	"rp",						/* 6 - toggle respond to nop ping from target */	"ra",						/* 7 - toggle respond to async from target */	"tr",						/* 8 - send connection recovery request - SAI */	"ls",						/* 9 - send session logout request */	"lc",						/*10 - send connection logout request */	NULL};intdo_test(int target, int cid, int host, int test){	int fd, n, len = 0, result;	static char file_name[128];	static char text_line[1024];	printf("do test %d on target %d cid %d host %d\n", test, target, cid, host);	sprintf(file_name, "/proc/scsi/iscsi_initiator/%d", host);	result = -1;	if ((fd = open(file_name, O_RDWR)) < 0) {		perror(file_name);	} else	/***	fd = fileno(stdout);	***/	{		len =			sprintf(text_line,					"iscsi_initiator test target %d cid %d test %d",					target, cid, test);		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 = 0;		close(fd);	}	return result;}intmain(int argc, char *argv[]){	int i, test_no, host_no, target_no, cid_no;	char *endptr, **ttptr;	if (argc < 2) {				/* must have at least 2 parameters */		printf("Usage: iscsi_test <test-name> [target=number] "			   "[cid=number] [host=number]\n");		exit(EXIT_FAILURE);	}	for (test_no = 0, ttptr = test_table; *ttptr != NULL; test_no++, ttptr++) {		if (strcmp(argv[1], *ttptr) == 0) {	/* found matching text name, test_no is corresponding test number */			break;		}	}	if (*ttptr == NULL) {		printf("illegal test name \"%s\"\n", argv[1]);		exit(EXIT_FAILURE);	}	host_no = DEFAULT_HOST_NO;	target_no = DEFAULT_TARGET_NO;	cid_no = DEFAULT_CID_NO;	for (i = 2; i < argc; i++) {		printf("look at argv[%d]: \"%s\"\n", i, argv[i]);		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], "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 {			printf("illegal parameter \"%s\"\n", argv[i]);			exit(EXIT_FAILURE);		}	}	/* if loop finishes, all parameters checked out ok, do the test */	if (do_test(target_no, cid_no, host_no, test_no) < 0)		exit(EXIT_FAILURE);	exit(EXIT_SUCCESS);}

⌨️ 快捷键说明

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