]> git.ipfire.org Git - thirdparty/strongswan.git/blob - linux/lib/libfreeswan/ttoprotoport.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / linux / lib / libfreeswan / ttoprotoport.c
1 /*
2 * conversion from protocol/port string to protocol and port
3 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
4 * Zuercher Hochschule Winterthur,
5 *
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>.
10 *
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
14 * for more details.
15 *
16 * RCSID $Id: ttoprotoport.c,v 1.1 2004/03/15 20:35:26 as Exp $
17 */
18
19 #include "internal.h"
20 #include "freeswan.h"
21
22 /*
23 * ttoprotoport - converts from protocol/port string to protocol and port
24 */
25 err_t
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 */
32 {
33 char *end, *service_name;
34 char proto_name[16];
35 int proto_len;
36 long int l;
37 struct protoent *protocol;
38 struct servent *service;
39
40 /* get the length of the string */
41 if (!src_len) src_len = strlen(src);
42
43 /* locate delimiter '/' between protocol and port */
44 end = strchr(src, '/');
45 if (end != NULL) {
46 proto_len = end - src;
47 service_name = end + 1;
48 } else {
49 proto_len = src_len;
50 service_name = src + src_len;
51 }
52
53 /* copy protocol name*/
54 memset(proto_name, '\0', sizeof(proto_name));
55 memcpy(proto_name, src, proto_len);
56
57 /* extract protocol by trying to resolve it by name */
58 protocol = getprotobyname(proto_name);
59 if (protocol != NULL) {
60 *proto = protocol->p_proto;
61 }
62 else /* failed, now try it by number */
63 {
64 l = strtol(proto_name, &end, 0);
65
66 if (*proto_name && *end)
67 return "<protocol> is neither a number nor a valid name";
68
69 if (l < 0 || l > 0xff)
70 return "<protocol> must be between 0 and 255";
71
72 *proto = (u_int8_t)l;
73 }
74
75 /* is there a port wildcard? */
76 *has_port_wildcard = (strcmp(service_name, "%any") == 0);
77
78 if (*has_port_wildcard)
79 {
80 *port = 0;
81 return NULL;
82 }
83
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);
88 }
89 else /* failed, now try it by number */
90 {
91 l = strtol(service_name, &end, 0);
92
93 if (*service_name && *end)
94 return "<port> is neither a number nor a valid name";
95
96 if (l < 0 || l > 0xffff)
97 return "<port> must be between 0 and 65535";
98
99 *port = (u_int16_t)l;
100 }
101 return NULL;
102 }
103