513 |
* @param socket the socket to write to |
* @param socket the socket to write to |
514 |
* @param buffer The buffer to write |
* @param buffer The buffer to write |
515 |
* @param size Number of bytes to send |
* @param size Number of bytes to send |
516 |
|
* @param timeout Seconds to wait for data to be written |
517 |
* @return The number of bytes sent or -1 if an error occured. |
* @return The number of bytes sent or -1 if an error occured. |
518 |
*/ |
*/ |
519 |
int sock_write(int socket, const void *buffer, int size) { |
int sock_write(int socket, const void *buffer, int size, int timeout) { |
520 |
|
|
521 |
ssize_t n= 0; |
ssize_t n= 0; |
522 |
|
|
529 |
} while(n == -1 && errno == EINTR); |
} while(n == -1 && errno == EINTR); |
530 |
|
|
531 |
if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
if(n == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
532 |
if(! can_write(socket, NET_TIMEOUT)) { |
if(! can_write(socket, timeout)) { |
533 |
return -1; |
return -1; |
534 |
} |
} |
535 |
do { |
do { |
551 |
* @param size Number of bytes to read from the socket |
* @param size Number of bytes to read from the socket |
552 |
* @param timeout Seconds to wait for data to be available |
* @param timeout Seconds to wait for data to be available |
553 |
* @return The number of bytes read or -1 if an error occured. |
* @return The number of bytes read or -1 if an error occured. |
554 |
*/ |
*/ |
555 |
int sock_read(int socket, void *buffer, int size, int timeout) { |
int sock_read(int socket, void *buffer, int size, int timeout) { |
556 |
|
|
557 |
ssize_t n; |
ssize_t n; |
577 |
|
|
578 |
} |
} |
579 |
|
|
580 |
|
|
581 |
|
/** |
582 |
|
* Write <code>size</code> bytes from the <code>buffer</code> to the |
583 |
|
* <code>socket</code>. The given socket <b>must</b> be a connected |
584 |
|
* UDP socket |
585 |
|
* @param socket the socket to write to |
586 |
|
* @param buffer The buffer to write |
587 |
|
* @param size Number of bytes to send |
588 |
|
* @param timeout Seconds to wait for data to be written |
589 |
|
* @return The number of bytes sent or -1 if an error occured. |
590 |
|
*/ |
591 |
|
int udp_write(int socket, void *b, int len, int timeout) { |
592 |
|
|
593 |
|
int i= 0, n; |
594 |
|
|
595 |
|
ASSERT(timeout>=0); |
596 |
|
|
597 |
|
/* write (not sendto) is required for a connected udp socket */ |
598 |
|
for(i= 4; i>=1; i--) { |
599 |
|
|
600 |
|
n= sock_write(socket, b, len, 0); |
601 |
|
|
602 |
|
/* Simple retransmit logic, wait for the server to reply |
603 |
|
back to our socket. This assume a request-response pattern, |
604 |
|
which really is the only pattern we can support */ |
605 |
|
if(can_read(socket, timeout/i)) break; |
606 |
|
DEBUG("udp_write: Resending request\n"); |
607 |
|
|
608 |
|
} |
609 |
|
|
610 |
|
return n; |
611 |
|
|
612 |
|
} |
613 |
|
|
614 |
|
|
615 |
/** |
/** |
616 |
* Create a ICMP socket against hostname, send echo and wait for response. |
* Create a ICMP socket against hostname, send echo and wait for response. |