void ThreadedSSLSocketInitiator::onStop() { SocketToThread threads; SocketToThread::iterator i; { Locker l(m_mutex); time_t start = 0; time_t now = 0; ::time(&start); while (isLoggedOn()) { if (::time(&now) - 5 >= start) break; } threads = m_threads; m_threads.clear(); } for (i = threads.begin(); i != threads.end(); ++i) ssl_socket_close(i->first.first, i->first.second); for (i = threads.begin(); i != threads.end(); ++i) { thread_join(i->second); if (i->first.second != 0) SSL_free(i->first.second); } threads.clear(); }
/** * ssl_socket_close_and_restore - Close an SSL Connection and restore Connection callbacks - Implements Connection::conn_close() */ static int ssl_socket_close_and_restore(struct Connection *conn) { int rc = ssl_socket_close(conn); conn->conn_read = raw_socket_read; conn->conn_write = raw_socket_write; conn->conn_close = raw_socket_close; return rc; }
static int tls_close (CONNECTION* conn) { int rc; rc = ssl_socket_close (conn); conn->conn_read = raw_socket_read; conn->conn_write = raw_socket_write; conn->conn_close = raw_socket_close; return rc; }
static int net_http_new_socket(struct http_connection_t *conn) { int ret; struct addrinfo *addr = NULL, *next_addr = NULL; int fd = socket_init( (void**)&addr, conn->port, conn->domain, SOCKET_TYPE_STREAM); #ifdef HAVE_SSL if (conn->sock_state.ssl) { if (!(conn->sock_state.ssl_ctx = ssl_socket_init(fd, conn->domain))) return -1; } #endif next_addr = addr; while(fd >= 0) { #ifdef HAVE_SSL if (conn->sock_state.ssl) { ret = ssl_socket_connect(conn->sock_state.ssl_ctx, (void*)next_addr, true, true); if (ret >= 0) break; ssl_socket_close(conn->sock_state.ssl_ctx); } else #endif { ret = socket_connect(fd, (void*)next_addr, true); if (ret >= 0 && socket_nonblock(fd)) break; socket_close(fd); } fd = socket_next((void**)&next_addr); } if (addr) freeaddrinfo_retro(addr); conn->sock_state.fd = fd; return fd; }
static int tls_close(conn_t * conn) { int rc; rc = ssl_socket_close(conn); /* * reset the read/write pointers */ conn->readn = conn_readn; conn->writen = conn_writen; conn->close = conn_close; return rc; }
struct http_t *net_http_new(struct http_connection_t *conn) { bool error = false; int fd = -1; struct http_t *state = NULL; if (!conn) goto error; fd = net_http_new_socket(conn); if (fd < 0) goto error; error = false; /* This is a bit lazy, but it works. */ if (conn->methodcopy) { net_http_send_str(&conn->sock_state, &error, conn->methodcopy); net_http_send_str(&conn->sock_state, &error, " /"); } else { net_http_send_str(&conn->sock_state, &error, "GET /"); } net_http_send_str(&conn->sock_state, &error, conn->location); net_http_send_str(&conn->sock_state, &error, " HTTP/1.1\r\n"); net_http_send_str(&conn->sock_state, &error, "Host: "); net_http_send_str(&conn->sock_state, &error, conn->domain); if (!conn->port) { char portstr[16]; portstr[0] = '\0'; snprintf(portstr, sizeof(portstr), ":%i", conn->port); net_http_send_str(&conn->sock_state, &error, portstr); } net_http_send_str(&conn->sock_state, &error, "\r\n"); /* this is not being set anywhere yet */ if (conn->contenttypecopy) { net_http_send_str(&conn->sock_state, &error, "Content-Type: "); net_http_send_str(&conn->sock_state, &error, conn->contenttypecopy); net_http_send_str(&conn->sock_state, &error, "\r\n"); } if (conn->methodcopy && (string_is_equal(conn->methodcopy, "POST"))) { size_t post_len, len; char *len_str = NULL; if (!conn->postdatacopy) goto error; if (!conn->contenttypecopy) net_http_send_str(&conn->sock_state, &error, "Content-Type: application/x-www-form-urlencoded\r\n"); net_http_send_str(&conn->sock_state, &error, "Content-Length: "); post_len = strlen(conn->postdatacopy); #ifdef _WIN32 len = snprintf(NULL, 0, "%" PRIuPTR, post_len); len_str = (char*)malloc(len + 1); snprintf(len_str, len + 1, "%" PRIuPTR, post_len); #else len = snprintf(NULL, 0, "%llu", (long long unsigned)post_len); len_str = (char*)malloc(len + 1); snprintf(len_str, len + 1, "%llu", (long long unsigned)post_len); #endif len_str[len] = '\0'; net_http_send_str(&conn->sock_state, &error, len_str); net_http_send_str(&conn->sock_state, &error, "\r\n"); free(len_str); } net_http_send_str(&conn->sock_state, &error, "User-Agent: libretro\r\n"); net_http_send_str(&conn->sock_state, &error, "Connection: close\r\n"); net_http_send_str(&conn->sock_state, &error, "\r\n"); if (conn->methodcopy && (string_is_equal(conn->methodcopy, "POST"))) net_http_send_str(&conn->sock_state, &error, conn->postdatacopy); if (error) goto error; state = (struct http_t*)malloc(sizeof(struct http_t)); state->sock_state = conn->sock_state; state->status = -1; state->data = NULL; state->part = P_HEADER_TOP; state->bodytype= T_FULL; state->error = false; state->pos = 0; state->len = 0; state->buflen = 512; state->data = (char*)malloc(state->buflen); if (!state->data) goto error; return state; error: if (conn) { if (conn->methodcopy) free(conn->methodcopy); if (conn->contenttypecopy) free(conn->contenttypecopy); conn->methodcopy = NULL; conn->contenttypecopy = NULL; conn->postdatacopy = NULL; } #ifdef HAVE_SSL if (conn && conn->sock_state.ssl && conn->sock_state.ssl_ctx && fd >= 0) { ssl_socket_close(conn->sock_state.ssl_ctx); ssl_socket_free(conn->sock_state.ssl_ctx); conn->sock_state.ssl_ctx = NULL; } #else if (fd >= 0) socket_close(fd); #endif if (state) free(state); return NULL; }