]>
git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan.tmp/ttoprotoport.c
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.1 2004/03/15 20:35:26 as 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
;
40 /* get the length of the string */
41 if (!src_len
) src_len
= strlen(src
);
43 /* locate delimiter '/' between protocol and port */
44 end
= strchr(src
, '/');
46 proto_len
= end
- src
;
47 service_name
= end
+ 1;
50 service_name
= src
+ src_len
;
53 /* copy protocol name*/
54 memset(proto_name
, '\0', sizeof(proto_name
));
55 memcpy(proto_name
, src
, proto_len
);
57 /* extract protocol by trying to resolve it by name */
58 protocol
= getprotobyname(proto_name
);
59 if (protocol
!= NULL
) {
60 *proto
= protocol
->p_proto
;
62 else /* failed, now try it by number */
64 l
= strtol(proto_name
, &end
, 0);
66 if (*proto_name
&& *end
)
67 return "<protocol> is neither a number nor a valid name";
69 if (l
< 0 || l
> 0xff)
70 return "<protocol> must be between 0 and 255";
75 /* is there a port wildcard? */
76 *has_port_wildcard
= (strcmp(service_name
, "%any") == 0);
78 if (*has_port_wildcard
)
84 /* extract port by trying to resolve it by name */
85 service
= getservbyname(service_name
, NULL
);
86 if (service
!= NULL
) {
87 *port
= ntohs(service
->s_port
);
89 else /* failed, now try it by number */
91 l
= strtol(service_name
, &end
, 0);
93 if (*service_name
&& *end
)
94 return "<port> is neither a number nor a valid name";
96 if (l
< 0 || l
> 0xffff)
97 return "<port> must be between 0 and 65535";