File: gsockaddr-serialize.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (155 lines) | stat: -rw-r--r-- 3,736 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 2002-2015 Balabit
 * Copyright (c) 2015 Viktor Juhasz <viktor.juhasz@balabit.com>
 *
 * 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 "gsockaddr-serialize.h"

#include <string.h>

static gboolean
_serialize_ipv4(GSockAddr *addr, SerializeArchive *sa)
{
  struct in_addr ina;
  ina = g_sockaddr_inet_get_address(addr);
  return serialize_write_blob(sa, (gchar *) &ina, sizeof(ina)) &&
         serialize_write_uint16(sa, htons(g_sockaddr_get_port(addr)));
}

#if SYSLOG_NG_ENABLE_IPV6
static gboolean
_serialize_ipv6(GSockAddr *addr, SerializeArchive *sa)
{
  struct in6_addr *in6a;
  in6a = g_sockaddr_inet6_get_address(addr);
  return serialize_write_blob(sa, (gchar *) in6a, sizeof(*in6a)) &&
         serialize_write_uint16(sa, htons(g_sockaddr_get_port(addr)));
}
#endif

gboolean
g_sockaddr_serialize(SerializeArchive *sa, GSockAddr *addr)
{
  if (!addr)
    {
      return serialize_write_uint16(sa, 0);
    }

  gboolean result = serialize_write_uint16(sa, addr->sa.sa_family);
  switch (addr->sa.sa_family)
    {
    case AF_INET:
    {
      result &= _serialize_ipv4(addr, sa);
      break;
    }
#if SYSLOG_NG_ENABLE_IPV6
    case AF_INET6:
    {
      result &= _serialize_ipv6(addr, sa);
      break;
    }
#endif
    case AF_UNIX:
    {
      break;
    }
    default:
    {
      result = FALSE;
      break;
    }
    }
  return result;
}

static gboolean
_deserialize_ipv4(SerializeArchive *sa, GSockAddr **addr)
{
  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));

  sin.sin_family = AF_INET;
  if (!serialize_read_blob(sa, (gchar *) &sin.sin_addr, sizeof(sin.sin_addr)) ||
      !serialize_read_uint16(sa, &sin.sin_port))
    return FALSE;

  *addr = g_sockaddr_inet_new2(&sin);
  return TRUE;
}


#if SYSLOG_NG_ENABLE_IPV6
static gboolean
_deserialize_ipv6(SerializeArchive *sa, GSockAddr **addr)
{
  gboolean result = FALSE;
  struct sockaddr_in6 sin6;
  memset(&sin6, 0, sizeof(sin6));

  sin6.sin6_family = AF_INET6;
  if (serialize_read_blob(sa, (gchar *) &sin6.sin6_addr, sizeof(sin6.sin6_addr)) &&
      serialize_read_uint16(sa, &sin6.sin6_port))
    {
      *addr = g_sockaddr_inet6_new2(&sin6);
      result = TRUE;
    }
  return result;
}
#endif

gboolean
g_sockaddr_deserialize(SerializeArchive *sa, GSockAddr **addr)
{
  guint16 family;
  gboolean result = TRUE;

  if (!serialize_read_uint16(sa, &family))
    return FALSE;

  switch (family)
    {
    case 0:
      /* special case, no address was stored */
      *addr = NULL;
      break;
    case AF_INET:
    {
      result = _deserialize_ipv4(sa, addr);
      break;
    }
#if SYSLOG_NG_ENABLE_IPV6
    case AF_INET6:
    {
      result = _deserialize_ipv6(sa, addr);
      break;
    }
#endif
    case AF_UNIX:
      *addr = g_sockaddr_unix_new(NULL);
      break;
    default:
      result = FALSE;
      break;
    }
  return result;
}