int shim_insert(struct shim **shimp, struct tcp_conn *tc, int layer,
		shim_frame_h *frameh, void *arg)
{
	struct shim *shim;
	int err;

	if (!shimp || !tc || !frameh)
		return EINVAL;

	shim = mem_zalloc(sizeof(*shim), destructor);
	if (!shim)
		return ENOMEM;

	shim->tc = mem_ref(tc);
	err = tcp_register_helper(&shim->th, tc, layer, NULL,
				  shim_send_handler,
				  shim_recv_handler, shim);
	if (err)
		goto out;

	shim->frameh = frameh;
	shim->arg = arg;

 out:
	if (err)
		mem_deref(shim);
	else
		*shimp = shim;

	return err;
}
示例#2
0
文件: tls_tcp.c 项目: kaija/libre
/**
 * Start TLS on a TCP-connection
 *
 * @param ptc   Pointer to allocated TLS connectioon
 * @param tls   TLS Context
 * @param tcp   TCP Connection
 * @param layer Protocol stack layer
 *
 * @return 0 if success, otherwise errorcode
 */
int tls_start_tcp(struct tls_conn **ptc, struct tls *tls, struct tcp_conn *tcp,
		  int layer)
{
	struct tls_conn *tc;
	int err;

	if (!ptc || !tls || !tcp)
		return EINVAL;

	tc = mem_zalloc(sizeof(*tc), destructor);
	if (!tc)
		return ENOMEM;

	err = tcp_register_helper(&tc->th, tcp, layer, estab_handler,
				  send_handler, recv_handler, tc);
	if (err)
		goto out;

	tc->tcp = mem_ref(tcp);

	err = ENOMEM;

	/* Connect the SSL socket */
	tc->ssl = SSL_new(tls->ctx);
	if (!tc->ssl) {
		DEBUG_WARNING("alloc: SSL_new() failed (ctx=%p)\n", tls->ctx);
		goto out;
	}

	tc->sbio_in = BIO_new(BIO_s_mem());
	if (!tc->sbio_in) {
		DEBUG_WARNING("alloc: BIO_new() failed\n");
		goto out;
	}

	tc->sbio_out = BIO_new(&bio_tcp_send);
	if (!tc->sbio_out) {
		DEBUG_WARNING("alloc: BIO_new_socket() failed\n");
		BIO_free(tc->sbio_in);
		goto out;
	}

	tc->sbio_out->ptr = tc;

	SSL_set_bio(tc->ssl, tc->sbio_in, tc->sbio_out);

	err = 0;

 out:
	if (err)
		mem_deref(tc);
	else
		*ptc = tc;

	return err;
}