back to topotato report
topotato coverage report
Current view: top level - lib - network.c (source / functions) Hit Total Coverage
Test: test_bgp_set_aspath_replace.py::BGPSetAspathReplace Lines: 11 60 18.3 %
Date: 2023-02-24 18:37:49 Functions: 2 8 25.0 %

          Line data    Source code
       1             : /*
       2             :  * Network library.
       3             :  * Copyright (C) 1997 Kunihiro Ishiguro
       4             :  *
       5             :  * This file is part of GNU Zebra.
       6             :  *
       7             :  * GNU Zebra is free software; you can redistribute it and/or modify it
       8             :  * under the terms of the GNU General Public License as published by the
       9             :  * Free Software Foundation; either version 2, or (at your option) any
      10             :  * later version.
      11             :  *
      12             :  * GNU Zebra is distributed in the hope that it will be useful, but
      13             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with this program; see the file COPYING; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include <zebra.h>
      23             : #include "log.h"
      24             : #include "network.h"
      25             : #include "lib_errors.h"
      26             : 
      27             : /* Read nbytes from fd and store into ptr. */
      28           0 : int readn(int fd, uint8_t *ptr, int nbytes)
      29             : {
      30           0 :         int nleft;
      31           0 :         int nread;
      32             : 
      33           0 :         nleft = nbytes;
      34             : 
      35           0 :         while (nleft > 0) {
      36           0 :                 nread = read(fd, ptr, nleft);
      37             : 
      38           0 :                 if (nread < 0)
      39           0 :                         return (nread);
      40           0 :                 else if (nread == 0)
      41             :                         break;
      42             : 
      43           0 :                 nleft -= nread;
      44           0 :                 ptr += nread;
      45             :         }
      46             : 
      47           0 :         return nbytes - nleft;
      48             : }
      49             : 
      50             : /* Write nbytes from ptr to fd. */
      51           0 : int writen(int fd, const uint8_t *ptr, int nbytes)
      52             : {
      53           0 :         int nleft;
      54           0 :         int nwritten;
      55             : 
      56           0 :         nleft = nbytes;
      57             : 
      58           0 :         while (nleft > 0) {
      59           0 :                 nwritten = write(fd, ptr, nleft);
      60             : 
      61           0 :                 if (nwritten < 0) {
      62           0 :                         if (!ERRNO_IO_RETRY(errno))
      63           0 :                                 return nwritten;
      64             :                 }
      65           0 :                 if (nwritten == 0)
      66             :                         return (nwritten);
      67             : 
      68           0 :                 nleft -= nwritten;
      69           0 :                 ptr += nwritten;
      70             :         }
      71           0 :         return nbytes - nleft;
      72             : }
      73             : 
      74         132 : int set_nonblocking(int fd)
      75             : {
      76         132 :         int flags;
      77             : 
      78             :         /* According to the Single UNIX Spec, the return value for F_GETFL
      79             :            should
      80             :            never be negative. */
      81         132 :         flags = fcntl(fd, F_GETFL);
      82         132 :         if (flags < 0) {
      83           0 :                 flog_err(EC_LIB_SYSTEM_CALL,
      84             :                          "fcntl(F_GETFL) failed for fd %d: %s", fd,
      85             :                          safe_strerror(errno));
      86           0 :                 return -1;
      87             :         }
      88         132 :         if (fcntl(fd, F_SETFL, (flags | O_NONBLOCK)) < 0) {
      89           0 :                 flog_err(EC_LIB_SYSTEM_CALL,
      90             :                          "fcntl failed setting fd %d non-blocking: %s", fd,
      91             :                          safe_strerror(errno));
      92           0 :                 return -1;
      93             :         }
      94             :         return 0;
      95             : }
      96             : 
      97         102 : int set_cloexec(int fd)
      98             : {
      99         102 :         int flags;
     100         102 :         flags = fcntl(fd, F_GETFD, 0);
     101         102 :         if (flags == -1)
     102             :                 return -1;
     103             : 
     104         102 :         flags |= FD_CLOEXEC;
     105         102 :         if (fcntl(fd, F_SETFD, flags) == -1)
     106             :                 return -1;
     107             :         return 0;
     108             : }
     109             : 
     110           0 : float htonf(float host)
     111             : {
     112           0 :         uint32_t lu1, lu2;
     113           0 :         float convert;
     114             : 
     115           0 :         memcpy(&lu1, &host, sizeof(uint32_t));
     116           0 :         lu2 = htonl(lu1);
     117           0 :         memcpy(&convert, &lu2, sizeof(uint32_t));
     118           0 :         return convert;
     119             : }
     120             : 
     121           0 : float ntohf(float net)
     122             : {
     123           0 :         return htonf(net);
     124             : }
     125             : 
     126           0 : uint64_t frr_sequence_next(void)
     127             : {
     128           0 :         static uint64_t last_sequence;
     129           0 :         struct timespec ts;
     130             : 
     131           0 :         (void)clock_gettime(CLOCK_MONOTONIC, &ts);
     132           0 :         if (last_sequence == (uint64_t)ts.tv_sec) {
     133           0 :                 last_sequence++;
     134           0 :                 return last_sequence;
     135             :         }
     136             : 
     137           0 :         last_sequence = ts.tv_sec;
     138           0 :         return last_sequence;
     139             : }
     140             : 
     141           0 : uint32_t frr_sequence32_next(void)
     142             : {
     143             :         /* coverity[Y2K38_SAFETY] */
     144           0 :         return (uint32_t)frr_sequence_next();
     145             : }

Generated by: LCOV version v1.16-topotato