From: Nikos Mavrogiannopoulos Date: Sun, 26 Mar 2000 14:10:41 +0000 (+0000) Subject: Included a reliable version of read/write (that read/write will return X-Git-Tag: gnutls0-0-4~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0ae1b40417373cdf39dfa43b50237e7d3fbd49d;p=thirdparty%2Fgnutls.git Included a reliable version of read/write (that read/write will return exactly the bytes specified). --- diff --git a/src/gnutls.c b/src/gnutls.c index 89f3491d2d..fcc2ef851e 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -233,8 +233,8 @@ int _gnutls_send_alert(int cd, GNUTLS_STATE state, AlertLevel level, } -int gnutls_send_int(int cd, GNUTLS_STATE state, ContentType type, - char *data, int sizeofdata) +ssize_t gnutls_send_int(int cd, GNUTLS_STATE state, ContentType type, + char *data, size_t sizeofdata) { GNUTLSPlaintext *gtxt; GNUTLSCompressed *gcomp; @@ -286,20 +286,20 @@ int gnutls_send_int(int cd, GNUTLS_STATE state, ContentType type, _gnutls_freeTLSCompressed(gcomp); fprintf(stderr, "size: %d\ntype: %d\n", gcipher->length, type); - if (write(cd, &gcipher->type, sizeof(ContentType)) != + if (Write(cd, &gcipher->type, sizeof(ContentType)) != sizeof(ContentType)) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, &gcipher->version.major, 1) != 1) { + if (Write(cd, &gcipher->version.major, 1) != 1) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, &gcipher->version.minor, 1) != 1) { + if (Write(cd, &gcipher->version.minor, 1) != 1) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; @@ -311,14 +311,14 @@ fprintf(stderr, "size: %d\ntype: %d\n", gcipher->length, type); #else length = byteswap16(gcipher->length); #endif - if (write(cd, &length, sizeof(uint16)) != sizeof(uint16)) { + if (Write(cd, &length, sizeof(uint16)) != sizeof(uint16)) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, gcipher->fragment, gcipher->length) != + if (Write(cd, gcipher->fragment, gcipher->length) != gcipher->length) { state->gnutls_internals.valid_connection = VALID_FALSE; @@ -364,32 +364,32 @@ fprintf(stderr, "size: %d\ntype: %d\n", gcipher->length, type); #else length = byteswap16(gcipher->length); #endif - if (write(cd, &gcipher->type, sizeof(ContentType)) != + if (Write(cd, &gcipher->type, sizeof(ContentType)) != sizeof(ContentType)) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, &gcipher->version.major, 1) != 1) { + if (Write(cd, &gcipher->version.major, 1) != 1) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, &gcipher->version.minor, 1) != 1) { + if (Write(cd, &gcipher->version.minor, 1) != 1) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, &length, sizeof(uint16)) != sizeof(uint16)) { + if (Write(cd, &length, sizeof(uint16)) != sizeof(uint16)) { state->gnutls_internals.valid_connection = VALID_FALSE; state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNABLE_SEND_DATA; } - if (write(cd, gcipher->fragment, gcipher->length) != + if (Write(cd, gcipher->fragment, gcipher->length) != gcipher->length) { state->gnutls_internals.valid_connection = VALID_FALSE; @@ -406,8 +406,8 @@ fprintf(stderr, "size: %d\ntype: %d\n", gcipher->length, type); } -int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, - char *data, int sizeofdata) +ssize_t gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, + char *data, size_t sizeofdata) { GNUTLSPlaintext *gtxt; GNUTLSCompressed *gcomp; @@ -423,7 +423,7 @@ int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, while (gnutls_getDataBufferSize(type, state) < sizeofdata) { - if (read(cd, &gcipher.type, sizeof(ContentType)) != + if (Read(cd, &gcipher.type, sizeof(ContentType)) != sizeof(ContentType)) { _gnutls_send_alert(cd, state, GNUTLS_FATAL, GNUTLS_INTERNAL_ERROR); @@ -432,7 +432,7 @@ int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNEXPECTED_PACKET_LENGTH; } - if (read(cd, &gcipher.version.major, 1) != 1) { + if (Read(cd, &gcipher.version.major, 1) != 1) { _gnutls_send_alert(cd, state, GNUTLS_FATAL, GNUTLS_INTERNAL_ERROR); state->gnutls_internals.valid_connection = @@ -440,7 +440,7 @@ int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, state->gnutls_internals.resumable = RESUME_FALSE; return GNUTLS_E_UNEXPECTED_PACKET_LENGTH; } - if (read(cd, &gcipher.version.minor, 1) != 1) { + if (Read(cd, &gcipher.version.minor, 1) != 1) { _gnutls_send_alert(cd, state, GNUTLS_FATAL, GNUTLS_INTERNAL_ERROR); state->gnutls_internals.valid_connection = @@ -456,7 +456,7 @@ int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, return GNUTLS_E_UNSUPPORTED_VERSION_PACKET; } - if (read(cd, &gcipher.length, 2) != 2) { + if (Read(cd, &gcipher.length, 2) != 2) { _gnutls_send_alert(cd, state, GNUTLS_FATAL, GNUTLS_INTERNAL_ERROR); state->gnutls_internals.valid_connection = @@ -479,7 +479,7 @@ int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, gcipher.fragment = gnutls_malloc(gcipher.length); /* read ciphertext */ - if (ret=read(cd, gcipher.fragment, gcipher.length) != + if (ret=Read(cd, gcipher.fragment, gcipher.length) != gcipher.length) { fprintf(stderr, "Received packet with length: %d\nExpecting %d\n", ret, gcipher.length); gnutls_free(gcipher.fragment); diff --git a/src/gnutls.h b/src/gnutls.h index 73cef39009..b4ed204e3a 100644 --- a/src/gnutls.h +++ b/src/gnutls.h @@ -14,8 +14,8 @@ typedef struct GNUTLS_STATE_INT* GNUTLS_STATE; int gnutls_init(GNUTLS_STATE * state, ConnectionEnd con_end); int gnutls_deinit(GNUTLS_STATE * state); -int gnutls_send_int(int cd, GNUTLS_STATE state, ContentType type, char* data, int sizeofdata); -int gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, char* data, int sizeofdata); +ssize_t gnutls_send_int(int cd, GNUTLS_STATE state, ContentType type, char* data, size_t sizeofdata); +ssize_t gnutls_recv_int(int cd, GNUTLS_STATE state, ContentType type, char* data, size_t sizeofdata); int gnutls_handshake(int cd, GNUTLS_STATE state); #define gnutls_send( x, y, z, w) gnutls_send_int( x, y, GNUTLS_APPLICATION_DATA, z, w) diff --git a/src/gnutls_buffers.c b/src/gnutls_buffers.c index 59bc3faa2e..2629362b02 100644 --- a/src/gnutls_buffers.c +++ b/src/gnutls_buffers.c @@ -4,31 +4,82 @@ int gnutls_insertDataBuffer(GNUTLS_STATE state, char *data, int length) { - int old_buffer=state->gnutls_internals.bufferSize; - - state->gnutls_internals.bufferSize+=length; - state->gnutls_internals.buffer = gnutls_realloc( state->gnutls_internals.buffer, state->gnutls_internals.bufferSize); - memmove( &state->gnutls_internals.buffer[old_buffer], data, length); + int old_buffer = state->gnutls_internals.bufferSize; + + state->gnutls_internals.bufferSize += length; + state->gnutls_internals.buffer = + gnutls_realloc(state->gnutls_internals.buffer, + state->gnutls_internals.bufferSize); + memmove(&state->gnutls_internals.buffer[old_buffer], data, length); return 0; - + } -int gnutls_getDataBufferSize(ContentType type, GNUTLS_STATE state) { - if (type==GNUTLS_APPLICATION_DATA) return state->gnutls_internals.bufferSize; +int gnutls_getDataBufferSize(ContentType type, GNUTLS_STATE state) +{ + if (type == GNUTLS_APPLICATION_DATA) + return state->gnutls_internals.bufferSize; return -1; } int gnutls_getDataFromBuffer(GNUTLS_STATE state, char *data, int length) { length = state->gnutls_internals.bufferSize; - - state->gnutls_internals.bufferSize-=length; - memmove( data, state->gnutls_internals.buffer, length); - + + state->gnutls_internals.bufferSize -= length; + memmove(data, state->gnutls_internals.buffer, length); + /* overwrite buffer */ - memmove( state->gnutls_internals.buffer, &state->gnutls_internals.buffer[length], state->gnutls_internals.bufferSize); - state->gnutls_internals.buffer = gnutls_realloc( state->gnutls_internals.buffer, state->gnutls_internals.bufferSize); + memmove(state->gnutls_internals.buffer, + &state->gnutls_internals.buffer[length], + state->gnutls_internals.bufferSize); + state->gnutls_internals.buffer = + gnutls_realloc(state->gnutls_internals.buffer, + state->gnutls_internals.bufferSize); return length; } + +ssize_t Read(int fd, void *iptr, size_t sizeOfPtr) +{ + size_t left; + ssize_t i=0; + char *ptr = iptr; + + left = sizeOfPtr; + while (left > 0) { + i = read(fd, &ptr[i], left); + if (i < 0) { + return -1; + } else { + if (i == 0) + break; /* EOF */ + } + + left -= i; + + } + + return (sizeOfPtr - left); +} + + +ssize_t Write(int fd, const void *iptr, size_t n) +{ + size_t left; + ssize_t i = 0; + const char *ptr = iptr; + + left = n; + while (left > 0) { + i = write(fd, &ptr[i], left); + if (i <= 0) { + return -1; + } + left -= i; + } + + return n; + +} diff --git a/src/gnutls_buffers.h b/src/gnutls_buffers.h index 78d63581f5..24ad57b878 100644 --- a/src/gnutls_buffers.h +++ b/src/gnutls_buffers.h @@ -1,3 +1,5 @@ int gnutls_insertDataBuffer(GNUTLS_STATE state, char *data, int length); int gnutls_getDataBufferSize(ContentType type, GNUTLS_STATE state); int gnutls_getDataFromBuffer(GNUTLS_STATE state, char *data, int length); +ssize_t Read(int fd, void *iptr, size_t n); +ssize_t Write(int fd, const void *iptr, size_t n);