iscontrol(8): Fix synopsis, sync usage() & improve markup
[dragonfly.git] / sbin / iscontrol / iscontrol.c
blob6c1428fcf7cc035a622ab312116d90fa950000f8
1 /*-
2 * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
28 | $Id: iscontrol.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
31 | the user level initiator (client)
34 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/module.h>
41 #include <sys/linker.h>
43 #include <netinet/in.h>
44 #include <netinet/tcp.h>
45 #include <arpa/inet.h>
46 #include <sys/ioctl.h>
47 #include <netdb.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <time.h>
55 #include <camlib.h>
57 #include "iscsi.h"
58 #include "iscontrol.h"
60 #define USAGE "[-dv] [-c file] [-n nickname] [-t target] [variable=value ...]"
61 #define OPTIONS "vdc:t:n:"
63 #ifndef DEBUG
64 //int vflag;
65 #endif
67 token_t AuthMethods[] = {
68 {"None", NONE},
69 {"KRB5", KRB5},
70 {"SPKM1", SPKM1},
71 {"SPKM2", SPKM2},
72 {"SRP", SRP},
73 {"CHAP", CHAP},
74 {0, 0}
77 token_t DigestMethods[] = {
78 {"None", 0},
79 {"CRC32", 1},
80 {"CRC32C", 1},
81 {0, 0}
84 u_char isid[6 + 6];
86 | Default values
88 isc_opt_t opvals = {
89 .port = 3260,
90 .sockbufsize = 128,
91 .iqn = "iqn.2005-01.il.ac.huji.cs:",
93 .sessionType = "Normal",
94 .targetAddress = 0,
95 .targetName = 0,
96 .initiatorName = 0,
97 .authMethod = "None",
98 .headerDigest = "None,CRC32C",
99 .dataDigest = "None,CRC32C",
100 .maxConnections = 1,
101 .maxRecvDataSegmentLength = 64 * 1024,
102 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
103 .maxBurstLength = 128 * 1024,
104 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
105 .defaultTime2Wait = 0,
106 .defaultTime2Retain = 0,
107 .maxOutstandingR2T = 1,
108 .errorRecoveryLevel = 0,
110 .dataPDUInOrder = TRUE,
111 .dataSequenceInOrder = TRUE,
113 .initialR2T = TRUE,
114 .immediateData = TRUE,
118 lookup(token_t *tbl, char *m)
120 token_t *tp;
122 for(tp = tbl; tp->name != NULL; tp++)
123 if(strcasecmp(tp->name, m) == 0)
124 return tp->val;
125 return 0;
129 main(int cc, char **vv)
131 int ch, disco;
132 char *pname, *p, *q, *ta, *kw;
133 isc_opt_t *op;
134 FILE *fd;
136 /* Try to load iscsi_initiator module before starting its operation */
137 if (modfind(INITIATORMOD) < 0) {
138 if (kldload(INITIATORMOD) < 0 || modfind(INITIATORMOD) < 0) {
139 perror(INITIATORMOD ": Error while handling kernel module");
140 return 1;
144 op = &opvals;
145 iscsidev = "/dev/"ISCSIDEV;
146 fd = NULL;
147 pname = vv[0];
148 if((p = strrchr(pname, '/')) != NULL)
149 pname = p + 1;
151 kw = ta = 0;
152 disco = 0;
154 while((ch = getopt(cc, vv, OPTIONS)) != -1) {
155 switch(ch) {
156 case 'v':
157 vflag++;
158 break;
159 case 'c':
160 fd = fopen(optarg, "r");
161 if(fd == NULL) {
162 perror(optarg);
163 exit(1);
165 break;
166 case 'd':
167 disco = 1;
168 break;
169 case 't':
170 ta = optarg;
171 break;
172 case 'n':
173 kw = optarg;
174 break;
175 default:
176 badu:
177 fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
178 exit(1);
181 if(fd == NULL)
182 fd = fopen("/etc/iscsi.conf", "r");
184 if(fd != NULL) {
185 parseConfig(fd, kw, op);
186 fclose(fd);
188 cc -= optind;
189 vv += optind;
190 if(cc > 0) {
191 if(vflag)
192 printf("adding '%s'\n", *vv);
193 parseArgs(cc, vv, op);
195 if(ta)
196 op->targetAddress = ta;
198 if(op->targetAddress == NULL) {
199 fprintf(stderr, "No target!\n");
200 goto badu;
202 q = op->targetAddress;
203 if(*q == '[' && (q = strchr(q, ']')) != NULL) {
204 *q++ = '\0';
205 op->targetAddress++;
206 } else
207 q = op->targetAddress;
208 if((p = strchr(q, ':')) != NULL) {
209 *p++ = 0;
210 op->port = atoi(p);
211 p = strchr(p, ',');
213 if(p || ((p = strchr(q, ',')) != NULL)) {
214 *p++ = 0;
215 op->targetPortalGroupTag = atoi(p);
217 if(op->initiatorName == 0) {
218 char hostname[256];
220 if(op->iqn) {
221 if(gethostname(hostname, sizeof(hostname)) == 0)
222 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
223 else
224 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
226 else {
227 if(gethostname(hostname, sizeof(hostname)) == 0)
228 asprintf(&op->initiatorName, "%s", hostname);
229 else
230 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
233 if(disco) {
234 op->sessionType = "Discovery";
235 op->targetName = 0;
238 fsm(op);
240 exit(0);