File: transport-udp-socket.c

package info (click to toggle)
syslog-ng 4.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,572 kB
  • sloc: ansic: 177,639; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (181 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2002-2019 Balabit
 * Copyright (c) 1998-2019 Balázs Scheidler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "transport-udp-socket.h"
#include "transport/transport-socket.h"
#include "gsocket.h"
#include "scratch-buffers.h"
#include "str-format.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <errno.h>
#include <string.h>

typedef struct _LogTransportUDP LogTransportUDP;
struct _LogTransportUDP
{
  LogTransportSocket super;
  GSockAddr *bind_addr;
};

#if defined(__FreeBSD__) || defined(__OpenBSD__)

GSockAddr *
_extract_dest_ip4_addr_from_cmsg(struct cmsghdr *cmsg, GSockAddr *bind_addr)
{
  if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR)
    {
      struct sockaddr_in sin = *(struct sockaddr_in *) &bind_addr->sa;

      struct in_addr *sin_addr = (struct in_addr *) CMSG_DATA(cmsg);
      sin.sin_addr = *sin_addr;

      return g_sockaddr_new((struct sockaddr *) &sin, sizeof(sin));
    }
  return NULL;
}

#else

GSockAddr *
_extract_dest_ip4_addr_from_cmsg(struct cmsghdr *cmsg, GSockAddr *bind_addr)
{
#ifdef IP_PKTINFO
  if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
    {
      struct sockaddr_in sin;
      struct in_pktinfo inpkt;
      memcpy(&inpkt, CMSG_DATA(cmsg), sizeof(inpkt));

      /* we need to copy the port number from the bind address as it is not
       * part of IP_PKTINFO */

      sin = *(struct sockaddr_in *) &bind_addr->sa;
      sin.sin_addr = inpkt.ipi_addr;
      return g_sockaddr_new((struct sockaddr *) &sin, sizeof(sin));
    }
#endif
  return NULL;
}
#endif

#if SYSLOG_NG_ENABLE_IPV6

GSockAddr *
_extract_dest_ip6_addr_from_cmsg(struct cmsghdr *cmsg, GSockAddr *bind_addr)
{
#ifdef IPV6_PKTINFO
  if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO)
    {
      struct sockaddr_in6 sin6;
      struct in6_pktinfo in6pkt;
      memcpy(&in6pkt, CMSG_DATA(cmsg), sizeof(in6pkt));

      /* we need to copy the port number (and scope id) from the bind
       * address as it is not part of IPV6_PKTINFO */

      sin6 = *(struct sockaddr_in6 *) &bind_addr->sa;
      sin6.sin6_addr = in6pkt.ipi6_addr;
      return g_sockaddr_new((struct sockaddr *) &sin6, sizeof(sin6));
    }
#endif
  return NULL;
}

#endif

GSockAddr *
_extract_dest_addr_from_cmsg(struct cmsghdr *cmsg, GSockAddr *bind_addr)
{
  if (bind_addr->sa.sa_family == AF_INET)
    return _extract_dest_ip4_addr_from_cmsg(cmsg, bind_addr);
#if SYSLOG_NG_ENABLE_IPV6
  else if (bind_addr->sa.sa_family == AF_INET6)
    return _extract_dest_ip6_addr_from_cmsg(cmsg, bind_addr);
#endif
  else
    g_assert_not_reached();
}

static void
log_transport_udp_parse_cmsg(LogTransportSocket *s, struct cmsghdr *cmsg, LogTransportAuxData *aux)
{
  LogTransportUDP *self = (LogTransportUDP *) s;

  log_transport_socket_parse_cmsg_method(s, cmsg, aux);

  GSockAddr *dest_addr = _extract_dest_addr_from_cmsg(cmsg, self->bind_addr);
  if (dest_addr)
    {
      log_transport_aux_data_set_local_addr_ref(aux, dest_addr);
      return;
    }
}

static void
_setup_fd(LogTransportUDP *self, gint fd)
{
  gint on = 1;

  self->bind_addr = g_socket_get_local_name(fd);

  if (self->super.address_family == AF_INET)
    {
#if defined(__FreeBSD__) || defined(__OpenBSD__)
      setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on));
#elif defined(IP_PKTINFO)
      setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
#endif
    }
#if SYSLOG_NG_ENABLE_IPV6 && defined(IPV6_RECVPKTINFO)
  else if (self->bind_addr->sa.sa_family == AF_INET6)
    setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));
#endif
  else
    g_assert_not_reached();

}

static void
log_transport_udp_socket_free(LogTransport *s)
{
  LogTransportUDP *self = (LogTransportUDP *)s;
  g_sockaddr_unref(self->bind_addr);
  log_transport_free_method(s);
}

LogTransport *
log_transport_udp_socket_new(gint fd)
{
  LogTransportUDP *self = g_new0(LogTransportUDP, 1);

  log_transport_dgram_socket_init_instance(&self->super, fd);
  self->super.super.free_fn = log_transport_udp_socket_free;
  self->super.parse_cmsg = log_transport_udp_parse_cmsg;

  _setup_fd(self, fd);
  return &self->super.super;
}