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