]> git.proxmox.com Git - mirror_frr.git/blob - lib/network.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / network.c
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 int readn(int fd, uint8_t *ptr, int nbytes)
29 {
30 int nleft;
31 int nread;
32
33 nleft = nbytes;
34
35 while (nleft > 0) {
36 nread = read(fd, ptr, nleft);
37
38 if (nread < 0)
39 return (nread);
40 else if (nread == 0)
41 break;
42
43 nleft -= nread;
44 ptr += nread;
45 }
46
47 return nbytes - nleft;
48 }
49
50 /* Write nbytes from ptr to fd. */
51 int writen(int fd, const uint8_t *ptr, int nbytes)
52 {
53 int nleft;
54 int nwritten;
55
56 nleft = nbytes;
57
58 while (nleft > 0) {
59 nwritten = write(fd, ptr, nleft);
60
61 if (nwritten < 0) {
62 if (!ERRNO_IO_RETRY(errno))
63 return nwritten;
64 }
65 if (nwritten == 0)
66 return (nwritten);
67
68 nleft -= nwritten;
69 ptr += nwritten;
70 }
71 return nbytes - nleft;
72 }
73
74 int set_nonblocking(int fd)
75 {
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) {
82 flog_err(EC_LIB_SYSTEM_CALL,
83 "fcntl(F_GETFL) failed for fd %d: %s", fd,
84 safe_strerror(errno));
85 return -1;
86 }
87 if (fcntl(fd, F_SETFL, (flags | O_NONBLOCK)) < 0) {
88 flog_err(EC_LIB_SYSTEM_CALL,
89 "fcntl failed setting fd %d non-blocking: %s", fd,
90 safe_strerror(errno));
91 return -1;
92 }
93 return 0;
94 }
95
96 int set_cloexec(int fd)
97 {
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;
107 }
108
109 float htonf(float host)
110 {
111 uint32_t lu1, lu2;
112 float convert;
113
114 memcpy(&lu1, &host, sizeof(uint32_t));
115 lu2 = htonl(lu1);
116 memcpy(&convert, &lu2, sizeof(uint32_t));
117 return convert;
118 }
119
120 float ntohf(float net)
121 {
122 return htonf(net);
123 }