]> git.proxmox.com Git - mirror_frr.git/blame - lib/network.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / network.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 20 */
21
22#include <zebra.h>
a269d613 23#include "log.h"
24#include "network.h"
fc41c153 25#include "lib_errors.h"
718e3744 26
27/* Read nbytes from fd and store into ptr. */
d7c0a89a 28int readn(int fd, uint8_t *ptr, int nbytes)
718e3744 29{
d62a17ae 30 int nleft;
31 int nread;
718e3744 32
d62a17ae 33 nleft = nbytes;
718e3744 34
d62a17ae 35 while (nleft > 0) {
36 nread = read(fd, ptr, nleft);
718e3744 37
d62a17ae 38 if (nread < 0)
39 return (nread);
40 else if (nread == 0)
41 break;
718e3744 42
d62a17ae 43 nleft -= nread;
44 ptr += nread;
45 }
718e3744 46
d62a17ae 47 return nbytes - nleft;
48}
718e3744 49
50/* Write nbytes from ptr to fd. */
d7c0a89a 51int writen(int fd, const uint8_t *ptr, int nbytes)
718e3744 52{
d62a17ae 53 int nleft;
54 int nwritten;
718e3744 55
d62a17ae 56 nleft = nbytes;
718e3744 57
d62a17ae 58 while (nleft > 0) {
59 nwritten = write(fd, ptr, nleft);
5f8fec87 60
d62a17ae 61 if (nwritten < 0) {
62 if (!ERRNO_IO_RETRY(errno))
63 return nwritten;
64 }
65 if (nwritten == 0)
66 return (nwritten);
718e3744 67
d62a17ae 68 nleft -= nwritten;
69 ptr += nwritten;
70 }
71 return nbytes - nleft;
718e3744 72}
a269d613 73
d62a17ae 74int set_nonblocking(int fd)
a269d613 75{
d62a17ae 76 int flags;
77
78 /* According to the Single UNIX Spec, the return value for F_GETFL
79 should
80 never be negative. */
81 if ((flags = fcntl(fd, F_GETFL)) < 0) {
450971aa 82 flog_err(EC_LIB_SYSTEM_CALL,
fc41c153
DS
83 "fcntl(F_GETFL) failed for fd %d: %s", fd,
84 safe_strerror(errno));
d62a17ae 85 return -1;
86 }
87 if (fcntl(fd, F_SETFL, (flags | O_NONBLOCK)) < 0) {
450971aa 88 flog_err(EC_LIB_SYSTEM_CALL,
fc41c153
DS
89 "fcntl failed setting fd %d non-blocking: %s", fd,
90 safe_strerror(errno));
d62a17ae 91 return -1;
92 }
93 return 0;
a269d613 94}
16f1b9ee 95
d62a17ae 96int set_cloexec(int fd)
2da59394 97{
d62a17ae 98 int flags;
99 flags = fcntl(fd, F_GETFD, 0);
100 if (flags == -1)
101 return -1;
102
103 flags |= FD_CLOEXEC;
104 if (fcntl(fd, F_SETFD, flags) == -1)
105 return -1;
106 return 0;
2da59394
DL
107}
108
d62a17ae 109float htonf(float host)
16f1b9ee 110{
d7c0a89a 111 uint32_t lu1, lu2;
d62a17ae 112 float convert;
16f1b9ee 113
d7c0a89a 114 memcpy(&lu1, &host, sizeof(uint32_t));
d62a17ae 115 lu2 = htonl(lu1);
d7c0a89a 116 memcpy(&convert, &lu2, sizeof(uint32_t));
d62a17ae 117 return convert;
16f1b9ee
OD
118}
119
d62a17ae 120float ntohf(float net)
16f1b9ee 121{
d62a17ae 122 return htonf(net);
16f1b9ee 123}