1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 #include "afinet.h"
24 #include "messages.h"
25 #include "gprocess.h"
26 #include "transport-mapper-inet.h"
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 static gint
afinet_lookup_service_and_proto(const gchar * service,const gchar * proto)37 afinet_lookup_service_and_proto(const gchar *service, const gchar *proto)
38 {
39   gchar *end;
40   gint port;
41 
42   /* check if service is numeric */
43   port = strtol(service, &end, 10);
44   if ((*end != 0))
45     {
46       struct servent *se;
47 
48       /* service is not numeric, check if it's a service in /etc/services */
49       se = getservbyname(service, proto);
50       if (se)
51         {
52           port = ntohs(se->s_port);
53         }
54       else
55         {
56           msg_error("Error finding port number, falling back to default",
57                     evt_tag_printf("service", "%s/%s", proto, service));
58           return 0;
59         }
60     }
61   return port;
62 }
63 
64 static const gchar *
afinet_lookup_proto(gint protocol_number,gint sock_type)65 afinet_lookup_proto(gint protocol_number, gint sock_type)
66 {
67   struct protoent *ipproto_ent = getprotobynumber(protocol_number);
68 
69   return ipproto_ent ? ipproto_ent->p_name
70          : ((sock_type == SOCK_STREAM) ? "tcp" : "udp");
71 }
72 
73 guint16
afinet_lookup_service(const TransportMapper * transport_mapper,const gchar * service)74 afinet_lookup_service(const TransportMapper *transport_mapper, const gchar *service)
75 {
76   const gchar *protocol_name = afinet_lookup_proto(transport_mapper->sock_proto, transport_mapper->sock_type);
77 
78   return afinet_lookup_service_and_proto(service, protocol_name);
79 }
80 
81 gint
afinet_determine_port(const TransportMapper * transport_mapper,const gchar * service_port)82 afinet_determine_port(const TransportMapper *transport_mapper, const gchar *service_port)
83 {
84   gint port = 0;
85 
86   if (!service_port)
87     port = transport_mapper_inet_get_server_port(transport_mapper);
88   else
89     port = afinet_lookup_service(transport_mapper, service_port);
90 
91   return port;
92 }
93