Revision 0bfe6568 src/net_helper.c

View differences:

src/net_helper.c
17 17

  
18 18
#include "net_helper.h"
19 19

  
20
#define MAX_MSG_SIZE 1024 * 60
21

  
20 22
struct nodeID {
21 23
  struct sockaddr_in addr;
22 24
  int fd;
......
117 119

  
118 120
void reg_message_send(int size, uint8_t type);
119 121

  
122
struct my_hdr_t {
123
  uint8_t m_seq;
124
  uint8_t frag_seq;
125
  uint8_t frags;
126
} __attribute__((packed));
127

  
120 128
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
121 129
{
122 130
  static struct msghdr msg;
123
  static uint8_t my_hdr;
131
  static struct my_hdr_t my_hdr;
124 132
  struct iovec iov[2];
125 133
  int res;
126 134

  
......
128 136
  reg_message_send(buffer_size, buffer_ptr[0]);
129 137

  
130 138
  iov[0].iov_base = &my_hdr;
131
  iov[0].iov_len = 1;
139
  iov[0].iov_len = sizeof(struct my_hdr_t);
132 140
  msg.msg_name = &to->addr;
133 141
  msg.msg_namelen = sizeof(struct sockaddr_in);
134 142
  msg.msg_iovlen = 2;
135 143
  msg.msg_iov = iov;
136
  
144

  
145
  my_hdr.m_seq++;
146
  my_hdr.frags = (buffer_size / (MAX_MSG_SIZE)) + 1;
147
  my_hdr.frag_seq = 0;
148

  
137 149
  do {
138 150
    iov[1].iov_base = buffer_ptr;
139
    if (buffer_size > 1024 * 60) {
140
      iov[1].iov_len = 1024 * 60;
141
      my_hdr = 0;
151
    if (buffer_size > MAX_MSG_SIZE) {
152
      iov[1].iov_len = MAX_MSG_SIZE;
142 153
    } else {
143 154
      iov[1].iov_len = buffer_size;
144
      my_hdr = 1;
145 155
    }
156
    my_hdr.frag_seq++;
157

  
146 158
    buffer_size -= iov[1].iov_len;
147 159
    buffer_ptr += iov[1].iov_len;
148 160
    res = sendmsg(from->fd, &msg, 0);
......
160 172

  
161 173
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
162 174
{
163
  int res, recv;
175
  int res, recv, m_seq, frag_seq;
164 176
  struct sockaddr_in raddr;
165 177
  static struct msghdr msg;
166
  static uint8_t my_hdr;
178
  static struct my_hdr_t my_hdr;
167 179
  struct iovec iov[2];
168 180
  uint8_t *buffer_ptr_orig = buffer_ptr;
169 181

  
170 182
  iov[0].iov_base = &my_hdr;
171
  iov[0].iov_len = 1;
183
  iov[0].iov_len = sizeof(struct my_hdr_t);
172 184
  msg.msg_name = &raddr;
173 185
  msg.msg_namelen = sizeof(struct sockaddr_in);
174 186
  msg.msg_iovlen = 2;
......
180 192
  }
181 193

  
182 194
  recv = 0;
195
  m_seq = -1;
196
  frag_seq = 0;
183 197
  do {
184 198
    iov[1].iov_base = buffer_ptr;
185
    if (buffer_size > 1024 * 60) {
186
      iov[1].iov_len = 1024 * 60;
199
    if (buffer_size > MAX_MSG_SIZE) {
200
      iov[1].iov_len = MAX_MSG_SIZE;
187 201
    } else {
188 202
      iov[1].iov_len = buffer_size;
189 203
    }
190 204
    buffer_size -= iov[1].iov_len;
191 205
    buffer_ptr += iov[1].iov_len;
192 206
    res = recvmsg(local->fd, &msg, 0);
193
    recv += (res - 1);
194
  } while ((my_hdr == 0) && (buffer_size > 0));
207
    recv += (res - sizeof(struct my_hdr_t));
208
    if (m_seq != -1 && my_hdr.m_seq != m_seq) {
209
      return -1;
210
    } else {
211
      m_seq = my_hdr.m_seq;
212
    }
213
    if (my_hdr.frag_seq != frag_seq + 1) {
214
      return -1;
215
    } else {
216
     frag_seq++;
217
    }
218
  } while ((my_hdr.frag_seq < my_hdr.frags) && (buffer_size > 0));
195 219
  memcpy(&(*remote)->addr, &raddr, msg.msg_namelen);
196 220
  (*remote)->fd = -1;
197 221

  

Also available in: Unified diff