• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
4  * Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
5  * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
6  */
7 
8 #define TST_NO_DEFAULT_MAIN
9 #include <stdio.h>
10 
11 #include "tst_safe_net.h"
12 #include "tst_test.h"
13 
help(const char * fname)14 static void help(const char *fname)
15 {
16 	printf("usage: %s FAMILY TYPE\n", fname);
17 	printf("FAMILY := { ipv4 | ipv6 }\n");
18 	printf("TYPE := { stream | dgram }\n");
19 }
20 
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23 	int family = 0, type = 0;
24 	int opt;
25 
26 	while ((opt = getopt(argc, argv, ":h")) != -1) {
27 		switch (opt) {
28 		case 'h':
29 			help(argv[0]);
30 			return 0;
31 		default:
32 			help(argv[0]);
33 			return 1;
34 		}
35 	}
36 
37 	if (argc != 3) {
38 		help(argv[0]);
39 		return 1;
40 	}
41 
42 	if (!strcmp(argv[1], "ipv4"))
43 		family = AF_INET;
44 	else if (!strcmp(argv[1], "ipv6"))
45 		family = AF_INET6;
46 
47 	if (!strcmp(argv[2], "stream"))
48 		type = SOCK_STREAM;
49 	else if (!strcmp(argv[2], "dgram"))
50 		type = SOCK_DGRAM;
51 
52 	if (!family || !type) {
53 		help(argv[0]);
54 		return 1;
55 	}
56 
57 	printf("%d", ntohs(TST_GET_UNUSED_PORT(family, type)));
58 	return 0;
59 }
60