Automatically determine most suitable local IP to talk to ambed

pull/191/head
Geoffrey Merck 5 years ago
parent f5f6f444d7
commit 56c5171364

@ -20,8 +20,6 @@ PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME="xlxd"
DAEMON="/xlxd/xlxd"
ARGUMENTS="XLX999 192.168.1.240 127.0.0.1"
#Case where ambed runs on another host and you want to use another IP to talk to it. Here ambed is on 10.9.0.100 and 10.9.0.1 is used to talk to it
#ARGUMENTS="XLX999 192.168.1.240 10.9.0.100 10.9.0.1
PIDFILE="/var/log/xlxd.pid"
USER=root
GROUP=root

@ -99,7 +99,7 @@ bool CCodecStream::Init(uint16 uiPort)
m_uiPort = uiPort;
// create our socket
ok = m_Socket.Open(CIp("0.0.0.0"), uiPort);
ok = m_Socket.Open(g_Reflector.GetTranscoderLocalIp(), uiPort);
if ( ok )
{
// start thread;

@ -3,7 +3,7 @@
// xlxd
//
// Created by Jean-Luc Deltombe (LX3JL) on 31/10/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reremoteed.
//
// ----------------------------------------------------------------------------
// This file is part of xlxd.
@ -24,6 +24,7 @@
#include "main.h"
#include <string.h>
#include <unistd.h>
#include "cip.h"
#include <netdb.h>
@ -88,4 +89,49 @@ CIp::operator const char *() const
return ::inet_ntoa(m_Addr.sin_addr);
}
////////////////////////////////////////////////////////////////////////////////////////
// operator
void CIp::GetLocalIp(const CIp & remoteIp, const CIp & defaultLocalIp, CIp & localIp)
{
localIp = CIp(defaultLocalIp);
int remotePort = 10100;
struct sockaddr_in remote;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
//Socket could not be created
if(sock < 0)
{
std::cout << "Socket error" << std::endl;
}
memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = remoteIp.m_Addr.sin_addr.s_addr;
remote.sin_port = htons(remotePort);
int err = connect(sock, (const struct sockaddr*)&remote, sizeof(remote));
if (err < 0)
{
std::cout << "Error number: " << errno
<< ". Error message: " << strerror(errno) << std::endl;
}
struct sockaddr_in name;
socklen_t namelen = sizeof(name);
err = ::getsockname(sock, (struct sockaddr*)&name, &namelen);
char buffer[80];
const char* p = inet_ntop(AF_INET, &name.sin_addr, buffer, 80);
if(p != NULL)
{
localIp = CIp(&name);
//std::cout << "Local IP address for " << remoteIp << " is: " << buffer << std::endl;
}
else
{
//std::cout << "Error number: " << errno << ". Error message: " << strerror(errno) << std::endl;
}
::close(sock);
}

@ -53,6 +53,9 @@ public:
// operator
bool operator ==(const CIp &) const;
operator const char *() const;
// Factory
static void GetLocalIp(const CIp & remoteIp, const CIp & defaultLocalIp, CIp & localIp);
protected:
// data

@ -59,8 +59,10 @@ public:
const CCallsign &GetCallsign(void) const { return m_Callsign; }
void SetListenIp(const CIp &ip) { m_Ip = ip; }
void SetTranscoderIp(const CIp &ip) { m_AmbedIp = ip; }
void SetTranscoderLocalIp(const CIp &ip) { m_AmbedLocalIp = ip; }
const CIp &GetListenIp(void) const { return m_Ip; }
const CIp &GetTranscoderIp(void) const { return m_AmbedIp; }
const CIp &GetTranscoderLocalIp(void) const { return m_AmbedLocalIp; }
// operation
bool Start(void);
@ -121,6 +123,7 @@ protected:
CCallsign m_Callsign;
CIp m_Ip;
CIp m_AmbedIp;
CIp m_AmbedLocalIp;
// objects
CUsers m_Users; // sorted list of lastheard stations

@ -97,7 +97,7 @@ bool CTranscoder::Init(void)
m_Ip = g_Reflector.GetTranscoderIp();
// create our socket
ok = m_Socket.Open(CIp("0.0.0.0"), TRANSCODER_PORT);
ok = m_Socket.Open(g_Reflector.GetTranscoderLocalIp(), TRANSCODER_PORT);
if ( ok )
{
// start thread;

@ -99,8 +99,19 @@ int main(int argc, const char * argv[])
// initialize reflector
g_Reflector.SetCallsign(argv[1]);
g_Reflector.SetListenIp(CIp(argv[2]));
g_Reflector.SetTranscoderIp(CIp(CIp(argv[3])));
CIp listenIp = CIp(argv[2]);
CIp localTranscoderIp = CIp(listenIp);
CIp transcoderIp = CIp(argv[3]);
if(transcoderIp.GetAddr() != CIp("127.0.0.1").GetAddr())
{
CIp::GetLocalIp(transcoderIp, listenIp, localTranscoderIp);
}
g_Reflector.SetListenIp(listenIp);
g_Reflector.SetTranscoderIp(transcoderIp);
g_Reflector.SetTranscoderLocalIp(localTranscoderIp);
// and let it run
if ( !g_Reflector.Start() )
@ -109,7 +120,9 @@ int main(int argc, const char * argv[])
exit(EXIT_FAILURE);
}
std::cout << "Reflector " << g_Reflector.GetCallsign()
<< "started and listening on " << g_Reflector.GetListenIp() << std::endl;
<< "started and listening on " << g_Reflector.GetListenIp() << std::endl
<< "Listening for ambed " << g_Reflector.GetTranscoderIp()
<< " on " << g_Reflector.GetTranscoderLocalIp() << std::endl;
#ifdef RUN_AS_DAEMON
// run forever

Loading…
Cancel
Save

Powered by TurnKey Linux.