aboutsummaryrefslogtreecommitdiff
path: root/testsuite/findport.c
blob: 81d66c1c7f3656cf037a969e21c6dbdbbb0e6465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* This file is part of GNU Radius.
   Copyright (C) 2001,2003 Free Software Foundation, Inc.

   Written by Sergey Poznyakoff

   GNU Radius 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 of the License, or
   (at your option) any later version.
  
   GNU Radius 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 GNU Radius; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/*
 * This program is a part of test suite. It determines first N available
 * UDP ports to be used.
 * usage: findport [-c N][-s P][-m P][-f F]
 * Options are:
 *         -c N        Find first N not-used ports (default 1)
 *         -s P        Start from port P+1 (default 1024)
 *         -m P        Finish when port P is reached (default 65535)
 *         -f F        Use format string F for output.
 * Any subsequent occurence of characters %d in format string is replaced
 * with the found port number. Usual C backslash sequences are recognized.
 * All other characters encountered in format string are reproduced
 * verbatim.
 * If no format string is specified, the port numbers are printed one per
 * line of output.
 * Return value: 0 if OK, 1 on error.
 * Bugs: No check is made to ensure that the number of %d markers in format
 *       string coincides with number N. 
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>

char *format = NULL;

char *
who_am_i()
{
        struct passwd *pw = getpwuid(getuid());
        if (pw)
                return pw->pw_name;
        return "nobody";
}

void
output(int port)
{
        if (!format) {
                if (port)
                        printf("%d\n", port);
                return;
        }
        
        while (*format) {
                if (port && format[0] == '%' && format[1] == 'd') {
                        printf("%d", port);
                        format += 2;
                        break;
                } else if (format[0] == '%' && format[1] == 'u') {
                        printf("%s", who_am_i());
                        format += 2;
                } else if (format[0] == '\\' && format[1]) {
                        switch (format[1]) {
                        case 'a':
                                putchar('\a');
                                break;
                        case 'b':
                                putchar('\b');
                                break;
                        case 'n':
                                putchar('\n');
                                break;
                        case 't':
                                putchar('\t');
                                break;
                        case 'v':
                                putchar('\v');
                                break;
                        case '\\':
                                putchar('\\');
                                break;
                        default:
                                putchar(format[0]);
                                putchar(format[2]);
                                break;
                        }
                        format += 2;
                } else
                        putchar(*format++);
        }
}

int
main(int argc, char **argv)
{
        char *progname = argv[0]; 
        int local_port, max_port, num_ports;
        struct  sockaddr        salocal;
        struct  sockaddr_in     *sin;
        int fd;
        
        /* Process command line */
        local_port = 1024;
        max_port = 65535;
        num_ports = 1;

#define OPTARG (*argv)[2] ? *argv+2 : *++argv
        while (*++argv) {
                if (**argv == '-') {
                        switch ((*argv)[1]) {
                        case 's':
                                local_port = atoi(OPTARG);
                                break;
                        case 'm':
                                max_port = atoi(OPTARG);
                                break;
                        case 'c':
                                num_ports = atoi(OPTARG);
                                break;
                        case 'f':
                                format = OPTARG;
                                break;
                        default:
                                fprintf(stderr,
                                        "%s: unknown switch: %s\n",
                                        progname, *argv);
                                return 1;
                        }
                } else {
                        fprintf(stderr,
                                "%s: stray argument %s\n", progname, *argv);
                        return 1;
                }
        }
        
        while (num_ports--) {
		int true = 1;

                fd = socket(AF_INET, SOCK_STREAM, 0);
                if (fd < 0) {
                        fprintf(stderr,
                                "%s: can't open socket: %d\n",
                                progname, errno);
                        return 1;
                }

		setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));

                sin = (struct sockaddr_in *) &salocal;
                memset(sin, 0, sizeof (salocal));
                sin->sin_family = AF_INET;
                sin->sin_addr.s_addr = INADDR_ANY;

                do {
                        if (++local_port > max_port) {
                                fprintf(stderr, "%s: can't bind socket\n",
                                        progname);
                                return 1;
                        }
                        sin->sin_port = htons((u_short)local_port);
                } while ((bind(fd, &salocal, sizeof(struct sockaddr_in)) < 0) &&
                         local_port < max_port);
                output(local_port);
                close(fd);
        }
        output(0);
        return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.