+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * include/avx_discovery_server.h - Centralized discovery server (TCP port 7959) |
| 7 | */ |
| 8 | |
| 9 | #ifndef AVX_DISCOVERY_SERVER_H |
| 10 | #define AVX_DISCOVERY_SERVER_H |
| 11 | |
| 12 | #include <stdint.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stddef.h> |
| 15 | #include "avx_platform.h" |
| 16 | #include "openavx.h" |
| 17 | |
| 18 | #define AVX_DISCOVERY_SERVER_DEFAULT_PORT 7959 |
| 19 | #define AVX_DISCOVERY_SERVER_MAX_SOURCES 256 |
| 20 | #define AVX_DISCOVERY_SERVER_MAX_CLIENTS 128 |
| 21 | |
| 22 | #define AVX_RELAY_SERVER_DEFAULT_PORT 7960 |
| 23 | #define AVX_RELAY_MAGIC 0x55585241 /* 'ARXU' - OpenAVX Relay UDP */ |
| 24 | #define AVX_STUN_MAGIC 0x55585341 /* 'ASXU' - OpenAVX STUN UDP */ |
| 25 | |
| 26 | #define AVX_RELAY_CHANNEL_MEDIA 0 |
| 27 | #define AVX_RELAY_CHANNEL_CONTROL 1 |
| 28 | |
| 29 | #define AVX_RELAY_FLAG_DATA 0x00 |
| 30 | #define AVX_RELAY_FLAG_BIND 0x01 |
| 31 | #define AVX_RELAY_FLAG_ACK 0x02 |
| 32 | #define AVX_RELAY_FLAG_HEARTBEAT 0x04 |
| 33 | #define AVX_RELAY_FLAG_LEAVE 0x08 |
| 34 | |
| 35 | #define AVX_RELAY_MAX_SESSIONS 256 |
| 36 | #define AVX_RELAY_MAX_SUBSCRIBERS_PER_SESSION 64 |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | AVX_PACKED_START |
| 43 | typedef struct { |
| 44 | uint32_t magic; /* AVX_RELAY_MAGIC (0x55585241) */ |
| 45 | uint16_t session_id; /* Session ID (1..65535) */ |
| 46 | uint8_t channel; /* AVX_RELAY_CHANNEL_MEDIA (0) or AVX_RELAY_CHANNEL_CONTROL (1) */ |
| 47 | uint8_t flags; /* AVX_RELAY_FLAG_* */ |
| 48 | } AVX_PACKED avx_relay_header_t; |
| 49 | |
| 50 | typedef struct { |
| 51 | uint32_t magic; /* AVX_STUN_MAGIC (0x55585341) */ |
| 52 | uint32_t cookie; /* Transaction/correlation ID */ |
| 53 | uint16_t family; /* Address family (AF_INET or AF_INET6) */ |
| 54 | uint16_t mapped_port; /* Reflexive port (network byte order) */ |
| 55 | uint8_t mapped_ip[16]; /* Reflexive IP (4 bytes for IPv4, 16 for IPv6) */ |
| 56 | } AVX_PACKED avx_stun_msg_t; |
| 57 | AVX_PACKED_END |
| 58 | |
| 59 | typedef struct { |
| 60 | char name[256]; |
| 61 | char url[256]; |
| 62 | char groups[256]; |
| 63 | avx_socket_t owner_fd; |
| 64 | char public_ip[64]; |
| 65 | uint16_t public_port; |
| 66 | } avx_server_source_entry_t; |
| 67 | |
| 68 | typedef struct { |
| 69 | const char *bind_ip; /* IP to bind (NULL or "0.0.0.0" for dual-stack wildcard) */ |
| 70 | uint16_t tcp_port; /* TCP discovery port (default: 7959) */ |
| 71 | uint16_t relay_udp_port; /* UDP relay & STUN port (default: 7960) */ |
| 72 | bool enable_relay; /* Whether to enable UDP relay & STUN reflection (default: true) */ |
| 73 | const char *public_ip; /* Optional advertised public IP of the relay server */ |
| 74 | } avx_discovery_server_config_t; |
| 75 | |
| 76 | typedef struct { |
| 77 | uint32_t active_sessions; |
| 78 | uint32_t active_subscribers; |
| 79 | uint64_t packets_relayed; |
| 80 | uint64_t bytes_relayed; |
| 81 | } avx_relay_stats_t; |
| 82 | |
| 83 | typedef struct avx_discovery_server_t avx_discovery_server_t; |
| 84 | |
| 85 | /** |
| 86 | * Creates and starts a centralized discovery server (with UDP relay enabled by default on port 7960). |
| 87 | */ |
| 88 | OPENAVX_API avx_discovery_server_t* avx_discovery_server_create(const char *bind_ip, uint16_t port); |
| 89 | |
| 90 | /** |
| 91 | * Creates and starts a centralized discovery and relay server with custom configuration. |
| 92 | */ |
| 93 | OPENAVX_API avx_discovery_server_t* avx_discovery_server_create_ex(const avx_discovery_server_config_t *cfg); |
| 94 | |
| 95 | /** |
| 96 | * Stops the discovery server and frees resources. |
| 97 | */ |
| 98 | OPENAVX_API void avx_discovery_server_destroy(avx_discovery_server_t *srv); |
| 99 | |
| 100 | /** |
| 101 | * Retrieves current statistics of the UDP relay. |
| 102 | */ |
| 103 | OPENAVX_API bool avx_relay_server_get_stats(avx_discovery_server_t *srv, avx_relay_stats_t *out_stats); |
| 104 | |
| 105 | /** |
| 106 | * Client Helper: Register a sender with a discovery server. |
| 107 | */ |
| 108 | OPENAVX_API int avx_discovery_client_register(const char *server_ip, uint16_t server_port, |
| 109 | const char *name, const char *url, const char *groups); |
| 110 | |
| 111 | /** |
| 112 | * Client Helper: Query active sources from a discovery server. |
| 113 | */ |
| 114 | OPENAVX_API int avx_discovery_client_query(const char *server_ip, uint16_t server_port, |
| 115 | char names[][256], char urls[][256], size_t max_sources); |
| 116 | |
| 117 | /** |
| 118 | * Client Helper: Resolve stream details from discovery server (returns local URL and public reflexive IP:port). |
| 119 | */ |
| 120 | OPENAVX_API int avx_discovery_client_resolve(const char *server_ip, uint16_t server_port, |
| 121 | const char *name, char *out_url, size_t out_url_max, |
| 122 | char *out_wan_addr, size_t out_wan_max); |
| 123 | |
| 124 | /** |
| 125 | * Client Helper: Request NAT hole punching for a stream via discovery signaling. |
| 126 | */ |
| 127 | OPENAVX_API int avx_discovery_client_request_punch(const char *server_ip, uint16_t server_port, |
| 128 | const char *name, const char *local_candidate, |
| 129 | char *out_peer_candidate, size_t out_cand_max); |
| 130 | |
| 131 | /** |
| 132 | * Client Helper: STUN-like query using a caller-provided UDP socket to discover its reflexive address. |
| 133 | */ |
| 134 | OPENAVX_API int avx_discovery_client_get_reflexive_addr_sock(avx_socket_t sock, |
| 135 | const char *server_ip, uint16_t udp_port, |
| 136 | char *out_ip, uint16_t *out_port); |
| 137 | |
| 138 | /** |
| 139 | * Client Helper: STUN-like query to discover reflexive address (creates temporary socket). |
| 140 | */ |
| 141 | OPENAVX_API int avx_discovery_client_get_reflexive_addr(const char *server_ip, uint16_t udp_port, |
| 142 | char *out_ip, uint16_t *out_port); |
| 143 | |
| 144 | /** |
| 145 | * Client Helper: Bind a socket to a relay session (as publisher or subscriber). |
| 146 | */ |
| 147 | OPENAVX_API int avx_relay_client_bind_session(avx_socket_t sock, const char *relay_ip, uint16_t relay_port, |
| 148 | uint32_t session_id, bool is_publisher); |
| 149 | |
| 150 | /** |
| 151 | * Client Helper: Send an encapsulated packet through the UDP relay. |
| 152 | */ |
| 153 | OPENAVX_API int avx_relay_send_packet(avx_socket_t sock, const struct sockaddr *relay_addr, avx_socklen_t addr_len, |
| 154 | uint32_t session_id, uint8_t channel, |
| 155 | const void *payload, size_t payload_len); |
| 156 | |
| 157 | #ifdef __cplusplus |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | #endif /* AVX_DISCOVERY_SERVER_H */ |