}
-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;
_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;
#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;
#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;
}
-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;
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);
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 =
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 =
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 =
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);
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;
+
+}