Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 8.8.8.8 and instead determine the local IP for each packet #1637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/satip/rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,8 @@ void satip_server_rtsp_init
reg = 1;
}
s = rtsp_ip;
rtsp_ip = strdup(bindaddr);
if(bindaddr != NULL)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that bindaddr is checked for null before calling strdup. This ensures that the function does not crash if bindaddr is null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats.... uhmmm... literally what I am doing?

rtsp_ip = strdup(bindaddr);
free(s);
rtsp_port = port;
rtsp_descramble = descramble;
Expand Down
57 changes: 31 additions & 26 deletions src/satip/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ satip_server_http_xml(http_connection_t *hc)

char buf[sizeof(MSG) + 1024], buf2[64], purl[128];
const char *cs;
char addrbuf[50];
char *devicelist = NULL;
htsbuf_queue_t q;
mpegts_network_t *mn;
Expand All @@ -135,7 +136,7 @@ satip_server_http_xml(http_connection_t *hc)
{}
};

if (http_server_ip == NULL)
if (http_server_port == 0)
return HTTP_STATUS_NOT_FOUND;

htsbuf_queue_init(&q, 0);
Expand Down Expand Up @@ -222,15 +223,22 @@ satip_server_http_xml(http_connection_t *hc)
tvheadend_webroot ?: "", cs);
}

char* own_server_ip = http_server_ip;

if(own_server_ip == NULL) {
tcp_get_str_from_ip(hc->hc_self, addrbuf, sizeof(addrbuf));
own_server_ip = addrbuf;
}

snprintf(buf, sizeof(buf), MSG,
config_get_server_name(),
buf2, tvheadend_version,
satip_server_conf.satip_uuid,
http_server_ip, http_server_port,
http_server_ip, http_server_port,
http_server_ip, http_server_port,
http_server_ip, http_server_port,
http_server_ip, http_server_port,
own_server_ip, http_server_port,
own_server_ip, http_server_port,
own_server_ip, http_server_port,
own_server_ip, http_server_port,
own_server_ip, http_server_port,
devicelist ?: "", purl);

free(devicelist);
Expand Down Expand Up @@ -378,15 +386,17 @@ DEVICEID.SES.COM: %d\r\n\r\n"
abort();
}

char* own_server_ip = http_server_ip;

snprintf(buf, sizeof(buf), MSG, UPNP_MAX_AGE,
http_server_ip, http_server_port, tvheadend_webroot ?: "",
own_server_ip ?: "%s", http_server_port, tvheadend_webroot ?: "",
nt, tvheadend_version,
satip_server_conf.satip_uuid, usn2, (long)satip_server_bootid,
satip_server_deviceid);

htsbuf_queue_init(&q, 0);
htsbuf_append_str(&q, buf);
upnp_send(&q, NULL, attempt * 11, 1);
upnp_send(&q, NULL, attempt * 11, 1, own_server_ip == NULL);
htsbuf_queue_flush(&q);
}
#undef MSG
Expand Down Expand Up @@ -420,8 +430,10 @@ CONFIGID.UPNP.ORG: 0\r\n"
buf, ntohs(IP_PORT(*dst)), deviceid ? " device: " : "", deviceid ?: "");
}

char* own_server_ip = http_server_ip;

snprintf(buf, sizeof(buf), MSG, UPNP_MAX_AGE,
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
http_server_ip, http_server_port, tvheadend_webroot ?: "",
own_server_ip ?: "%s", http_server_port, tvheadend_webroot ?: "",
tvheadend_version,
satip_server_conf.satip_uuid, (long)satip_server_bootid);

Expand All @@ -431,7 +443,7 @@ CONFIGID.UPNP.ORG: 0\r\n"
htsbuf_qprintf(&q, "DEVICEID.SES.COM: %s", deviceid);
htsbuf_append(&q, "\r\n", 2);
storage = *dst;
upnp_send(&q, &storage, 0, from_multicast);
upnp_send(&q, &storage, 0, from_multicast, own_server_ip == NULL);
htsbuf_queue_flush(&q);
#undef MSG
}
Expand Down Expand Up @@ -517,7 +529,7 @@ satips_upnp_discovery_received
return;
if (conn->multicast && strcmp(argv[0], "239.255.255.250"))
return;
if (!conn->multicast && strcmp(argv[0], http_server_ip))
if (!conn->multicast && http_server_ip != NULL && strcmp(argv[0], http_server_ip))
return;

if (tvhtrace_enabled()) {
Expand Down Expand Up @@ -584,8 +596,8 @@ static void satip_server_info(const char *prefix, int descramble, int muxcnf)
}
tvhinfo(LS_SATIPS, "SAT>IP Server %sinitialized", prefix);
tvhinfo(LS_SATIPS, " HTTP %s:%d, RTSP %s:%d",
http_server_ip, http_server_port,
http_server_ip, satip_server_rtsp_port);
http_server_ip ?: "0.0.0.0", http_server_port,
http_server_ip ?: "0.0.0.0", satip_server_rtsp_port);
tvhinfo(LS_SATIPS, " descramble %d, muxcnf %d",
descramble, muxcnf);
for (fe = 1; fe <= 128; fe++) {
Expand Down Expand Up @@ -988,18 +1000,10 @@ static void satip_server_init_common(const char *prefix, int announce)
if (satip_server_rtsp_port <= 0)
return;

if (http_server_ip == NULL) {
if (tcp_server_onall(http_server) && satip_server_bindaddr == NULL) {
tvherror(LS_SATIPS, "use --satip_bindaddr parameter to select the local IP for SAT>IP");
tvherror(LS_SATIPS, "using Google lookup (might block the task until timeout)");
}
if (tcp_server_bound(http_server, &http, PF_INET) < 0) {
tvherror(LS_SATIPS, "Unable to determine the HTTP/RTSP address");
return;
}
tcp_get_str_from_ip(&http, http_ip, sizeof(http_ip));
http_server_ip = strdup(satip_server_bindaddr ?: http_ip);
http_server_port = ntohs(IP_PORT(http));
if (http_server_port == 0) {
tcp_server_onall(http_server);
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
http_server_ip = satip_server_bindaddr ? strdup(satip_server_bindaddr) : null;
http_server_port = ntohs(IP_PORT((tcp_server_t *)http_server));
}

descramble = satip_server_conf.satip_descramble;
Expand Down Expand Up @@ -1082,7 +1086,7 @@ void satip_server_register(void)
tvh_uuid_t u;
int save = 0;

if (http_server_ip == NULL)
if (http_server_port == 0)
return;

if (satip_server_conf.satip_rtsp != satip_server_rtsp_port) {
Expand Down Expand Up @@ -1142,6 +1146,7 @@ void satip_server_done(void)
satip_server_rtsp_port = 0;
free(http_server_ip);
http_server_ip = NULL;
http_server_port = 0;
free(satip_server_conf.satip_uuid);
satip_server_conf.satip_uuid = NULL;
free(satip_server_bindaddr);
Expand Down
48 changes: 0 additions & 48 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,54 +1013,6 @@ tcp_server_delete(void *server)
tvh_write(tcp_server_pipe.wr, &c, 1);
}

/**
*
*/
int
tcp_default_ip_addr ( struct sockaddr_storage *deflt, int family )
{

struct sockaddr_storage ss;
socklen_t ss_len;
int sock;

memset(&ss, 0, sizeof(ss));
ss.ss_family = family == PF_UNSPEC ? tcp_preferred_address_family : family;
if (inet_pton(ss.ss_family,
ss.ss_family == AF_INET ?
/* Google name servers */
"8.8.8.8" : "2001:4860:4860::8888",
IP_IN_ADDR(ss)) <= 0)
return -1;

IP_PORT_SET(ss, htons(53));

sock = tvh_socket(ss.ss_family, SOCK_STREAM, 0);
if (sock < 0)
return -1;

if (connect(sock, (struct sockaddr *)&ss, IP_IN_ADDRLEN(ss)) < 0) {
close(sock);
return -1;
}

ss_len = sizeof(ss);
if (getsockname(sock, (struct sockaddr *)&ss, &ss_len) < 0) {
close(sock);
return -1;
}

if (ss.ss_family == AF_INET)
IP_AS_V4(&ss, port) = 0;
else
IP_AS_V6(&ss, port) = 0;

memset(deflt, 0, sizeof(*deflt));
memcpy(deflt, &ss, ss_len);

close(sock);
return 0;
}

/**
*
Expand Down
4 changes: 0 additions & 4 deletions src/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ void tcp_server_register(void *server);

void tcp_server_delete(void *server);

int tcp_default_ip_addr(struct sockaddr_storage *deflt, int family);

int tcp_server_bound(void *server, struct sockaddr_storage *bound, int family);

int tcp_server_onall(void *server);

int tcp_read(int fd, void *buf, size_t len);
Expand Down
64 changes: 62 additions & 2 deletions src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,56 @@ udp_close( udp_connection_t *uc )
free(uc);
}

int
udp_write_fill_source( udp_connection_t *uc, const void *buf, size_t len,
struct sockaddr_storage *storage)
{
int r;
struct sockaddr_in local_addr;
socklen_t local_addr_len = sizeof(local_addr);

if (storage == NULL)
storage = &uc->ip;

// Connect the socket to the destination address
if (connect(uc->fd, (struct sockaddr *)storage, sizeof(*storage)) < 0) {
tvherror(uc->subsystem, "connect() failed: %s", strerror(errno));
return -1;
}

// Get the local endpoint information (source IP address)
if (getsockname(uc->fd, (struct sockaddr *)&local_addr, &local_addr_len) == -1) {
tvherror(uc->subsystem, "getsockname() failed: %s", strerror(errno));
return -1;
}

tvhdebug(uc->subsystem, "Got source IP address: %s\n", inet_ntoa(local_addr.sin_addr));

len += strlen(inet_ntoa(local_addr.sin_addr)) - 2;
char* data = malloc(len + 1);
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
memcpy(data, buf, len);
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
data[len] = 0;
snprintf(data, len + 1, inet_ntoa(local_addr.sin_addr));

// Send data over the established connection
while (len) {
r = write(uc->fd, data, len);
if (r < 0) {
if (ERRNO_AGAIN(errno)) {
tvh_safe_usleep(100);
continue;
}
break;
}
len -= r;
data += r;
}

free(data);

return len;
}

int
udp_write( udp_connection_t *uc, const void *buf, size_t len,
struct sockaddr_storage *storage )
Expand Down Expand Up @@ -517,7 +567,14 @@ udp_write( udp_connection_t *uc, const void *buf, size_t len,

int
udp_write_queue( udp_connection_t *uc, htsbuf_queue_t *q,
struct sockaddr_storage *storage )
struct sockaddr_storage *storage)
{
return udp_write_queue_fill_source(uc, q, storage);
}

int
udp_write_queue_fill_source( udp_connection_t *uc, htsbuf_queue_t *q,
struct sockaddr_storage *storage, int fill_source)
{
htsbuf_data_t *hd;
int l, r = 0;
Expand All @@ -527,7 +584,10 @@ udp_write_queue( udp_connection_t *uc, htsbuf_queue_t *q,
if (!r) {
l = hd->hd_data_len - hd->hd_data_off;
p = hd->hd_data + hd->hd_data_off;
r = udp_write(uc, p, l, storage);
if(fill_source)
r = udp_write_fill_source(uc, p, l, storage);
else
r = udp_write(uc, p, l, storage);
}
htsbuf_data_free(q, hd);
}
Expand Down
3 changes: 3 additions & 0 deletions src/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ udp_write( udp_connection_t *uc, const void *buf, size_t len,
int
udp_write_queue( udp_connection_t *uc, htsbuf_queue_t *q,
struct sockaddr_storage *storage );
int
udp_write_queue_fill_source( udp_connection_t *uc, htsbuf_queue_t *q,
struct sockaddr_storage *storage, int fill_source );

typedef struct udp_multirecv {
int um_psize;
Expand Down
10 changes: 6 additions & 4 deletions src/upnp.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef struct upnp_data {
htsbuf_queue_t queue;
int delay_ms;
int from_multicast;
int fill_source;
} upnp_data_t;

TAILQ_HEAD(upnp_data_queue_write, upnp_data);
Expand Down Expand Up @@ -75,7 +76,7 @@ void upnp_service_destroy( upnp_service_t *us )
*/
void
upnp_send( htsbuf_queue_t *q, struct sockaddr_storage *storage,
int delay_ms, int from_multicast )
int delay_ms, int from_multicast, int fill_source )
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
{
upnp_data_t *data;

Expand All @@ -90,6 +91,7 @@ upnp_send( htsbuf_queue_t *q, struct sockaddr_storage *storage,
data->storage = *storage;
data->delay_ms = delay_ms;
data->from_multicast = from_multicast;
data->fill_source = fill_source;
tvh_mutex_lock(&upnp_lock);
TAILQ_INSERT_TAIL(&upnp_data_write, data, data_link);
tvh_mutex_unlock(&upnp_lock);
Expand Down Expand Up @@ -185,8 +187,8 @@ upnp_thread( void *aux )
if (data == NULL)
break;
upnp_dump_data(data);
udp_write_queue(data->from_multicast ? multicast : unicast,
&data->queue, &data->storage);
udp_write_queue_fill_source(data->from_multicast ? multicast : unicast,
Flole998 marked this conversation as resolved.
Show resolved Hide resolved
&data->queue, &data->storage, data->fill_source);
htsbuf_queue_flush(&data->queue);
free(data);
delay_ms = 0;
Expand All @@ -204,7 +206,7 @@ upnp_thread( void *aux )
break;
tvh_safe_usleep((long)data->delay_ms * 1000);
upnp_dump_data(data);
udp_write_queue(unicast, &data->queue, &data->storage);
udp_write_queue_fill_source(unicast, &data->queue, &data->storage, data->fill_source);
htsbuf_queue_flush(&data->queue);
free(data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/upnp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ upnp_service_t *upnp_service_create0(upnp_service_t *us);
void upnp_service_destroy(upnp_service_t *service);

void upnp_send(htsbuf_queue_t *q, struct sockaddr_storage *storage,
int delay_ms, int from_multicast);
int delay_ms, int from_multicast, int fill_source);

void upnp_server_init(const char *bindaddr);
void upnp_server_done(void);
Expand Down
Loading