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