2
* conversion from protocol/port string to protocol and port
3
* Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
4
* Zuercher Hochschule Winterthur,
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the
8
* Free Software Foundation; either version 2 of the License, or (at your
9
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* RCSID $Id: ttoprotoport.c,v 1.5 2004/06/09 00:41:09 mcr Exp $
23
* ttoprotoport - converts from protocol/port string to protocol and port
26
ttoprotoport(src, src_len, proto, port, has_port_wildcard)
27
char *src; /* input string */
28
size_t src_len; /* length of input string, use strlen() if 0 */
29
u_int8_t *proto; /* extracted protocol number */
30
u_int16_t *port; /* extracted port number if it exists */
31
int *has_port_wildcard; /* set if port is %any */
33
char *end, *service_name;
37
struct protoent *protocol;
38
struct servent *service;
41
/* get the length of the string */
42
if (!src_len) src_len = strlen(src);
44
/* locate delimiter '/' between protocol and port */
45
end = strchr(src, '/');
47
proto_len = end - src;
48
service_name = end + 1;
51
service_name = src + src_len;
54
/* copy protocol name*/
55
memset(proto_name, '\0', sizeof(proto_name));
56
memcpy(proto_name, src, proto_len);
58
/* extract protocol by trying to resolve it by name */
59
protocol = getprotobyname(proto_name);
60
if (protocol != NULL) {
61
*proto = protocol->p_proto;
63
else /* failed, now try it by number */
65
l = strtol(proto_name, &end, 0);
67
if (*proto_name && *end)
68
return "<protocol> is neither a number nor a valid name";
70
if (l < 0 || l > 0xff)
71
return "<protocol> must be between 0 and 255";
76
/* is there a port wildcard? */
77
wildcard = (strcmp(service_name, "%any") == 0);
79
if(has_port_wildcard) {
80
*has_port_wildcard = wildcard;
88
/* extract port by trying to resolve it by name */
89
service = getservbyname(service_name, NULL);
90
if (service != NULL) {
91
*port = ntohs(service->s_port);
93
else /* failed, now try it by number */
95
l = strtol(service_name, &end, 0);
97
if (*service_name && *end)
98
return "<port> is neither a number nor a valid name";
100
if (l < 0 || l > 0xffff)
101
return "<port> must be between 0 and 65535";
103
*port = (u_int16_t)l;
108
#ifdef TTOPROTOPORT_MAIN
113
static void regress(char *pgm);
116
- main - convert first argument to hex, or run regression
119
main(int argc, char *argv[])
125
int has_port_wildcard;
128
fprintf(stderr, "Usage: %s {0x<hex>|0s<base64>|-r}\n", pgm);
132
if (strcmp(argv[1], "-r") == 0) {
133
regress(pgm); /* should not return */
134
fprintf(stderr, "%s: regress() returned?!?\n", pgm);
138
oops = ttoprotoport(argv[1], strlen(argv[1]),
139
&proto, &port, &has_port_wildcard);
142
fprintf(stderr, "%s: ttodata error `%s' in `%s'\n", pgm,
147
printf("%s -> %d/%d with %d\n",
148
argv[1], proto, port, has_port_wildcard);
154
char *ascii; /* NULL for end */
155
int proto, port, wild;
157
/*{ "", 0, 0, -1 }, */
158
{ "tcp/%any", 6, 0, 1, },
162
static void /* should not return at all, in fact */
170
for (r = atodatatab; r->ascii != NULL; r++) {
173
int has_port_wildcard;
175
err = ttoprotoport(r->ascii, strlen(r->ascii),
176
&proto, &port, &has_port_wildcard);
180
/* okay, error expected */
183
printf("%s expected error, got none.\n", r->ascii);
190
printf("%s got error: %s\n", r->ascii, err);
195
if(proto != r->proto) {
196
printf("%s expected proto %d, got %d\n",r->ascii, proto, r->proto);
201
if(port != r->port) {
202
printf("%s expected port %d, got %d\n",r->ascii, port, r->port);
207
if(has_port_wildcard != r->wild) {
208
printf("%s expected wild %d, got %d\n",r->ascii,
209
has_port_wildcard, r->wild);
219
#endif /* TTODATA_MAIN */