13 #define LOCATE_HP(a, b) (((a) - (b)) / 2 + 1)
15 static size_t host_is_valid(char *);
16 static size_t is_sh_clear(char *);
17 static int port_is_valid(char *);
18 static void usage(void);
21 main(int argc
, char **argv
)
23 const unsigned int uargc
= (unsigned int)argc
;
24 char *args
[2] = {NULL
, NULL
}, *init
= NULL
;
29 if (uargc
< 3) usage();
30 while ((ch
= getopt(argc
, argv
, "i:s:")) != -1)
33 if (is_sh_clear(optarg
))
34 errx(1, "sh(1) meta-characters and quotation"
35 " marks are not allowed in `ii arg'.");
39 if (is_sh_clear(optarg
))
40 errx(1, "sh(1) meta-characters and quotation"
41 " marks are not allowed in `sh arg'.");
49 for (o
= 1; o
< uargc
- 2; o
++)
50 if (!strcmp(argv
[o
], "--"))
52 o
++; /* argv[o] is now the first hostname, hopefully: */
53 if ((uargc
- o
) & 1) usage();
55 for (n
= o
; n
< uargc
; n
++) /* Validate the hosts and ports. */
57 if ((rv
= port_is_valid(argv
[n
])) < 0)
58 errx(1, "Port number `%lu' is not in range.",
59 (unsigned long)LOCATE_HP(n
, o
));
61 errx(1, "Character `%lu' in port `%lu' is not"
62 " allowed.", (unsigned long)rv
,
63 (unsigned long)LOCATE_HP(n
, o
));
65 if ((rv
= host_is_valid(argv
[n
])) != NULL
)
66 errx(1, "Character `%lu' in hostname `%lu' is"
67 " not allowed.", (unsigned long)rv
,
68 (unsigned long)LOCATE_HP(n
, o
));
70 if (args
[0]) printf("ii arg: `%s'.\n", args
[0]);
71 if (args
[1]) printf("sh arg: `%s'.\n", args
[1]);
72 for (n
= o
; n
< uargc
- 1; n
+= 2)
73 printf("Server[%lu]: %s:%d\n", (unsigned long)LOCATE_HP(n
, o
),
74 argv
[n
], atoi(argv
[n
+ 1]));
98 if (args
[1] && pids
[1] > 0) {
99 if (kill(pids
[1], SIGKILL
) && errno
!= ESRCH
)
104 /* http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx */
105 n
= o
+ 1 + random() * 1.0 / ( RAND_MAX
+ 1.0 ) * (uargc
- o
- 1);
106 if ((n
- o
) & 1) n
--;
107 s
= strlen(args
[0]) + strlen(argv
[n
]) + strlen(argv
[n
+ 1]) + 10
108 + sizeof(EXECUTABLE
);
110 if ((init
= calloc(s
, 1)) == NULL
)
112 strlcpy(init
, EXECUTABLE
" ", s
);
113 strlcat(init
, args
[0], s
);
114 strlcat(init
, " -s ", s
);
115 strlcat(init
, argv
[n
], s
);
116 strlcat(init
, " -p ", s
);
117 strlcat(init
, argv
[n
+ 1], s
);
123 } else if (pids
[1] > 0) {
140 host_is_valid(char *h
)
145 * Permitted characters in a hostname:
146 * a-z (0x61 <= c <= 0x7a)
147 * 0-9 (0x30 <= c <= 0x39)
148 * A-Z (0x41 <= c <= 0x5a)
151 for (n
= 0; n
< strlen(h
); n
++)
152 if (((h
[n
] < 0x61 || h
[n
] > 0x7a) && (h
[n
] < 0x30 ||
153 h
[n
] > 0x39) && (h
[n
] < 0x41 || h
[n
] > 0x5a))
154 && h
[n
] != '.' && h
[n
] != '-')
164 for (n
= 0; n
< strlen(s
); n
++)
165 if (s
[n
] == '<' || s
[n
] == '>' || s
[n
] == '|' || s
[n
] == ';' ||
166 s
[n
] == '(' || s
[n
] == ')' || s
[n
] == '&' || s
[n
] == '"' ||
168 if (n
&& s
[n
- 1] != '\\')
174 port_is_valid(char *p
)
178 if (atoi(p
) < 1 || atoi(p
) > 65535)
181 for (n
= 0; n
< strlen(p
); n
++)
182 if (p
[n
] < 0x30 || p
[n
] > 0x39)
190 extern char *__progname
;
192 (void)fprintf(stderr
, "usage: %s [-i ii arg] [-s sh arg] \\\n\t --"
193 " host1 port1 [host2 port2 ...]\n", __progname
);