+
+/* Data exchange over raw sockets. Placing this in there is a kind of crude hack.
+ It means that the only possible raw are TCP where we may want to do UDP for them.
+ But I fail to find a good internal organization for now. We may want to split
+ raw and regular sockets more efficiently.
+*/
+gras_error_t gras_socket_raw_exchange(gras_socket_t peer,
+ int sender,
+ unsigned int timeout,
+ unsigned long int exp_size,
+ unsigned long int msg_size) {
+ char *chunk;
+ int res_last, msg_sofar, exp_sofar;
+
+ fd_set rd_set;
+ int rv;
+
+ struct timeval timeOut;
+
+ chunk = gras_malloc(msg_size);
+
+ for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
+ for(msg_sofar=0; msg_sofar < msg_size; msg_sofar += res_last) {
+
+ if(sender) {
+ res_last = send(peer->sd, chunk, msg_size - msg_sofar, 0);
+ } else {
+ res_last = 0;
+ FD_ZERO(&rd_set);
+ FD_SET(peer->sd,&rd_set);
+ timeOut.tv_sec = timeout;
+ timeOut.tv_usec = 0;
+
+ if (0 < select(peer->sd+1,&rd_set,NULL,NULL,&timeOut))
+ res_last = recv(peer->sd, chunk, msg_size-msg_sofar, 0);
+
+ }
+ if (res_last == 0) {
+ /* No progress done, bail out */
+ gras_free(chunk);
+ RAISE0(unknown_error,"Not exchanged a single byte, bailing out");
+ }
+ }
+ }
+
+ gras_free(chunk);
+ return no_error;
+}