/** Complete non-blocking connect()-sequence. Check access and * terminate connection, if trouble detected. * @param cptr Client to which we have connected, with all ConfItem structs attached. * @return Zero on failure (caller should exit_client()), non-zero on success. */ static int completed_connection(struct Client* cptr) { struct ConfItem *aconf; time_t newts; struct Client *acptr; int i; assert(0 != cptr); /* * get the socket status from the fd first to check if * connection actually succeeded */ if ((cli_error(cptr) = os_get_sockerr(cli_fd(cptr)))) { const char* msg = strerror(cli_error(cptr)); if (!msg) msg = "Unknown error"; sendto_opmask_butone(0, SNO_OLDSNO, "Connection failed to %s: %s", cli_name(cptr), msg); return 0; } if (!(aconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_SERVER))) { sendto_opmask_butone(0, SNO_OLDSNO, "Lost Server Line for %s", cli_name(cptr)); return 0; } if (s_state(&(cli_socket(cptr))) == SS_CONNECTING) socket_state(&(cli_socket(cptr)), SS_CONNECTED); if (!EmptyString(aconf->passwd)) sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd); /* * Create a unique timestamp */ newts = TStime(); for (i = HighestFd; i > -1; --i) { if ((acptr = LocalClientArray[i]) && (IsServer(acptr) || IsHandshake(acptr))) { if (cli_serv(acptr)->timestamp >= newts) newts = cli_serv(acptr)->timestamp + 1; } } assert(0 != cli_serv(cptr)); cli_serv(cptr)->timestamp = newts; SetHandshake(cptr); /* * Make us timeout after twice the timeout for DNS look ups */ cli_lasttime(cptr) = CurrentTime; ClearPingSent(cptr); sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6n :%s", cli_name(&me), cli_serv(&me)->timestamp, newts, MAJOR_PROTOCOL, NumServCap(&me), feature_bool(FEAT_HUB) ? "h" : "", cli_info(&me)); return (IsDead(cptr)) ? 0 : 1; }
/* * auth_sock_callback - called when an event occurs on the socket */ static void auth_sock_callback(struct Event* ev) { struct AuthRequest* auth; assert(0 != ev_socket(ev)); assert(0 != s_data(ev_socket(ev))); auth = s_data(ev_socket(ev)); switch (ev_type(ev)) { case ET_DESTROY: /* being destroyed */ auth->flags &= ~AM_SOCKET; if (!(auth->flags & AM_FREE_MASK)) { Debug((DEBUG_LIST, "Freeing auth from sock callback; %p [%p]", auth, ev_socket(ev))); MyFree(auth); /* done with it finally */ } break; case ET_CONNECT: /* socket connection completed */ Debug((DEBUG_LIST, "Connection completed for auth %p [%p]; sending query", auth, ev_socket(ev))); socket_state(&auth->socket, SS_CONNECTED); send_auth_query(auth); break; case ET_READ: /* socket is readable */ case ET_EOF: /* end of file on socket */ case ET_ERROR: /* error on socket */ Debug((DEBUG_LIST, "Auth socket %p [%p] readable", auth, ev_socket(ev))); read_auth_reply(auth); break; default: #ifndef NDEBUG abort(); /* unrecognized event */ #endif break; } }
void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarativeBluetoothService *service) { if (d->m_socket){ delete d->m_socket; } d->m_service = service; d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; d->m_error = NoError; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); socket_state(socket->state()); emit connectedChanged(); }
void connect() { Q_ASSERT(!uri.isEmpty()); m_error = QLatin1String("No Error"); if (m_socket) m_socket->deleteLater(); m_socket = new QLlcpSocket; Q_Q(const QDeclarativeNearFieldSocket); QObject::connect(m_socket, SIGNAL(connected()), q, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), q, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QLlcpSocket::SocketError)), q, SLOT(socket_error(QLlcpSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QLlcpSocket::SocketState)), q, SLOT(socket_state(QLlcpSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), q, SLOT(socket_readyRead())); m_socket->connectToService(0, uri); }
void connect() { Q_ASSERT(m_service); qDebug() << "Connecting to: " << m_service->serviceInfo()->device().address().toString(); m_error = QLatin1String("No Error"); if(m_socket) m_socket->deleteLater(); // delete m_socket; m_socket = new QBluetoothSocket(); m_socket->connectToService(*m_service->serviceInfo()); QObject::connect(m_socket, SIGNAL(connected()), m_dbs, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), m_dbs, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)), m_dbs, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), m_dbs, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), m_dbs, SLOT(socket_readyRead())); m_stream = new QDataStream(m_socket); }
QDeclarativeBluetoothSocket::QDeclarativeBluetoothSocket(QBluetoothSocket *socket, QDeclarativeBluetoothService *service, QObject *parent) : QObject(parent) { d = new QDeclarativeBluetoothSocketPrivate(this); d->m_service = service; d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); d->m_stream = new QDataStream(socket); }
void connect() { Q_ASSERT(m_service); //qDebug() << "Connecting to: " << m_service->serviceInfo()->device().address().toString(); m_error = QDeclarativeBluetoothSocket::NoError; if (m_socket) m_socket->deleteLater(); QBluetoothServiceInfo::Protocol socketProtocol; if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::L2capProtocol) socketProtocol = QBluetoothServiceInfo::L2capProtocol; else if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::RfcommProtocol) socketProtocol = QBluetoothServiceInfo::RfcommProtocol; else socketProtocol = QBluetoothServiceInfo::UnknownProtocol; m_socket = new QBluetoothSocket(socketProtocol); m_socket->connectToService(*m_service->serviceInfo()); QObject::connect(m_socket, SIGNAL(connected()), m_dbs, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), m_dbs, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)), m_dbs, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), m_dbs, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), m_dbs, SLOT(socket_readyRead())); }
DECLARE_TEST(udp, datagram_ipv6) { network_address_t** address_local = 0; network_address_t* address = 0; network_address_t* address_server = 0; test_datagram_arg_t client_arg[4]; int server_port; int state, iaddr, asize; thread_t threads[5]; socket_t* sock_server; socket_t* sock_client[4]; if (!network_supports_ipv6()) return 0; sock_server = udp_socket_allocate(); sock_client[0] = udp_socket_allocate(); sock_client[1] = udp_socket_allocate(); sock_client[2] = udp_socket_allocate(); sock_client[3] = udp_socket_allocate(); address_local = network_address_local(); for (iaddr = 0, asize = array_size(address_local); iaddr < asize; ++iaddr) { if (network_address_family(address_local[iaddr]) == NETWORK_ADDRESSFAMILY_IPV6) { address = address_local[iaddr]; break; } } EXPECT_NE(address, 0); do { server_port = random32_range(1024, 35535); network_address_ip_set_port(address, server_port); if (socket_bind(sock_server, address)) break; } while (true); address_server = network_address_clone(address); network_address_ip_set_port(address_server, server_port); network_address_array_deallocate(address_local); state = socket_state(sock_server); EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED); state = socket_state(sock_client[0]); EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED); state = socket_state(sock_client[1]); EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED); state = socket_state(sock_client[2]); EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED); state = socket_state(sock_client[3]); EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED); socket_set_blocking(sock_server, true); socket_set_blocking(sock_client[0], true); socket_set_blocking(sock_client[1], true); socket_set_blocking(sock_client[2], true); socket_set_blocking(sock_client[3], true); client_arg[0].sock = sock_client[0]; client_arg[0].target = address_server; client_arg[1].sock = sock_client[1]; client_arg[1].target = address_server; client_arg[2].sock = sock_client[2]; client_arg[2].target = address_server; client_arg[3].sock = sock_client[3]; client_arg[3].target = address_server; thread_initialize(&threads[0], datagram_server_blocking_thread, sock_server, STRING_CONST("server_thread"), THREAD_PRIORITY_NORMAL, 0); thread_initialize(&threads[1], datagram_client_blocking_thread, &client_arg[0], STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0); thread_initialize(&threads[2], datagram_client_blocking_thread, &client_arg[1], STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0); thread_initialize(&threads[3], datagram_client_blocking_thread, &client_arg[2], STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0); thread_initialize(&threads[4], datagram_client_blocking_thread, &client_arg[3], STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0); thread_start(&threads[0]); thread_start(&threads[1]); thread_start(&threads[2]); thread_start(&threads[3]); thread_start(&threads[4]); test_wait_for_threads_startup(threads, 5); thread_finalize(&threads[0]); thread_finalize(&threads[1]); thread_finalize(&threads[2]); thread_finalize(&threads[3]); thread_finalize(&threads[4]); socket_deallocate(sock_server); socket_deallocate(sock_client[0]); socket_deallocate(sock_client[1]); socket_deallocate(sock_client[2]); socket_deallocate(sock_client[3]); memory_deallocate(address_server); return 0; }
DECLARE_TEST(udp, stream_ipv6) { network_address_t** address_local = 0; network_address_t* address = 0; int server_port, client_port; int state, iaddr, asize; thread_t threads[2]; socket_t* sock_server; socket_t* sock_client; if (!network_supports_ipv6()) return 0; sock_server = udp_socket_allocate(); sock_client = udp_socket_allocate(); address_local = network_address_local(); for (iaddr = 0, asize = array_size(address_local); iaddr < asize; ++iaddr) { if (network_address_family(address_local[iaddr]) == NETWORK_ADDRESSFAMILY_IPV6) { address = address_local[iaddr]; break; } } EXPECT_NE(address, 0); do { server_port = random32_range(1024, 35535); network_address_ip_set_port(address, server_port); if (socket_bind(sock_server, address)) break; } while (true); do { client_port = random32_range(1024, 35535); network_address_ip_set_port(address, client_port); if (socket_bind(sock_client, address)) break; } while (true); socket_set_blocking(sock_server, false); socket_set_blocking(sock_client, false); network_address_ip_set_port(address, client_port); socket_connect(sock_server, address, 0); network_address_ip_set_port(address, server_port); socket_connect(sock_client, address, 0); network_address_array_deallocate(address_local); state = socket_state(sock_server); EXPECT_TRUE(state == SOCKETSTATE_CONNECTED); state = socket_state(sock_client); EXPECT_TRUE(state == SOCKETSTATE_CONNECTED); socket_set_blocking(sock_server, true); socket_set_blocking(sock_client, true); thread_initialize(&threads[0], stream_blocking_thread, sock_server, STRING_CONST("io_thread"), THREAD_PRIORITY_NORMAL, 0); thread_initialize(&threads[1], stream_blocking_thread, sock_client, STRING_CONST("io_thread"), THREAD_PRIORITY_NORMAL, 0); thread_start(&threads[0]); thread_start(&threads[1]); test_wait_for_threads_startup(threads, 2); thread_finalize(&threads[0]); thread_finalize(&threads[1]); socket_deallocate(sock_server); socket_deallocate(sock_client); return 0; }
DECLARE_TEST(tcp, bind) { bool has_ipv4 = network_supports_ipv4(); bool has_ipv6 = network_supports_ipv6(); bool was_bound = false; unsigned int port; if (has_ipv4) { socket_t* sock = tcp_socket_allocate(); EXPECT_EQ(socket_address_local(sock), 0); EXPECT_EQ(socket_address_remote(sock), 0); EXPECT_EQ(socket_state(sock), SOCKETSTATE_NOTCONNECTED); for (port = 31890; !was_bound && (port < 32890); ++port) { network_address_t* address = network_address_ipv4_any(); network_address_ip_set_port(address, port); if (socket_bind(sock, address)) { EXPECT_NE(socket_address_local(sock), 0); EXPECT_EQ(socket_address_remote(sock), 0); EXPECT_EQ(socket_state(sock), SOCKETSTATE_NOTCONNECTED); EXPECT_TRUE(network_address_equal(socket_address_local(sock), address)); was_bound = true; } memory_deallocate(address); } EXPECT_TRUE(was_bound); socket_deallocate(sock); } if (has_ipv6) { socket_t* sock = tcp_socket_allocate(); was_bound = false; for (port = 31890; !was_bound && (port < 32890); ++port) { network_address_t* address = network_address_ipv6_any(); network_address_ip_set_port(address, port); if (socket_bind(sock, address)) { EXPECT_NE(socket_address_local(sock), 0); EXPECT_EQ(socket_address_remote(sock), 0); EXPECT_EQ(socket_state(sock), SOCKETSTATE_NOTCONNECTED); EXPECT_TRUE(network_address_equal(socket_address_local(sock), address)); was_bound = true; } memory_deallocate(address); } EXPECT_TRUE(was_bound); socket_deallocate(sock); } return 0; }
/** Attempt to send a sequence of bytes to the connection. * As a side effect, updates \a cptr's FLAG_BLOCKED setting * and sendB/sendK fields. * @param cptr Client that should receive data. * @param buf Message buffer to send to client. * @return Negative on connection-fatal error; otherwise * number of bytes sent. */ unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf) { unsigned int bytes_written = 0; unsigned int bytes_count = 0; assert(0 != cptr); #if defined(USE_SSL) switch (client_sendv(cptr, buf, &bytes_count, &bytes_written)) { #else switch (os_sendv_nonb(cli_fd(cptr), buf, &bytes_count, &bytes_written)) { #endif case IO_SUCCESS: ClrFlag(cptr, FLAG_BLOCKED); cli_sendB(cptr) += bytes_written; cli_sendB(&me) += bytes_written; /* A partial write implies that future writes will block. */ if (bytes_written < bytes_count) SetFlag(cptr, FLAG_BLOCKED); break; case IO_BLOCKED: SetFlag(cptr, FLAG_BLOCKED); break; case IO_FAILURE: cli_error(cptr) = errno; SetFlag(cptr, FLAG_DEADSOCKET); break; } return bytes_written; } /** Complete non-blocking connect()-sequence. Check access and * terminate connection, if trouble detected. * @param cptr Client to which we have connected, with all ConfItem structs attached. * @return Zero on failure (caller should exit_client()), non-zero on success. */ static int completed_connection(struct Client* cptr) { struct ConfItem *aconf; time_t newts; struct Client *acptr; int i; #if defined(USE_SSL) char *sslfp; int r; #endif assert(0 != cptr); /* * get the socket status from the fd first to check if * connection actually succeeded */ if ((cli_error(cptr) = os_get_sockerr(cli_fd(cptr)))) { const char* msg = strerror(cli_error(cptr)); if (!msg) msg = "Unknown error"; sendto_opmask(0, SNO_OLDSNO, "Connection failed to %s: %s", cli_name(cptr), msg); return 0; } if (!(aconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_SERVER))) { sendto_opmask(0, SNO_OLDSNO, "Lost Server Line for %s", cli_name(cptr)); return 0; } #if defined(USE_SSL) if (aconf->flags & CONF_SSL) { r = ssl_connect(&(cli_socket(cptr))); if (r == -1) { sendto_opmask(0, SNO_OLDSNO, "Connection failed to %s: SSL error", cli_name(cptr)); return 0; } else if (r == 0) return 1; sslfp = ssl_get_fingerprint(cli_socket(cptr).s_ssl); if (sslfp) ircd_strncpy(cli_sslclifp(cptr), sslfp, BUFSIZE+1); SetSSL(cptr); } #endif if (s_state(&(cli_socket(cptr))) == SS_CONNECTING) socket_state(&(cli_socket(cptr)), SS_CONNECTED); if (!EmptyString(aconf->passwd)) sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd); /* * Create a unique timestamp */ newts = TStime(); for (i = HighestFd; i > -1; --i) { if ((acptr = LocalClientArray[i]) && (IsServer(acptr) || IsHandshake(acptr))) { if (cli_serv(acptr)->timestamp >= newts) newts = cli_serv(acptr)->timestamp + 1; } } assert(0 != cli_serv(cptr)); cli_serv(cptr)->timestamp = newts; SetHandshake(cptr); /* * Make us timeout after twice the timeout for DNS look ups */ cli_lasttime(cptr) = CurrentTime; ClearPingSent(cptr); /* TODO: NEGOCIACION envia_config_req(cptr); */ sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s", cli_name(&me), cli_serv(&me)->timestamp, newts, MAJOR_PROTOCOL, NumServCap(&me), feature_bool(FEAT_HUB) ? "h" : "", cli_info(&me)); #if defined(DDB) ddb_burst(cptr); #endif return (IsDead(cptr)) ? 0 : 1; } /** Close the physical connection. Side effects: MyConnect(cptr) * becomes false and cptr->from becomes NULL. * @param cptr Client to disconnect. */ void close_connection(struct Client *cptr) { struct ConfItem* aconf; if (IsServer(cptr)) { ServerStats->is_sv++; ServerStats->is_sbs += cli_sendB(cptr); ServerStats->is_sbr += cli_receiveB(cptr); ServerStats->is_sti += CurrentTime - cli_firsttime(cptr); /* * If the connection has been up for a long amount of time, schedule * a 'quick' reconnect, else reset the next-connect cycle. */ if ((aconf = find_conf_exact(cli_name(cptr), cptr, CONF_SERVER))) { /* * Reschedule a faster reconnect, if this was a automatically * connected configuration entry. (Note that if we have had * a rehash in between, the status has been changed to * CONF_ILLEGAL). But only do this if it was a "good" link. */ aconf->hold = CurrentTime; aconf->hold += ((aconf->hold - cli_since(cptr) > feature_int(FEAT_HANGONGOODLINK)) ? feature_int(FEAT_HANGONRETRYDELAY) : ConfConFreq(aconf)); /* if (nextconnect > aconf->hold) */ /* nextconnect = aconf->hold; */ } } else if (IsUser(cptr)) { ServerStats->is_cl++; ServerStats->is_cbs += cli_sendB(cptr); ServerStats->is_cbr += cli_receiveB(cptr); ServerStats->is_cti += CurrentTime - cli_firsttime(cptr); } else ServerStats->is_ni++; #if defined(USE_ZLIB) /* * Siempre es una conexion nuestra */ if (cli_connect(cptr)->zlib_negociation & ZLIB_IN) { inflateEnd(cli_connect(cptr)->comp_in); MyFree(cli_connect(cptr)->comp_in); } if (cli_connect(cptr)->zlib_negociation & ZLIB_OUT) { deflateEnd(cli_connect(cptr)->comp_out); MyFree(cli_connect(cptr)->comp_out); } #endif if (-1 < cli_fd(cptr)) { flush_connections(cptr); LocalClientArray[cli_fd(cptr)] = 0; close(cli_fd(cptr)); socket_del(&(cli_socket(cptr))); /* queue a socket delete */ cli_fd(cptr) = -1; cli_freeflag(cptr) &= ~FREEFLAG_SOCKET; } SetFlag(cptr, FLAG_DEADSOCKET); MsgQClear(&(cli_sendQ(cptr))); client_drop_sendq(cli_connect(cptr)); DBufClear(&(cli_recvQ(cptr))); memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr))); set_snomask(cptr, 0, SNO_SET); det_confs_butmask(cptr, 0); if (cli_listener(cptr)) { release_listener(cli_listener(cptr)); cli_listener(cptr) = 0; } for ( ; HighestFd > 0; --HighestFd) { if (LocalClientArray[HighestFd]) break; } } /** Close all unregistered connections. * @param source Oper who requested the close. * @return Number of closed connections. */ int net_close_unregistered_connections(struct Client* source) { int i; struct Client* cptr; int count = 0; assert(0 != source); for (i = HighestFd; i > 0; --i) { if ((cptr = LocalClientArray[i]) && !IsRegistered(cptr)) { send_reply(source, RPL_CLOSING, get_client_name(source, HIDE_IP)); exit_client(source, cptr, &me, "Oper Closing"); ++count; } } return count; }