]>
git.proxmox.com Git - mirror_frr.git/blob - lib/network.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 1997 Kunihiro Ishiguro
10 #include "lib_errors.h"
12 /* Read nbytes from fd and store into ptr. */
13 int readn(int fd
, uint8_t *ptr
, int nbytes
)
21 nread
= read(fd
, ptr
, nleft
);
32 return nbytes
- nleft
;
35 /* Write nbytes from ptr to fd. */
36 int writen(int fd
, const uint8_t *ptr
, int nbytes
)
44 nwritten
= write(fd
, ptr
, nleft
);
47 if (!ERRNO_IO_RETRY(errno
))
56 return nbytes
- nleft
;
59 int set_nonblocking(int fd
)
63 /* According to the Single UNIX Spec, the return value for F_GETFL
66 flags
= fcntl(fd
, F_GETFL
);
68 flog_err(EC_LIB_SYSTEM_CALL
,
69 "fcntl(F_GETFL) failed for fd %d: %s", fd
,
70 safe_strerror(errno
));
73 if (fcntl(fd
, F_SETFL
, (flags
| O_NONBLOCK
)) < 0) {
74 flog_err(EC_LIB_SYSTEM_CALL
,
75 "fcntl failed setting fd %d non-blocking: %s", fd
,
76 safe_strerror(errno
));
82 int set_cloexec(int fd
)
85 flags
= fcntl(fd
, F_GETFD
, 0);
90 if (fcntl(fd
, F_SETFD
, flags
) == -1)
95 float htonf(float host
)
100 memcpy(&lu1
, &host
, sizeof(uint32_t));
102 memcpy(&convert
, &lu2
, sizeof(uint32_t));
106 float ntohf(float net
)
111 uint64_t frr_sequence_next(void)
113 static uint64_t last_sequence
;
116 (void)clock_gettime(CLOCK_MONOTONIC
, &ts
);
117 if (last_sequence
== (uint64_t)ts
.tv_sec
) {
119 return last_sequence
;
122 last_sequence
= ts
.tv_sec
;
123 return last_sequence
;
126 uint32_t frr_sequence32_next(void)
128 /* coverity[Y2K38_SAFETY] */
129 return (uint32_t)frr_sequence_next();