00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "netio.h"
00027 #include "netio_private.h"
00028 #include "berkeleyapi.h"
00029 #include <unistd.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include "util.h"
00033 #include "debug.h"
00034
00035 errorcode readMsg(sock_t sd, comm_type_t type, void* buf, int buf_len) {
00036
00037
00038
00039
00040
00041 int bytes_read, prev_bytes_read=0, ret, large_buf_len;
00042 char *large_buf, *buf_p;
00043 comm_type_t received_type;
00044
00045
00046
00047 CHECK_NOT_NEG(sd,ERROR_NEG_ARG_1);
00048 CHECK_NOT_NEG(buf_len, ERROR_NEG_ARG_4);
00049
00050
00051
00052
00053 if (buf != NULL)
00054 memset(buf,'\0',buf_len);
00055
00056
00057
00058 if ( (large_buf = (char*)malloc(buf_len+COMM_HEADER_LEN))==NULL)
00059 return ERROR_MALLOC_FAILED;
00060 memset(large_buf,0,buf_len+COMM_HEADER_LEN);
00061 large_buf_len = buf_len+COMM_HEADER_LEN;
00062
00063
00064 buf_p = large_buf;
00065
00066
00067
00068
00069 while (1) {
00070 if ((bytes_read=read(sd, buf_p,
00071 large_buf_len-prev_bytes_read))<0) {
00072 safe_free(large_buf);
00073 return ERROR_1;
00074 }
00075 if (bytes_read==0) {
00076 safe_free(large_buf);
00077 return ERROR_TCP_READ;
00078 }
00079 if (bytes_read+prev_bytes_read > COMM_MAX_LEN) {
00080 DEBUG(DBG_ALL,"ALL:WARNING: POSSIBLE BUFFER OVERRUN\n");
00081 safe_free(large_buf);
00082 return ERROR_2;
00083 }
00084 if (bytes_read + prev_bytes_read < COMM_MIN_LEN ) {
00085
00086 prev_bytes_read += bytes_read;
00087 buf_p = buf_p + bytes_read;
00088 continue;
00089 }
00090
00091
00092
00093 if ((ret=checkMsgLen(large_buf,bytes_read+prev_bytes_read))<0){
00094
00095 if (ret!=NOT_OK) {
00096
00097
00098 safe_free(large_buf);
00099 return ERROR_3;
00100 }
00101
00102 prev_bytes_read += bytes_read;
00103 buf_p = buf_p + bytes_read;
00104 continue;
00105 }
00106
00107 break;
00108 }
00109
00110
00111 if (buf != NULL)
00112 memcpy(buf,large_buf+COMM_HEADER_LEN,buf_len);
00113
00114
00115 memcpy(&received_type,large_buf,4);
00116
00117 safe_free(large_buf);
00118
00119 if (ntohl(received_type) != type)
00120 return ERROR_4;
00121
00122 return SUCCESS;
00123 }
00124
00125 errorcode checkMsgLen(char *msg, int msg_len) {
00126
00127 unsigned long len;
00128
00129 CHECK_NOT_NEG(msg,ERROR_NULL_ARG_1);
00130 CHECK_NOT_NEG(msg_len,ERROR_NEG_ARG_2);
00131 if (msg_len < COMM_MIN_LEN)
00132 return ERROR_1;
00133
00134
00135
00136 memcpy(&len, msg+COMM_TYPE_LEN, COMM_LENGTH_LEN);
00137 len = ntohl(len);
00138
00139 if (msg_len > COMM_MAX_LEN)
00140 return ERROR_2;
00141 else if (len < (msg_len - COMM_HEADER_LEN))
00142 return ERROR_3;
00143
00144
00145 if (len > (msg_len - COMM_HEADER_LEN) )
00146 return NOT_OK;
00147
00148
00149 return OK;
00150 }
00151
00152 errorcode sendMsg(sock_t sd, long type, void* payload, long payload_len) {
00153
00154 char *buf;
00155 long tmp;
00156 unsigned long utmp;
00157
00158 CHECK_NOT_NEG(sd,ERROR_NEG_ARG_1);
00159 CHECK_NOT_NEG(payload_len,ERROR_NEG_ARG_4);
00160 if ((payload==NULL)&&(payload_len!=0))
00161 return ERROR_NULL_ARG_3;
00162
00163 buf = (char*)malloc(COMM_HEADER_LEN+payload_len);
00164 if (buf==NULL)
00165 return ERROR_MALLOC_FAILED;
00166
00167
00168
00169
00170 tmp = htonl(type);
00171 memcpy(buf, &tmp, COMM_TYPE_LEN);
00172
00173
00174 utmp = htonl(payload_len);
00175 memcpy(buf+COMM_TYPE_LEN, &utmp, COMM_LENGTH_LEN);
00176
00177
00178 if (payload_len != 0)
00179 memcpy(buf+COMM_TYPE_LEN+COMM_LENGTH_LEN,payload,payload_len);
00180
00181
00182 if( write(sd, buf, COMM_HEADER_LEN+payload_len) < 0 ) {
00183 DEBUG(DBG_NETWORK,"NETWORK:Failed to send data to socket.\n");
00184 safe_free(buf);
00185 return ERROR_TCP_SEND;
00186 }
00187
00188 safe_free(buf);
00189 return SUCCESS;
00190 }
00191