]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | /* Read nbytes from fd and store into ptr. */ | |
27 | int readn(int fd, u_char *ptr, int nbytes) | |
28 | { | |
29 | int nleft; | |
30 | int nread; | |
31 | ||
32 | nleft = nbytes; | |
33 | ||
34 | while (nleft > 0) { | |
35 | nread = read(fd, ptr, nleft); | |
36 | ||
37 | if (nread < 0) | |
38 | return (nread); | |
39 | else if (nread == 0) | |
40 | break; | |
41 | ||
42 | nleft -= nread; | |
43 | ptr += nread; | |
44 | } | |
45 | ||
46 | return nbytes - nleft; | |
47 | } | |
48 | ||
49 | /* Write nbytes from ptr to fd. */ | |
50 | int writen(int fd, const u_char *ptr, int nbytes) | |
51 | { | |
52 | int nleft; | |
53 | int nwritten; | |
54 | ||
55 | nleft = nbytes; | |
56 | ||
57 | while (nleft > 0) { | |
58 | nwritten = write(fd, ptr, nleft); | |
59 | ||
60 | if (nwritten < 0) { | |
61 | if (!ERRNO_IO_RETRY(errno)) | |
62 | return nwritten; | |
63 | } | |
64 | if (nwritten == 0) | |
65 | return (nwritten); | |
66 | ||
67 | nleft -= nwritten; | |
68 | ptr += nwritten; | |
69 | } | |
70 | return nbytes - nleft; | |
71 | } | |
72 | ||
73 | int set_nonblocking(int fd) | |
74 | { | |
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; | |
91 | } | |
92 | ||
93 | int set_cloexec(int fd) | |
94 | { | |
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; | |
104 | } | |
105 | ||
106 | float htonf(float host) | |
107 | { | |
108 | u_int32_t lu1, lu2; | |
109 | float convert; | |
110 | ||
111 | memcpy(&lu1, &host, sizeof(u_int32_t)); | |
112 | lu2 = htonl(lu1); | |
113 | memcpy(&convert, &lu2, sizeof(u_int32_t)); | |
114 | return convert; | |
115 | } | |
116 | ||
117 | float ntohf(float net) | |
118 | { | |
119 | return htonf(net); | |
120 | } |