Commit 8861d32f by Carlos Martín Nieto Committed by Carlos Martín Nieto

ssl: use the callback instead of ifs to determine how to get data

Using the callbacks makes it clearer and reduces the amount of #ifdefs
and ifs and we need in the code.
parent ae789622
...@@ -61,45 +61,26 @@ static int ssl_set_error(gitno_ssl *ssl, int error) ...@@ -61,45 +61,26 @@ static int ssl_set_error(gitno_ssl *ssl, int error)
} }
#endif #endif
void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
{
memset(buf, 0x0, sizeof(gitno_buffer));
memset(data, 0x0, len);
buf->data = data;
buf->len = len;
buf->offset = 0;
buf->fd = t->socket;
#ifdef GIT_SSL
if (t->encrypt)
buf->ssl = &t->ssl;
#endif
buf->recv = recv;
buf->cb_data = cb_data;
}
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
{
gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
}
int gitno_recv(gitno_buffer *buf) int gitno_recv(gitno_buffer *buf)
{ {
return buf->recv(buf); return buf->recv(buf);
} }
#ifdef GIT_SSL #ifdef GIT_SSL
static int ssl_recv(gitno_ssl *ssl, void *data, size_t len) static int gitno__recv_ssl(gitno_buffer *buf)
{ {
int ret; int ret;
do { do {
ret = SSL_read(ssl->ssl, data, len); ret = SSL_read(buf->ssl->ssl, buf->data + buf->offset, buf->len - buf->offset);
} while (SSL_get_error(ssl->ssl, ret) == SSL_ERROR_WANT_READ); } while (SSL_get_error(buf->ssl->ssl, ret) == SSL_ERROR_WANT_READ);
if (ret < 0) if (ret < 0) {
return ssl_set_error(ssl, ret); net_set_error("Error receiving socket data");
return -1;
}
buf->offset += ret;
return ret; return ret;
} }
#endif #endif
...@@ -108,29 +89,39 @@ int gitno__recv(gitno_buffer *buf) ...@@ -108,29 +89,39 @@ int gitno__recv(gitno_buffer *buf)
{ {
int ret; int ret;
#ifdef GIT_SSL
if (buf->ssl != NULL) {
if ((ret = ssl_recv(buf->ssl, buf->data + buf->offset, buf->len - buf->offset)) < 0)
return -1;
} else {
ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
if (ret < 0) {
net_set_error("Error receiving socket data");
return -1;
}
}
#else
ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0); ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
if (ret < 0) { if (ret < 0) {
net_set_error("Error receiving socket data"); net_set_error("Error receiving socket data");
return -1; return -1;
} }
#endif
buf->offset += ret; buf->offset += ret;
return ret; return ret;
} }
void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
{
memset(buf, 0x0, sizeof(gitno_buffer));
memset(data, 0x0, len);
buf->data = data;
buf->len = len;
buf->offset = 0;
buf->fd = t->socket;
buf->recv = recv;
buf->cb_data = cb_data;
}
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
{
#ifdef GIT_SSL
if (t->encrypt) {
gitno_buffer_setup_callback(t, buf, data, len, gitno__recv_ssl, NULL);
buf->ssl = &t->ssl;
} else
#endif
gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
}
/* Consume up to ptr and move the rest of the buffer to the beginning */ /* Consume up to ptr and move the rest of the buffer to the beginning */
void gitno_consume(gitno_buffer *buf, const char *ptr) void gitno_consume(gitno_buffer *buf, const char *ptr)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment