2 * The author of this work has dedicated it to the public by waiving all of his
3 * or her rights to the work under copyright law and all related or neighboring
4 * legal rights he or she had in the work, to the extent allowable by law.
18 #define LOCATE_HP(a, b) (((a) - (b)) / 2 + 1)
20 static size_t host_is_valid(char *);
21 static size_t is_sh_clear(char *);
22 static int port_is_valid(char *);
23 static void usage(void);
26 main(int argc
, char **argv
)
28 const unsigned int uargc
= (unsigned int)argc
;
29 char *args
[2] = {NULL
, NULL
}, *init
= NULL
;
34 if (uargc
< 3) usage();
35 while ((ch
= getopt(argc
, argv
, "i:s:")) != -1)
38 if (is_sh_clear(optarg
))
39 errx(1, "sh(1) meta-characters and quotation"
40 " marks are not allowed in `ii arg'.");
44 if (is_sh_clear(optarg
))
45 errx(1, "sh(1) meta-characters and quotation"
46 " marks are not allowed in `sh arg'.");
54 for (o
= 1; o
< uargc
- 2; o
++)
55 if (!strcmp(argv
[o
], "--"))
57 o
++; /* argv[o] is now the first hostname, hopefully: */
58 if ((uargc
- o
) & 1) usage();
60 for (n
= o
; n
< uargc
; n
++) /* Validate the hosts and ports. */
62 if ((rv
= port_is_valid(argv
[n
])) < 0)
63 errx(1, "Port number `%lu' is not in range.",
64 (unsigned long)LOCATE_HP(n
, o
));
66 errx(1, "Character `%lu' in port `%lu' is not"
67 " allowed.", (unsigned long)rv
,
68 (unsigned long)LOCATE_HP(n
, o
));
70 if ((rv
= host_is_valid(argv
[n
])) != NULL
)
71 errx(1, "Character `%lu' in hostname `%lu' is"
72 " not allowed.", (unsigned long)rv
,
73 (unsigned long)LOCATE_HP(n
, o
));
75 if (args
[0]) printf("ii arg: `%s'.\n", args
[0]);
76 if (args
[1]) printf("sh arg: `%s'.\n", args
[1]);
77 for (n
= o
; n
< uargc
- 1; n
+= 2)
78 printf("Server[%lu]: %s:%d\n", (unsigned long)LOCATE_HP(n
, o
),
79 argv
[n
], atoi(argv
[n
+ 1]));
103 if (args
[1] && pids
[1] > 0) {
104 if (kill(pids
[1], SIGKILL
) && errno
!= ESRCH
)
111 /* http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx */
112 n
= o
+ 1 + random() * 1.0 / ( RAND_MAX
+ 1.0 ) * (uargc
- o
- 1);
113 if ((n
- o
) & 1) n
--;
114 s
= strlen(args
[0]) + strlen(argv
[n
]) + strlen(argv
[n
+ 1]) + 10
117 if ((init
= calloc(s
, 1)) == NULL
)
119 strlcpy(init
, II_EXEC
" ", s
);
120 strlcat(init
, args
[0], s
);
121 strlcat(init
, " -s ", s
);
122 strlcat(init
, argv
[n
], s
);
123 strlcat(init
, " -p ", s
);
124 strlcat(init
, argv
[n
+ 1], s
);
130 } else if (pids
[1] > 0) {
147 host_is_valid(char *h
)
152 * Permitted characters in a hostname:
153 * a-z (0x61 <= c <= 0x7a)
154 * 0-9 (0x30 <= c <= 0x39)
155 * A-Z (0x41 <= c <= 0x5a)
158 for (n
= 0; n
< strlen(h
); n
++)
159 if (((h
[n
] < 0x61 || h
[n
] > 0x7a) && (h
[n
] < 0x30 ||
160 h
[n
] > 0x39) && (h
[n
] < 0x41 || h
[n
] > 0x5a))
161 && h
[n
] != '.' && h
[n
] != '-')
171 for (n
= 0; n
< strlen(s
); n
++)
172 if (s
[n
] == '<' || s
[n
] == '>' || s
[n
] == '|' || s
[n
] == ';' ||
173 s
[n
] == '(' || s
[n
] == ')' || s
[n
] == '&' || s
[n
] == '"' ||
175 if (n
&& s
[n
- 1] != '\\')
181 port_is_valid(char *p
)
185 if (atoi(p
) < 1 || atoi(p
) > 65535)
188 for (n
= 0; n
< strlen(p
); n
++)
189 if (p
[n
] < 0x30 || p
[n
] > 0x39)
197 extern char *__progname
;
199 (void)fprintf(stderr
, "usage: %s [-i ii arg] [-s sh arg] \\\n\t --"
200 " host1 port1 [host2 port2 ...]\n", __progname
);