@@ -365,16 +365,27 @@ netannounce(int domain, int proto, const char *local, const char *bind_dev, int
365365 return s ;
366366}
367367
368-
369368/*******************************************************************/
370- /* reads 'count' bytes from a socket */
369+ /* Nread - reads 'count' bytes from a socket */
371370/********************************************************************/
372371
373372int
374373Nread (int fd , char * buf , size_t count , int prot )
374+ {
375+ return Nrecv (fd , buf , count , prot , 0 );
376+ }
377+
378+ /*******************************************************************/
379+ /* Nrecv - reads 'count' bytes from a socket */
380+ /********************************************************************/
381+
382+ int
383+ Nrecv (int fd , char * buf , size_t count , int prot , int sock_opt )
375384{
376385 register ssize_t r ;
377- register size_t nleft = count ;
386+ // `nleft` must be signed as it may get negative value for SKIP-RX-COPY UDP (MSG_TRUNC in sock_opt).
387+ register ssize_t nleft = count ;
388+ register size_t total = 0 ;
378389 struct iperf_time ftimeout = { 0 , 0 };
379390
380391 fd_set rfdset ;
@@ -403,7 +414,11 @@ Nread(int fd, char *buf, size_t count, int prot)
403414 }
404415
405416 while (nleft > 0 ) {
406- r = read (fd , buf , nleft );
417+ if (sock_opt )
418+ r = recv (fd , buf , nleft , sock_opt );
419+ else
420+ r = read (fd , buf , nleft );
421+
407422 if (r < 0 ) {
408423 /* XXX EWOULDBLOCK can't happen without non-blocking sockets */
409424 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK )
@@ -413,7 +428,8 @@ Nread(int fd, char *buf, size_t count, int prot)
413428 } else if (r == 0 )
414429 break ;
415430
416- nleft -= r ;
431+ total += r ;
432+ nleft -= r ;
417433 buf += r ;
418434
419435 /*
@@ -449,20 +465,32 @@ Nread(int fd, char *buf, size_t count, int prot)
449465 }
450466 }
451467 }
452- return count - nleft ;
468+ return total ;
453469}
454470
455471/********************************************************************/
456- /* reads 'count' bytes from a socket - but without using select() */
472+ /* Nreads 'count' bytes from a socket - but without using select() */
457473/********************************************************************/
458474int
459475Nread_no_select (int fd , char * buf , size_t count , int prot )
476+ {
477+ return Nrecv_no_select (fd , buf , count , prot , 0 );
478+ }
479+
480+ /********************************************************************/
481+ /* Nrecv reads 'count' bytes from a socket - but without using select() */
482+ /********************************************************************/
483+ int
484+ Nrecv_no_select (int fd , char * buf , size_t count , int prot , int sock_opt )
460485{
461486 register ssize_t r ;
462487 register size_t nleft = count ;
463488
464489 while (nleft > 0 ) {
465- r = read (fd , buf , nleft );
490+ if (sock_opt )
491+ r = recv (fd , buf , nleft , sock_opt );
492+ else
493+ r = read (fd , buf , nleft );
466494 if (r < 0 ) {
467495 /* XXX EWOULDBLOCK can't happen without non-blocking sockets */
468496 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK )
0 commit comments