1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2013-2021 by Sander Jansen. All Rights Reserved *
* --- *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses. *
********************************************************************************/
#include "ap_defs.h"
#include "ap_thread_queue.h"
#ifndef _WIN32
#include <unistd.h> // for close()
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h> // for getaddrinfo()
#endif
// AI_ADDRCONFIG not available on OpenBSD
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0
#endif
#include "ap_connect.h"
#include "ap_socket.h"
namespace ap {
ConnectionFactory::ConnectionFactory(){
}
ConnectionFactory::~ConnectionFactory(){
}
Socket * ConnectionFactory::create(FXint domain,FXint type,FXint protocol) {
Socket * io = nullptr;
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (use_ssl)
io = new SecureSocket();
else
io = new Socket();
#else
io = new Socket();
#endif
if (io->create(domain,type,protocol,0)==false){
delete io;
return nullptr;
}
io->setSendTimeout(10_s);
io->setReceiveTimeout(10_s);
return io;
}
FXIO* ConnectionFactory::open(const FXString & hostname,FXint port,FXbool ssl) {
struct addrinfo hints;
struct addrinfo * list=nullptr;
struct addrinfo * item=nullptr;
FXint result;
memset(&hints,0,sizeof(struct addrinfo));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags|=(AI_NUMERICSERV|AI_ADDRCONFIG);
// Automatically enable ssl for 443
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
use_ssl = ssl;
if (use_ssl) GM_DEBUG_PRINT("[connection] using SSL on port %d\n",port);
#else
if (ssl) {
GM_DEBUG_PRINT("[connection] no support for SSL\n");
return nullptr;
}
#endif
result=getaddrinfo(hostname.text(),FXString::value(port).text(),&hints,&list);
if (result) return nullptr;
for (item=list;item;item=item->ai_next){
Socket * io = create(item->ai_family,item->ai_socktype,item->ai_protocol);
if (io==nullptr)
continue;
switch(io->connect((const struct sockaddr*)item->ai_addr,(FXint)item->ai_addrlen)){
case 0: // connected
freeaddrinfo(list);
return io;
break;
case 1: // user interrupt, give up
delete io;
freeaddrinfo(list);
return nullptr;
break;
default: // try next
delete io;
break;
}
}
if (list) freeaddrinfo(list);
return nullptr;
}
//------------------------------------------------------------------------------
ThreadConnectionFactory::ThreadConnectionFactory(IOContext * ctx)
: context(ctx) {
}
Socket * ThreadConnectionFactory::create(FXint domain,FXint type,FXint protocol) {
Socket * io = nullptr;
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (use_ssl)
io = new ThreadSecureSocket(context);
else
io = new ThreadSocket(context);
#else
io = new ThreadSocket(context);
#endif
if (io->create(domain,type,protocol,FXIO::NonBlocking)==false){
delete io;
return nullptr;
}
io->setSendTimeout(10_s);
io->setReceiveTimeout(10_s);
return io;
}
}
|