Пример #1
0
static int establish_logical_layer(wsh_t *wsh)
{

	if (!wsh->sanity) {
		return -1;
	}

	if (wsh->logical_established) {
		return 0;
	}

	if (wsh->secure && !wsh->secure_established) {
		int code;

		if (!wsh->ssl) {
			wsh->ssl = SSL_new(wsh->ssl_ctx);
			assert(wsh->ssl);

			SSL_set_fd(wsh->ssl, wsh->sock);
		}

		do {
			code = SSL_accept(wsh->ssl);

			if (code == 1) {
				wsh->secure_established = 1;
				break;
			}

			if (code == 0) {
				return -1;
			}
			
			if (code < 0) {
				if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
					return -1;
				}
			}

			if (wsh->block) {
				ms_sleep(10);
			} else {
				ms_sleep(1);
			}

			wsh->sanity--;

			if (!wsh->block) {
				return -2;
			}

		} while (wsh->sanity > 0);
		
		if (!wsh->sanity) {
			return -1;
		}
		
	}

	while (!wsh->down && !wsh->handshake) {
		int r = ws_handshake(wsh);

		if (r < 0) {
			wsh->down = 1;
			return -1;
		}

		if (!wsh->handshake && !wsh->block) {
			return -2;
		}

	}

	wsh->logical_established = 1;
	
	return 0;
}
Пример #2
0
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock)
{
	memset(wsh, 0, sizeof(*wsh));
	wsh->sock = sock;

	if (!ssl_ctx) {
		ssl_ctx = ws_globals.ssl_ctx;
	}

	if (close_sock) {
		wsh->close_sock = 1;
	}

	wsh->buflen = sizeof(wsh->buffer);
	wsh->secure = ssl_ctx ? 1 : 0;

	setup_socket(sock);

	if (wsh->secure) {
		int code;
		int sanity = 500;
		
		wsh->ssl = SSL_new(ssl_ctx);
		assert(wsh->ssl);

		SSL_set_fd(wsh->ssl, wsh->sock);

		do {
			code = SSL_accept(wsh->ssl);

			if (code == 1) {
				break;
			}

			if (code == 0) {
				return -1;
			}
			
			if (code < 0) {
				if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
					return -1;
				}
			}
#ifndef _MSC_VER
				usleep(10000);
#else
				Sleep(10);
#endif				
				
		} while (--sanity > 0);
		
		if (!sanity) {
			return -1;
		}
		
	}

	while (!wsh->down && !wsh->handshake) {
		int r = ws_handshake(wsh);

		if (r < 0) {
			wsh->down = 1;
			return -1;
		}
	}

	if (wsh->down) {
		return -1;
	}

	return 0;
}