| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "librist/udpsocket.h" |
| 9 |
#include "log-private.h" |
| 10 |
#include "pthread-shim.h" |
| 11 |
#ifdef _WIN32 |
| 12 |
#include <ws2ipdef.h> |
| 13 |
#include <mstcpip.h> |
| 14 |
#ifndef MCAST_JOIN_GROUP |
| 15 |
#define MCAST_JOIN_GROUP 41 |
| 16 |
#endif |
| 17 |
#ifndef MCAST_JOIN_SOURCE_GROUP |
| 18 |
#define MCAST_JOIN_SOURCE_GROUP 45 |
| 19 |
#endif |
| 20 |
|
| 21 |
|
| 22 |
#ifndef SIO_UDP_CONNRESET |
| 23 |
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12) |
| 24 |
#endif |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
static const int yes = 1; |
| 29 |
|
| 30 |
#ifdef _WIN32 |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#if !defined(_WIN32) || HAVE_PTHREADS |
| 43 |
#if defined(__GNU__) |
| 44 |
static pthread_once_t winsock_init_once = {__PTHREAD_ONCE_INIT}; |
| 45 |
#else |
| 46 |
static pthread_once_t winsock_init_once = PTHREAD_ONCE_INIT; |
| 47 |
#endif |
| 48 |
#endif |
| 49 |
#if defined(_WIN32) && !HAVE_PTHREADS |
| 50 |
static INIT_ONCE winsock_init_once = INIT_ONCE_STATIC_INIT; |
| 51 |
#endif |
| 52 |
|
| 53 |
#if HAVE_PTHREADS |
| 54 |
static void _librist_udpsocket_init_winsock_func(void) |
| 55 |
#else |
| 56 |
static BOOL WINAPI _librist_udpsocket_init_winsock_func(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 57 |
#endif |
| 58 |
{ |
| 59 |
WSADATA wsaData; |
| 60 |
int ret = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 61 |
if (ret != 0) { |
| 62 |
rist_log_priv3(RIST_LOG_ERROR, "WSAStartup failed: %d\n", ret); |
| 63 |
} |
| 64 |
#if !HAVE_PTHREADS |
| 65 |
return TRUE; |
| 66 |
#endif |
| 67 |
} |
| 68 |
|
| 69 |
static void _librist_udpsocket_init_winsock(void) |
| 70 |
{ |
| 71 |
#if HAVE_PTHREADS |
| 72 |
pthread_once(&winsock_init_once, _librist_udpsocket_init_winsock_func); |
| 73 |
#else |
| 74 |
InitOnceExecuteOnce(&winsock_init_once, _librist_udpsocket_init_winsock_func, NULL, NULL); |
| 75 |
#endif |
| 76 |
} |
| 77 |
#else |
| 78 |
static inline void _librist_udpsocket_init_winsock(void) { } |
| 79 |
#endif |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
int udpsocket_resolve_host(const char *host, uint16_t port, struct sockaddr *addr) |
| 84 |
{ |
| 85 |
struct sockaddr_in *a4 = (struct sockaddr_in *)addr; |
| 86 |
struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr; |
| 87 |
|
| 88 |
_librist_udpsocket_init_winsock(); |
| 89 |
|
| 90 |
|
| 91 |
if (inet_pton(AF_INET6, host, &a6->sin6_addr) > 0) { |
| 92 |
a6->sin6_family = AF_INET6; |
| 93 |
a6->sin6_port = htons(port); |
| 94 |
} |
| 95 |
|
| 96 |
else if (inet_pton(AF_INET, host, &a4->sin_addr) > 0) { |
| 97 |
a4->sin_family = AF_INET; |
| 98 |
a4->sin_port = htons(port); |
| 99 |
|
| 100 |
} else { |
| 101 |
struct addrinfo *res; |
| 102 |
int gai_ret = getaddrinfo(host, NULL, NULL, &res); |
| 103 |
if (gai_ret != 0) { |
| 104 |
rist_log_priv3( RIST_LOG_ERROR, "Failure resolving host %s: %s\n", host, gai_strerror(gai_ret)); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
if (res[0].ai_family == AF_INET6) { |
| 108 |
memcpy(a6, res[0].ai_addr, sizeof(struct sockaddr_in6)); |
| 109 |
a6->sin6_port = htons(port); |
| 110 |
} else { |
| 111 |
memcpy(a4, res[0].ai_addr, sizeof(struct sockaddr_in)); |
| 112 |
a4->sin_port = htons(port); |
| 113 |
} |
| 114 |
freeaddrinfo(res); |
| 115 |
} |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void udpsocket_set_dontfragment(int sd, uint16_t af) |
| 127 |
{ |
| 128 |
#if defined(__linux__) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) |
| 129 |
if (af == AF_INET) { |
| 130 |
int val = IP_PMTUDISC_DO; |
| 131 |
if (setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)) < 0) |
| 132 |
rist_log_priv3(RIST_LOG_WARN, |
| 133 |
"setsockopt(IP_MTU_DISCOVER=DO) failed: %s; oversized " |
| 134 |
"datagrams may be silently IP-fragmented\n", strerror(errno)); |
| 135 |
} |
| 136 |
#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) |
| 137 |
else if (af == AF_INET6) { |
| 138 |
int val = IPV6_PMTUDISC_DO; |
| 139 |
if (setsockopt(sd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val)) < 0) |
| 140 |
rist_log_priv3(RIST_LOG_WARN, |
| 141 |
"setsockopt(IPV6_MTU_DISCOVER=DO) failed: %s\n", strerror(errno)); |
| 142 |
} |
| 143 |
#endif |
| 144 |
#elif defined(_WIN32) && defined(IP_DONTFRAGMENT) |
| 145 |
DWORD val = 1; |
| 146 |
if (af == AF_INET) { |
| 147 |
if (setsockopt(sd, IPPROTO_IP, IP_DONTFRAGMENT, |
| 148 |
(const char *)&val, sizeof(val)) == SOCKET_ERROR) |
| 149 |
rist_log_priv3(RIST_LOG_WARN, |
| 150 |
"setsockopt(IP_DONTFRAGMENT) failed: WSAGetLastError=%d\n", |
| 151 |
WSAGetLastError()); |
| 152 |
} |
| 153 |
#if defined(IPV6_DONTFRAG) |
| 154 |
else if (af == AF_INET6) { |
| 155 |
if (setsockopt(sd, IPPROTO_IPV6, IPV6_DONTFRAG, |
| 156 |
(const char *)&val, sizeof(val)) == SOCKET_ERROR) |
| 157 |
rist_log_priv3(RIST_LOG_WARN, |
| 158 |
"setsockopt(IPV6_DONTFRAG) failed: WSAGetLastError=%d\n", |
| 159 |
WSAGetLastError()); |
| 160 |
} |
| 161 |
#endif |
| 162 |
#elif defined(IP_DONTFRAG) |
| 163 |
int val = 1; |
| 164 |
if (af == AF_INET) { |
| 165 |
if (setsockopt(sd, IPPROTO_IP, IP_DONTFRAG, &val, sizeof(val)) < 0) |
| 166 |
rist_log_priv3(RIST_LOG_WARN, |
| 167 |
"setsockopt(IP_DONTFRAG) failed: %s\n", strerror(errno)); |
| 168 |
} |
| 169 |
#if defined(IPV6_DONTFRAG) |
| 170 |
else if (af == AF_INET6) { |
| 171 |
if (setsockopt(sd, IPPROTO_IPV6, IPV6_DONTFRAG, &val, sizeof(val)) < 0) |
| 172 |
rist_log_priv3(RIST_LOG_WARN, |
| 173 |
"setsockopt(IPV6_DONTFRAG) failed: %s\n", strerror(errno)); |
| 174 |
} |
| 175 |
#endif |
| 176 |
#else |
| 177 |
(void)sd; (void)af; |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
int udpsocket_set_mcast_ttl(int sd, uint16_t af, uint32_t ttl) |
| 182 |
{ |
| 183 |
if (ttl == 0) |
| 184 |
return 0; |
| 185 |
if (af == AF_INET6) { |
| 186 |
#ifdef IPV6_MULTICAST_HOPS |
| 187 |
int hops = (int)ttl; |
| 188 |
if (setsockopt(sd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char *)&hops, sizeof(hops)) < 0) { |
| 189 |
#ifdef _WIN32 |
| 190 |
rist_log_priv3(RIST_LOG_WARN, "setsockopt(IPV6_MULTICAST_HOPS) failed: WSAGetLastError=%d\n", WSAGetLastError()); |
| 191 |
#else |
| 192 |
rist_log_priv3(RIST_LOG_WARN, "setsockopt(IPV6_MULTICAST_HOPS) failed: %s\n", strerror(errno)); |
| 193 |
#endif |
| 194 |
return -1; |
| 195 |
} |
| 196 |
#else |
| 197 |
(void)sd; (void)ttl; |
| 198 |
#endif |
| 199 |
} else { |
| 200 |
#ifdef IP_MULTICAST_TTL |
| 201 |
int val = (int)ttl; |
| 202 |
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&val, sizeof(val)) < 0) { |
| 203 |
#ifdef _WIN32 |
| 204 |
rist_log_priv3(RIST_LOG_WARN, "setsockopt(IP_MULTICAST_TTL) failed: WSAGetLastError=%d\n", WSAGetLastError()); |
| 205 |
#else |
| 206 |
rist_log_priv3(RIST_LOG_WARN, "setsockopt(IP_MULTICAST_TTL) failed: %s\n", strerror(errno)); |
| 207 |
#endif |
| 208 |
return -1; |
| 209 |
} |
| 210 |
#else |
| 211 |
(void)sd; (void)ttl; |
| 212 |
#endif |
| 213 |
} |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
int udpsocket_open(uint16_t af) |
| 218 |
{ |
| 219 |
_librist_udpsocket_init_winsock(); |
| 220 |
int sd = socket(af, SOCK_DGRAM, 0); |
| 221 |
if (sd < 0) { |
| 222 |
#ifdef _WIN32 |
| 223 |
sd = -1 * WSAGetLastError(); |
| 224 |
#endif |
| 225 |
return sd; |
| 226 |
} |
| 227 |
#ifdef _WIN32 |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
BOOL connreset_new_behavior = FALSE; |
| 237 |
DWORD connreset_bytes = 0; |
| 238 |
if (WSAIoctl(sd, SIO_UDP_CONNRESET, |
| 239 |
&connreset_new_behavior, sizeof(connreset_new_behavior), |
| 240 |
NULL, 0, &connreset_bytes, NULL, NULL) == SOCKET_ERROR) { |
| 241 |
rist_log_priv3(RIST_LOG_WARN, |
| 242 |
"WSAIoctl(SIO_UDP_CONNRESET, FALSE) failed: WSAGetLastError=%d; " |
| 243 |
"WSAECONNRESET indications remain enabled\n", |
| 244 |
WSAGetLastError()); |
| 245 |
} |
| 246 |
#endif |
| 247 |
return sd; |
| 248 |
} |
| 249 |
|
| 250 |
int udpsocket_set_optimal_buffer_size(int sd) |
| 251 |
{ |
| 252 |
|
| 253 |
uint32_t current_recvbuf = udpsocket_get_buffer_size(sd); |
| 254 |
uint32_t bufsize = UDPSOCKET_SOCK_BUFSIZE_MAX; |
| 255 |
if (current_recvbuf < bufsize){ |
| 256 |
setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, sizeof(uint32_t)); |
| 257 |
current_recvbuf = udpsocket_get_buffer_size(sd); |
| 258 |
#if defined(SO_RCVBUFFORCE) |
| 259 |
if (current_recvbuf < bufsize){ |
| 260 |
setsockopt(sd, SOL_SOCKET, SO_RCVBUFFORCE, (char *)&bufsize, sizeof(uint32_t)); |
| 261 |
current_recvbuf = udpsocket_get_buffer_size(sd); |
| 262 |
} |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
if (current_recvbuf < UDPSOCKET_SOCK_BUFSIZE){ |
| 267 |
|
| 268 |
bufsize = UDPSOCKET_SOCK_BUFSIZE/5; |
| 269 |
setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, sizeof(uint32_t)); |
| 270 |
current_recvbuf = udpsocket_get_buffer_size(sd); |
| 271 |
#if defined(SO_RCVBUFFORCE) |
| 272 |
if (current_recvbuf < bufsize){ |
| 273 |
setsockopt(sd, SOL_SOCKET, SO_RCVBUFFORCE, (char *)&bufsize, sizeof(uint32_t)); |
| 274 |
current_recvbuf = udpsocket_get_buffer_size(sd); |
| 275 |
} |
| 276 |
#endif |
| 277 |
if (current_recvbuf < bufsize){ |
| 278 |
rist_log_priv3( RIST_LOG_ERROR, "Your UDP receive buffer is set < 200 kbytes (%"PRIu32") and the kernel denied our request for an increase. It's recommended to set your net.core.rmem_max setting to at least 200 kbyte for best results.", current_recvbuf); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
} |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
int udpsocket_set_optimal_buffer_send_size(int sd) |
| 286 |
{ |
| 287 |
|
| 288 |
uint32_t current_sendbuf = udpsocket_get_buffer_send_size(sd); |
| 289 |
uint32_t bufsize = UDPSOCKET_SOCK_BUFSIZE_MAX; |
| 290 |
if (current_sendbuf < bufsize){ |
| 291 |
setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, sizeof(uint32_t)); |
| 292 |
current_sendbuf = udpsocket_get_buffer_send_size(sd); |
| 293 |
#if defined(SO_SNDBUFFORCE) |
| 294 |
if (current_sendbuf < bufsize){ |
| 295 |
setsockopt(sd, SOL_SOCKET, SO_SNDBUFFORCE, (char *)&bufsize, sizeof(uint32_t)); |
| 296 |
current_sendbuf = udpsocket_get_buffer_send_size(sd); |
| 297 |
} |
| 298 |
#endif |
| 299 |
} |
| 300 |
if (current_sendbuf < UDPSOCKET_SOCK_BUFSIZE){ |
| 301 |
|
| 302 |
bufsize = UDPSOCKET_SOCK_BUFSIZE/5; |
| 303 |
setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, sizeof(uint32_t)); |
| 304 |
current_sendbuf = udpsocket_get_buffer_send_size(sd); |
| 305 |
#if defined(SO_SNDBUFFORCE) |
| 306 |
if (current_sendbuf < bufsize){ |
| 307 |
setsockopt(sd, SOL_SOCKET, SO_SNDBUFFORCE, (char *)&bufsize, sizeof(uint32_t)); |
| 308 |
current_sendbuf = udpsocket_get_buffer_send_size(sd); |
| 309 |
} |
| 310 |
#endif |
| 311 |
if (current_sendbuf < bufsize){ |
| 312 |
rist_log_priv3( RIST_LOG_ERROR, "Your UDP send buffer is set < 200 kbytes (%"PRIu32") and the kernel denied our request for an increase. It's recommended to set your net.core.wmem_max setting to at least 200 kbyte for best results.", current_sendbuf); |
| 313 |
return -1; |
| 314 |
} |
| 315 |
} |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
int udpsocket_set_buffer_size(int sd, uint32_t bufsize) |
| 320 |
{ |
| 321 |
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, sizeof(uint32_t)) < 0) |
| 322 |
return -1; |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
int udpsocket_set_buffer_send_size(int sd, uint32_t bufsize) |
| 327 |
{ |
| 328 |
if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, sizeof(uint32_t)) < 0) |
| 329 |
return -1; |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
uint32_t udpsocket_get_buffer_size(int sd) |
| 334 |
{ |
| 335 |
uint32_t bufsize; |
| 336 |
socklen_t val_size = sizeof(uint32_t); |
| 337 |
if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, &val_size) < 0) |
| 338 |
return 0; |
| 339 |
return bufsize; |
| 340 |
} |
| 341 |
|
| 342 |
uint32_t udpsocket_get_buffer_send_size(int sd) |
| 343 |
{ |
| 344 |
uint32_t bufsize; |
| 345 |
socklen_t val_size = sizeof(uint32_t); |
| 346 |
if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, &val_size) < 0) |
| 347 |
return 0; |
| 348 |
return bufsize; |
| 349 |
} |
| 350 |
|
| 351 |
int udpsocket_set_mcast_iface(int sd, const char *mciface, uint16_t family) |
| 352 |
{ |
| 353 |
#ifndef _WIN32 |
| 354 |
int scope = if_nametoindex(mciface); |
| 355 |
#else |
| 356 |
int scope = atoi(mciface); |
| 357 |
#endif |
| 358 |
if (scope == 0) |
| 359 |
return -1; |
| 360 |
#ifdef _WIN32 |
| 361 |
RIST_MARK_UNUSED(family); |
| 362 |
return setsockopt(sd, SOL_IP, IP_MULTICAST_IF, (char *)&scope, sizeof(scope)); |
| 363 |
#else |
| 364 |
if (family == AF_INET6) { |
| 365 |
return setsockopt(sd, SOL_IPV6, IPV6_MULTICAST_IF, &scope, sizeof(scope)); |
| 366 |
} else { |
| 367 |
struct ip_mreqn req = { .imr_ifindex = scope }; |
| 368 |
return setsockopt(sd, SOL_IP, IP_MULTICAST_IF, &req, sizeof(req)); |
| 369 |
} |
| 370 |
return -1; |
| 371 |
#endif |
| 372 |
} |
| 373 |
|
| 374 |
bool is_ip_address(const char *ipaddress, int family) { |
| 375 |
union { |
| 376 |
struct in_addr v4; |
| 377 |
struct in6_addr v6; |
| 378 |
} buf; |
| 379 |
return inet_pton(family, ipaddress, &buf) == 1; |
| 380 |
} |
| 381 |
|
| 382 |
int udpsocket_join_mcast_group(int sd, const char* miface, struct sockaddr* sa, uint16_t family, const char *ssm_source) { |
| 383 |
char mcastaddress[INET6_ADDRSTRLEN]; |
| 384 |
uint32_t src_addr = htonl(INADDR_ANY); |
| 385 |
int ifindex = 0; |
| 386 |
|
| 387 |
if (miface != NULL && miface[0] != '\0') { |
| 388 |
if (is_ip_address(miface, AF_INET)) { |
| 389 |
inet_pton(AF_INET, miface, &src_addr); |
| 390 |
} else { |
| 391 |
#ifndef _WIN32 |
| 392 |
ifindex = if_nametoindex(miface); |
| 393 |
#else |
| 394 |
ifindex = atoi(miface); |
| 395 |
#endif |
| 396 |
if (!ifindex) { |
| 397 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to get interface index error: %s\n", strerror(errno)); |
| 398 |
rist_log_priv3(RIST_LOG_INFO, "Falling back to joining via default route\n"); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
if (family == AF_INET6) { |
| 404 |
struct sockaddr_in6 *mcast_v6 = (struct sockaddr_in6 *)sa; |
| 405 |
inet_ntop(AF_INET6, &mcast_v6->sin6_addr, mcastaddress, INET6_ADDRSTRLEN); |
| 406 |
#ifdef MCAST_JOIN_GROUP |
| 407 |
struct group_req gr; |
| 408 |
memset(&gr, 0, sizeof(gr)); |
| 409 |
gr.gr_interface = ifindex; |
| 410 |
memcpy(&gr.gr_group, sa, sizeof(struct sockaddr_in6)); |
| 411 |
rist_log_priv3(RIST_LOG_INFO, "Joining IPv6 multicast address: %s\n", mcastaddress); |
| 412 |
if (setsockopt(sd, IPPROTO_IPV6, MCAST_JOIN_GROUP, (const char *)&gr, sizeof(gr)) == 0) |
| 413 |
return 0; |
| 414 |
#endif |
| 415 |
#ifdef IPV6_JOIN_GROUP |
| 416 |
{ |
| 417 |
struct ipv6_mreq mreq6; |
| 418 |
memcpy(&mreq6.ipv6mr_multiaddr, &mcast_v6->sin6_addr, sizeof(mreq6.ipv6mr_multiaddr)); |
| 419 |
mreq6.ipv6mr_interface = ifindex; |
| 420 |
if (setsockopt(sd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (const char *)&mreq6, sizeof(mreq6)) < 0) { |
| 421 |
#ifdef _WIN32 |
| 422 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to join IPv6 multicast group: WSAGetLastError=%d\n", WSAGetLastError()); |
| 423 |
#else |
| 424 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to join IPv6 multicast group: %s\n", strerror(errno)); |
| 425 |
#endif |
| 426 |
return -1; |
| 427 |
} |
| 428 |
return 0; |
| 429 |
} |
| 430 |
#endif |
| 431 |
rist_log_priv3(RIST_LOG_ERROR, "IPv6 multicast join not supported on this platform\n"); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
struct sockaddr_in *mcast_v4 = (struct sockaddr_in *)sa; |
| 437 |
inet_ntop(AF_INET, &(mcast_v4->sin_addr), mcastaddress, INET_ADDRSTRLEN); |
| 438 |
|
| 439 |
|
| 440 |
if (ssm_source != NULL && ssm_source[0] != '\0') { |
| 441 |
#ifdef IP_ADD_SOURCE_MEMBERSHIP |
| 442 |
struct ip_mreq_source mreqs; |
| 443 |
memset(&mreqs, 0, sizeof(mreqs)); |
| 444 |
mreqs.imr_multiaddr = mcast_v4->sin_addr; |
| 445 |
mreqs.imr_interface.s_addr = src_addr; |
| 446 |
if (inet_pton(AF_INET, ssm_source, &mreqs.imr_sourceaddr) != 1) { |
| 447 |
rist_log_priv3(RIST_LOG_ERROR, "Invalid SSM source address: %s\n", ssm_source); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
rist_log_priv3(RIST_LOG_INFO, "SSM join: group %s source %s\n", mcastaddress, ssm_source); |
| 451 |
if (setsockopt(sd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (const char *)&mreqs, sizeof(mreqs)) < 0) { |
| 452 |
#ifdef _WIN32 |
| 453 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to join SSM group (IP_ADD_SOURCE_MEMBERSHIP): WSAGetLastError=%d\n", WSAGetLastError()); |
| 454 |
#else |
| 455 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to join SSM group (IP_ADD_SOURCE_MEMBERSHIP): %s\n", strerror(errno)); |
| 456 |
#endif |
| 457 |
return -1; |
| 458 |
} |
| 459 |
return 0; |
| 460 |
#else |
| 461 |
rist_log_priv3(RIST_LOG_ERROR, "SSM (source-specific multicast) not supported on this platform\n"); |
| 462 |
return -1; |
| 463 |
#endif |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
#ifdef MCAST_JOIN_GROUP |
| 468 |
if (ifindex) { |
| 469 |
struct group_req gr; |
| 470 |
memset(&gr, 0, sizeof(gr)); |
| 471 |
gr.gr_interface = ifindex; |
| 472 |
memcpy(&gr.gr_group, mcast_v4, sizeof(*mcast_v4)); |
| 473 |
rist_log_priv3(RIST_LOG_INFO, "Joining multicast address: %s with %s\n", mcastaddress, miface); |
| 474 |
if (setsockopt(sd, SOL_IP, MCAST_JOIN_GROUP, (const char *)&gr, sizeof(gr)) == 0) { |
| 475 |
return 0; |
| 476 |
} |
| 477 |
} |
| 478 |
#endif |
| 479 |
{ |
| 480 |
char address[INET6_ADDRSTRLEN]; |
| 481 |
inet_ntop(AF_INET, &(src_addr), address, INET_ADDRSTRLEN); |
| 482 |
rist_log_priv3(RIST_LOG_INFO, "Joining multicast address: %s from IP %s\n", mcastaddress, address); |
| 483 |
} |
| 484 |
struct ip_mreq group; |
| 485 |
group.imr_multiaddr.s_addr = mcast_v4->sin_addr.s_addr; |
| 486 |
group.imr_interface.s_addr = src_addr; |
| 487 |
if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) { |
| 488 |
rist_log_priv3(RIST_LOG_ERROR, "Failed to join multicast group\n"); |
| 489 |
return -1; |
| 490 |
} |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int udpsocket_open_connect(const char *host, uint16_t port, const char *mciface) |
| 495 |
{ |
| 496 |
int sd; |
| 497 |
struct sockaddr_in6 raw; |
| 498 |
uint16_t addrlen; |
| 499 |
uint16_t proto; |
| 500 |
uint32_t ttlcmd; |
| 501 |
const uint32_t ttl = UDPSOCKET_MAX_HOPS; |
| 502 |
|
| 503 |
if (udpsocket_resolve_host(host, port, (struct sockaddr *)&raw) < 0) |
| 504 |
return -1; |
| 505 |
|
| 506 |
sd = udpsocket_open(raw.sin6_family); |
| 507 |
if (sd < 0) |
| 508 |
return sd; |
| 509 |
|
| 510 |
if (raw.sin6_family == AF_INET6) { |
| 511 |
addrlen = sizeof(struct sockaddr_in6); |
| 512 |
proto = IPPROTO_IPV6; |
| 513 |
ttlcmd = IPV6_MULTICAST_HOPS; |
| 514 |
} else { |
| 515 |
addrlen = sizeof(struct sockaddr_in); |
| 516 |
proto = IPPROTO_IP; |
| 517 |
ttlcmd = IP_MULTICAST_TTL; |
| 518 |
} |
| 519 |
|
| 520 |
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(int)) < 0) { |
| 521 |
|
| 522 |
#ifdef _WIN32 |
| 523 |
rist_log_priv3( RIST_LOG_ERROR,"Cannot set SO_REUSEADDR: WSAGetLastError=%d\n", WSAGetLastError()); |
| 524 |
#else |
| 525 |
rist_log_priv3( RIST_LOG_ERROR,"Cannot set SO_REUSEADDR: %s\n", strerror(errno)); |
| 526 |
#endif |
| 527 |
} |
| 528 |
if (setsockopt(sd, proto, ttlcmd, (char *)&ttl, sizeof(ttl)) < 0) { |
| 529 |
|
| 530 |
#ifdef _WIN32 |
| 531 |
rist_log_priv3( RIST_LOG_ERROR,"Cannot set socket MAX HOPS: WSAGetLastError=%d\n", WSAGetLastError()); |
| 532 |
#else |
| 533 |
rist_log_priv3( RIST_LOG_ERROR,"Cannot set socket MAX HOPS: %s\n", strerror(errno)); |
| 534 |
#endif |
| 535 |
} |
| 536 |
if (mciface && mciface[0] != '\0') |
| 537 |
udpsocket_set_mcast_iface(sd, mciface, raw.sin6_family); |
| 538 |
|
| 539 |
if (connect(sd, (struct sockaddr *)&raw, addrlen) < 0) { |
| 540 |
#ifdef _WIN32 |
| 541 |
int winerr = WSAGetLastError(); |
| 542 |
rist_log_priv3( RIST_LOG_ERROR, "connect() failed: WSAGetLastError=%d\n", winerr); |
| 543 |
udpsocket_close(sd); |
| 544 |
return -1; |
| 545 |
#else |
| 546 |
int err = errno; |
| 547 |
udpsocket_close(sd); |
| 548 |
errno = err; |
| 549 |
return -1; |
| 550 |
#endif |
| 551 |
} |
| 552 |
udpsocket_set_dontfragment(sd, raw.sin6_family); |
| 553 |
|
| 554 |
return sd; |
| 555 |
} |
| 556 |
|
| 557 |
int udpsocket_open_bind_mcast(const char *host, uint16_t port, const char *mciface, |
| 558 |
uint32_t ttl, const char *ssm_source) |
| 559 |
{ |
| 560 |
int sd; |
| 561 |
struct sockaddr_in6 raw; |
| 562 |
uint16_t addrlen; |
| 563 |
if (udpsocket_resolve_host(host, port, (struct sockaddr *)&raw) < 0) |
| 564 |
return -1; |
| 565 |
|
| 566 |
sd = udpsocket_open(raw.sin6_family); |
| 567 |
if (sd < 0) |
| 568 |
return sd; |
| 569 |
|
| 570 |
int is_multicast = 0; |
| 571 |
if (raw.sin6_family == AF_INET6) { |
| 572 |
addrlen = sizeof(struct sockaddr_in6); |
| 573 |
is_multicast = IN6_IS_ADDR_MULTICAST(&raw.sin6_addr); |
| 574 |
} else { |
| 575 |
struct sockaddr_in *tmp = (struct sockaddr_in*)&raw; |
| 576 |
addrlen = sizeof(struct sockaddr_in); |
| 577 |
is_multicast = IN_MULTICAST(ntohl(tmp->sin_addr.s_addr)); |
| 578 |
} |
| 579 |
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(int)) < 0) { |
| 580 |
#ifdef _WIN32 |
| 581 |
rist_log_priv3(RIST_LOG_ERROR, "Cannot set SO_REUSEADDR: WSAGetLastError=%d\n", WSAGetLastError()); |
| 582 |
#else |
| 583 |
rist_log_priv3(RIST_LOG_ERROR, "Cannot set SO_REUSEADDR: %s\n", strerror(errno)); |
| 584 |
#endif |
| 585 |
} |
| 586 |
if (bind(sd, (struct sockaddr *)&raw, addrlen) < 0) { |
| 587 |
#ifdef _WIN32 |
| 588 |
rist_log_priv3(RIST_LOG_ERROR, "Could not bind to interface: WSAGetLastError=%d\n", WSAGetLastError()); |
| 589 |
#else |
| 590 |
rist_log_priv3(RIST_LOG_ERROR, "Could not bind to interface: %s\n", strerror(errno)); |
| 591 |
#endif |
| 592 |
udpsocket_close(sd); |
| 593 |
return -1; |
| 594 |
} |
| 595 |
udpsocket_set_dontfragment(sd, raw.sin6_family); |
| 596 |
if (is_multicast) { |
| 597 |
if (ttl > 0) |
| 598 |
udpsocket_set_mcast_ttl(sd, raw.sin6_family, ttl); |
| 599 |
if (udpsocket_join_mcast_group(sd, mciface, (struct sockaddr *)&raw, raw.sin6_family, ssm_source) != 0) { |
| 600 |
rist_log_priv3(RIST_LOG_ERROR, "Could not join multicast group: %s on %s\n", host, mciface); |
| 601 |
udpsocket_close(sd); |
| 602 |
return -1; |
| 603 |
} |
| 604 |
} |
| 605 |
return sd; |
| 606 |
} |
| 607 |
|
| 608 |
int udpsocket_open_bind(const char *host, uint16_t port, const char *mciface) |
| 609 |
{ |
| 610 |
return udpsocket_open_bind_mcast(host, port, mciface, 0, NULL); |
| 611 |
} |
| 612 |
|
| 613 |
int udpsocket_set_nonblocking(int sd) |
| 614 |
{ |
| 615 |
#ifdef _WIN32 |
| 616 |
u_long iMode=1; |
| 617 |
if (ioctlsocket(sd, FIONBIO, &iMode) != 0) { |
| 618 |
rist_log_priv3(RIST_LOG_WARN, "ioctlsocket(FIONBIO) failed: WSAGetLastError=%d\n", WSAGetLastError()); |
| 619 |
return -1; |
| 620 |
} |
| 621 |
#else |
| 622 |
RIST_MARK_UNUSED(sd); |
| 623 |
#endif |
| 624 |
return 0; |
| 625 |
} |
| 626 |
|
| 627 |
int udpsocket_send_nonblocking(int sd, const void *buf, size_t size) |
| 628 |
{ |
| 629 |
return (int)send(sd, buf, size, MSG_DONTWAIT); |
| 630 |
} |
| 631 |
|
| 632 |
int udpsocket_send(int sd, const void *buf, size_t size) |
| 633 |
{ |
| 634 |
return (int)send(sd, buf, size, 0); |
| 635 |
} |
| 636 |
|
| 637 |
int udpsocket_sendto(int sd, const void *buf, size_t size, const char *host, uint16_t port) |
| 638 |
{ |
| 639 |
struct sockaddr_in6 raw; |
| 640 |
uint16_t addrlen; |
| 641 |
if (udpsocket_resolve_host(host, port, (struct sockaddr *)&raw) < 0) |
| 642 |
return -1; |
| 643 |
|
| 644 |
if (raw.sin6_family == AF_INET6) |
| 645 |
addrlen = sizeof(struct sockaddr_in6); |
| 646 |
else |
| 647 |
addrlen = sizeof(struct sockaddr_in); |
| 648 |
return (int)sendto(sd, buf, size, 0, (struct sockaddr *)(&raw), addrlen); |
| 649 |
} |
| 650 |
|
| 651 |
int udpsocket_recv(int sd, void *buf, size_t size) |
| 652 |
{ |
| 653 |
return (int)recv(sd, buf, size, 0); |
| 654 |
} |
| 655 |
|
| 656 |
int udpsocket_recvfrom(int sd, void *buf, size_t size, int flags, struct sockaddr *addr, socklen_t *addr_len) |
| 657 |
{ |
| 658 |
return (int)recvfrom(sd, buf, size, flags, addr, addr_len); |
| 659 |
} |
| 660 |
|
| 661 |
int udpsocket_close(int sd) |
| 662 |
{ |
| 663 |
#ifndef _WIN32 |
| 664 |
return close(sd); |
| 665 |
#else |
| 666 |
return closesocket(sd); |
| 667 |
#endif |
| 668 |
} |
| 669 |
|
| 670 |
int udpsocket_parse_url_parameters(char *url, udpsocket_url_param_t *params, int max_params, |
| 671 |
uint32_t *clean_url_len) |
| 672 |
{ |
| 673 |
const char* query = NULL; |
| 674 |
int i = 0; |
| 675 |
char *token = NULL; |
| 676 |
|
| 677 |
query = strchr( url, '?' ); |
| 678 |
if (query != NULL) |
| 679 |
*clean_url_len = (uint32_t)(query - url + 1); |
| 680 |
else |
| 681 |
*clean_url_len = (uint32_t)(strlen(url) + 1); |
| 682 |
|
| 683 |
if (!query || *query == '\0') |
| 684 |
return -1; |
| 685 |
if (!params || max_params == 0) |
| 686 |
return 0; |
| 687 |
|
| 688 |
const char amp[2] = "&"; |
| 689 |
token = strtok( (char*)query + 1, amp ); |
| 690 |
while (token != NULL && i < max_params) { |
| 691 |
params[i].key = token; |
| 692 |
params[i].val = NULL; |
| 693 |
if ((params[i].val = strchr( params[i].key, '=' )) != NULL) { |
| 694 |
size_t val_len = strlen( params[i].val ); |
| 695 |
*(params[i].val) = '\0'; |
| 696 |
if (val_len > 1) { |
| 697 |
params[i].val++; |
| 698 |
if (params[i].key[0]) |
| 699 |
i++; |
| 700 |
}; |
| 701 |
} |
| 702 |
token = strtok( NULL, amp ); |
| 703 |
} |
| 704 |
return i; |
| 705 |
} |
| 706 |
|
| 707 |
int udpsocket_parse_url(char *url, char *address, int address_maxlen, uint16_t *port, int *local) |
| 708 |
{ |
| 709 |
char *p_port = NULL, *p_addr = (char *)url; |
| 710 |
int using_sqbrkts = 0; |
| 711 |
char *p; |
| 712 |
if (!url) |
| 713 |
return -1; |
| 714 |
|
| 715 |
p = url; |
| 716 |
if (strlen(p) < 1) |
| 717 |
return -1; |
| 718 |
|
| 719 |
while (1) { |
| 720 |
char *p_slash; |
| 721 |
p_slash = strchr(p, '/'); |
| 722 |
if (!p_slash) |
| 723 |
break; |
| 724 |
p = p_slash + 1; |
| 725 |
} |
| 726 |
p_addr = p; |
| 727 |
if (*p_addr == '@') { |
| 728 |
*local = 1; |
| 729 |
p_addr++; |
| 730 |
} else |
| 731 |
*local = 0; |
| 732 |
|
| 733 |
if (*p_addr == '[') { |
| 734 |
using_sqbrkts = 1; |
| 735 |
p_addr++; |
| 736 |
} |
| 737 |
p = p_addr; |
| 738 |
if (using_sqbrkts) { |
| 739 |
char *p_end; |
| 740 |
p_end = strchr(p, ']'); |
| 741 |
if (!p_end) |
| 742 |
return -1; |
| 743 |
*p_end = 0; |
| 744 |
p = p_end + 1; |
| 745 |
} |
| 746 |
p_port = strchr(p, ':'); |
| 747 |
if (p_port) { |
| 748 |
*p_port = 0; |
| 749 |
p_port++; |
| 750 |
} |
| 751 |
if (p_port && (strlen(p_port) > 0)) |
| 752 |
*port = (uint16_t)atoi(p_port); |
| 753 |
|
| 754 |
if (strlen(p_addr) > 0) { |
| 755 |
strncpy(address, p_addr, address_maxlen); |
| 756 |
address[address_maxlen - 1] = '\0'; |
| 757 |
} else if ( !using_sqbrkts) { |
| 758 |
sprintf(address, "0.0.0.0"); |
| 759 |
} else { |
| 760 |
sprintf(address, "::"); |
| 761 |
} |
| 762 |
return 0; |
| 763 |
} |