summaryrefslogtreecommitdiffstats
path: root/sysdep/linux/sysio.h
blob: 3a29cdc991b4d3a59f964951b687304c78c2e268 (plain)
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
/*
 *	BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes
 *
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifdef IPV6

#ifndef IPV6_UNICAST_HOPS
/* Needed on glibc 2.0 systems */
#include <linux/in6.h>
#define CONFIG_IPV6_GLIBC_20
#endif

static inline void
set_inaddr(struct in6_addr *ia, ip_addr a)
{
  ipa_hton(a);
  memcpy(ia, &a, sizeof(a));
}

#else

#include <net/if.h>

static inline void
set_inaddr(struct in_addr *ia, ip_addr a)
{
  ipa_hton(a);
  memcpy(&ia->s_addr, &a, sizeof(a));
}

/*
 *  Multicasting in Linux systems is a real mess. Not only different kernels
 *  have different interfaces, but also different libc's export it in different
 *  ways. Horrible.
 */

static inline char *sysio_mcast_setup(sock *s)
{
  int zero = 0;

  if (ipa_nonzero(s->daddr))
    {
      if (
#ifdef IP_DEFAULT_MULTICAST_TTL
	  s->ttl != IP_DEFAULT_MULTICAST_TTL &&
#endif
	  setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0)
	return "IP_MULTICAST_TTL";
      if (
#ifdef IP_DEFAULT_MULTICAST_LOOP
	  IP_DEFAULT_MULTICAST_LOOP &&
#endif
	  setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
	return "IP_MULTICAST_LOOP";
    }
  return NULL;
}

#ifdef CONFIG_LINUX_MC_MREQN
/*
 *  2.1 and newer kernels use struct mreqn which passes ifindex, so no
 *  problems with unnumbered devices.
 */

#ifndef HAVE_STRUCT_IP_MREQN
/* Several versions of glibc don't define this structure, so we have to do it ourselves */
struct ip_mreqn
{
	struct in_addr	imr_multiaddr;		/* IP multicast address of group */
	struct in_addr	imr_address;		/* local IP address of interface */
	int		imr_ifindex;		/* Interface index */
};
#endif

static inline char *sysio_mcast_join(sock *s)
{
  struct ip_mreqn mreq;
  char *err;
  struct ifreq ifr;

  if (err = sysio_mcast_setup(s))
    return err;
  strcpy(ifr.ifr_name, s->iface->name);
  if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0)
    return "SO_BINDTODEVICE";
  mreq.imr_ifindex = s->iface->index;
  set_inaddr(&mreq.imr_address, s->iface->addr->ip);
  set_inaddr(&mreq.imr_multiaddr, s->daddr);
  /* This defines where should we send _outgoing_ multicasts */
  if (ipa_nonzero(s->daddr) && setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0)
    return "IP_MULTICAST_IF";
  /* And this one sets interface for _receiving_ multicasts from */
  if (ipa_nonzero(s->saddr) && setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
    return "IP_ADD_MEMBERSHIP";
  return NULL;
}
#endif

#if defined(CONFIG_LINUX_MC_MREQ) || defined(CONFIG_LINUX_MC_MREQ_BIND)
/*
 *  Older kernels support only struct mreq which matches interfaces by their
 *  addresses and thus fails on unnumbered devices. On newer 2.0 kernels
 *  we can use SO_BINDTODEVICE to circumvent this problem.
 */

static inline char *sysio_mcast_join(sock *s)
{
  struct in_addr mreq;
  struct ip_mreq mreq_add;
  char *err;

  if (err = sysio_mcast_setup(s))
    return err;
  set_inaddr(&mreq, s->iface->addr->ip);
#ifdef CONFIG_LINUX_MC_MREQ_BIND
  {
    struct ifreq ifr;
    strcpy(ifr.ifr_name, s->iface->name);
    if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0)
      return "SO_BINDTODEVICE";
    mreq_add.imr_interface.s_addr = INADDR_ANY;
  }
#else
  mreq_add.imr_interface = mreq;
#endif
  set_inaddr(&mreq_add.imr_multiaddr, s->daddr);
  /* This defines where should we send _outgoing_ multicasts */
  if (ipa_nonzero(s->daddr) && setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mreq, sizeof(mreq)) < 0)
    return "IP_MULTICAST_IF";
  /* And this one sets interface for _receiving_ multicasts from */
  if (ipa_nonzero(s->saddr) && setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq_add, sizeof(mreq_add)) < 0)
    return "IP_ADD_MEMBERSHIP";
  return NULL;
}
#endif

#endif