31#if defined(__FreeBSD__) || defined(__OpenBSD__)
33#include <netinet/in.h>
34#include <sys/socket.h>
60 int connectionTypeIndex = 0;
67 int autoSearchIndex = 1;
79bool TCP::ISNewText(
const char *dev,
const char *name,
char *texts[],
char *names[],
int n)
123 LOG_INFO(
"LAN search is enabled. When connecting, the driver shall attempt to "
124 "communicate with all devices on the local network until a connection is "
127 LOG_INFO(
"Auto search is disabled.");
142 struct sockaddr_in serv_addr;
143 struct hostent *hp =
nullptr;
153 LOGF_INFO(
"Connecting to %s@%s ...", hostname.c_str(),
port.c_str());
155 LOGF_DEBUG(
"Connecting to %s@%s ...", hostname.c_str(),
port.c_str());
159 hp = gethostbyname(hostname.c_str());
163 LOG_ERROR(
"Failed to lookup IP Address or hostname.");
167 memset(&serv_addr, 0,
sizeof(serv_addr));
168 serv_addr.sin_family = AF_INET;
169 serv_addr.sin_addr.s_addr = ((
struct in_addr *)(hp->h_addr_list[0]))->s_addr;
170 serv_addr.sin_port = htons(atoi(
port.c_str()));
175 socketType = SOCK_STREAM;
179 socketType = SOCK_DGRAM;
182 if ((
m_SockFD = socket(AF_INET, socketType, 0)) < 0)
189 setsockopt(
m_SockFD, SOL_SOCKET, SO_RCVTIMEO, &ts,
sizeof(
struct timeval));
190 setsockopt(
m_SockFD, SOL_SOCKET, SO_SNDTIMEO, &ts,
sizeof(
struct timeval));
193 if (::connect(
m_SockFD, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
196 LOGF_ERROR(
"Failed to connect to %s@%s: %s.", hostname.c_str(),
port.c_str(), strerror(
errno));
212 LOG_ERROR(
"Error! Server address is missing or invalid.");
216 bool handshakeResult =
true;
226 handshakeResult =
false;
227 std::regex ipv4(
"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
228 const auto isIPv4 = regex_match(hostname, ipv4);
234 LOGF_DEBUG(
"Connection to %s@%s is successful, attempting handshake...", hostname.c_str(),
port.c_str());
248 if (handshakeResult ==
false &&
252 size_t found = hostname.find_last_of(
".");
254 if (found != std::string::npos)
257 const auto sourceSubnet = hostname.substr(0, found);
258 std::deque<std::string> subnets;
262 for (
auto &oneInterfaceAddress : addrs_ipv4)
265 if (oneInterfaceAddress.rfind(
"127", 0) == 0)
268 size_t found = oneInterfaceAddress.find_last_of(
".");
269 if (found != std::string::npos)
272 const auto targetSubnet = oneInterfaceAddress.substr(0, found);
274 if (targetSubnet == sourceSubnet)
275 subnets.push_front(targetSubnet);
277 subnets.push_back(targetSubnet);
282 for (
auto &oneSubnet : subnets)
284 LOGF_INFO(
"Searching %s subnet, this operation will take a few minutes to complete. Stand by...", oneSubnet.c_str());
288 for (
int i = 1; i < 255; i++)
290 const auto newAddress = oneSubnet +
"." + std::to_string(i);
291 if (newAddress == hostname)
297 LOGF_DEBUG(
"Connection to %s@%s is successful, attempting handshake...", hostname.c_str(),
port.c_str());
301 hostname = newAddress;
333 return handshakeResult;
408 AddressTP[1].setText(std::to_string(addressPort));
The Interface class is the base class for all INDI connection plugins.
virtual Type type()
type Return connection type
const char * getDeviceName()
std::function< bool()> Handshake
INDI::DefaultDevice * m_Device
void setDefaultHost(const char *addressHost)
int m_ConfigConnectionType
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
static constexpr uint8_t SOCKET_TIMEOUT
void setLANSearchEnabled(bool enabled)
virtual std::string name() override
virtual bool Disconnect() override
Disconnect Disconnect from device.
void setConnectionType(int type)
TODO should be renamed to setDefaultConnectionType.
virtual void Activated() override
Activated Function called by the framework when the plugin is activated (i.e. selected by the user)....
void setDefaultPort(uint32_t addressPort)
INDI::PropertyText AddressTP
Properties.
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
INDI::PropertySwitch LANSearchSP
TCP(INDI::DefaultDevice *dev, IPerm permission=IP_RW)
INDI::PropertySwitch TcpUdpSP
virtual uint32_t port() const
virtual bool saveConfigItems(FILE *fp) override
bool establishConnection(const std::string &hostname, const std::string &port, int timeout=-1)
establishConnection Create a socket connection to the host and port. If successful,...
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
virtual void Deactivated() override
Deactivated Function called by the framework when the plugin is deactivated. It is usually used to de...
const char * getDeviceName() const
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
bool isInitializationComplete() const
isInitializationComplete Check if driver initialization is complete.
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
bool isSimulation() const
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
void save(FILE *f) const
save Save property to configuration file.
const char * getName() const
bool isNameMatch(const char *otherName) const
bool update(const ISState states[], const char *const names[], int n)
int findOnSwitchIndex() const
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, ISRule rule, double timeout, IPState state)
bool update(const char *const texts[], const char *const names[], int n)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state)
INDI::PropertyViewText * getText() const
IPerm
Permission hint, with respect to client.
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
IUGetConfigOnSwitchIndex Opens configuration file and reads single switch property to find ON switch ...
#define LOGF_INFO(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
const char * CONNECTION_TAB
const char * DEVICE_AUTO_SEARCH
Toggle device auto search. If enabled and on connection failure with the default port,...
const char * DEVICE_ADDRESS
Device hostname and port. It is part of Connection::TCPInterface to manage connections to devices ove...
const char * DEVICE_LAN_SEARCH
Toggle device LAN search. If the initial handshake with the specified hostname and port number fails,...
std::vector< std::string > getInterfaceAddressesV4()