aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-02-13 20:00:53 -0600
committerRob Landley <rob@landley.net>2019-02-13 20:00:53 -0600
commit7e6a7da60042dbf033cc3136877a2862bf825bd1 (patch)
tree24f98032c51c97237c51d3af6e36580758a15a50
parentfbedc9dd74aeb0331202f9e503316960e32fa879 (diff)
downloadtoybox-7e6a7da60042dbf033cc3136877a2862bf825bd1.tar.gz
Move nanomove(), nanodiff(), union socksaddr, and xrecvwait() to lib.
-rw-r--r--lib/lib.c26
-rw-r--r--lib/lib.h10
-rw-r--r--lib/net.c19
-rw-r--r--toys/net/sntp.c43
4 files changed, 54 insertions, 44 deletions
diff --git a/lib/lib.c b/lib/lib.c
index b074a92b..9d5c5f4b 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -248,7 +248,7 @@ struct string_list *find_in_path(char *path, char *filename)
cwd = xgetcwd();
for (;;) {
- char *next = strchr(path, ':');
+ char *res, *next = strchr(path, ':');
int len = next ? next-path : strlen(path);
struct string_list *rnext;
struct stat st;
@@ -257,9 +257,7 @@ struct string_list *find_in_path(char *path, char *filename)
+ (len ? len : strlen(cwd)) + 2);
if (!len) sprintf(rnext->str, "%s/%s", cwd, filename);
else {
- char *res = rnext->str;
-
- memcpy(res, path, len);
+ memcpy(res = rnext->str, path, len);
res += len;
*(res++) = '/';
strcpy(res, filename);
@@ -553,6 +551,26 @@ void msleep(long miliseconds)
nanosleep(&ts, &ts);
}
+// Adjust timespec by nanosecond offset
+void nanomove(struct timespec *ts, long long offset)
+{
+ long long nano = ts->tv_nsec + offset, secs = nano/1000000000;
+
+ ts->tv_sec += secs;
+ nano %= 1000000000;
+ if (nano<0) {
+ ts->tv_sec--;
+ nano += 1000000000;
+ }
+ ts->tv_nsec = nano;
+}
+
+// return difference between two timespecs in nanosecs
+long long nanodiff(struct timespec *old, struct timespec *new)
+{
+ return (new->tv_sec - old->tv_sec)*1000000000LL+(new->tv_nsec - old->tv_nsec);
+}
+
// return 1<<x of highest bit set
int highest_bit(unsigned long l)
{
diff --git a/lib/lib.h b/lib/lib.h
index ed31ffa9..b36d7a75 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -205,6 +205,8 @@ struct string_list **splitpath(char *path, struct string_list **list);
char *readfileat(int dirfd, char *name, char *buf, off_t *len);
char *readfile(char *name, char *buf, off_t len);
void msleep(long miliseconds);
+void nanomove(struct timespec *ts, long long offset);
+long long nanodiff(struct timespec *old, struct timespec *new);
int highest_bit(unsigned long l);
int64_t peek_le(void *ptr, unsigned size);
int64_t peek_be(void *ptr, unsigned size);
@@ -301,6 +303,13 @@ void tty_sigreset(int i);
void start_redraw(unsigned *width, unsigned *height);
// net.c
+
+union socksaddr {
+ struct sockaddr s;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+};
+
int xsocket(int domain, int type, int protocol);
void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
@@ -311,6 +320,7 @@ int xpoll(struct pollfd *fds, int nfds, int timeout);
int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
char *ntop(struct sockaddr *sa);
void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest);
+int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout);
// password.c
int get_salt(char *salt, char * algo);
diff --git a/lib/net.c b/lib/net.c
index 346d17e9..d4373fbe 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -138,3 +138,22 @@ void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
if (rc != len) perror_exit("sendto");
}
+
+// xrecvfrom with timeout in milliseconds
+int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
+{
+ socklen_t sl = sizeof(*sa);
+
+ if (timeout >= 0) {
+ struct pollfd pfd;
+
+ pfd.fd = fd;
+ pfd.events = POLLIN;
+ if (!xpoll(&pfd, 1, timeout)) return 0;
+ }
+
+ len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
+ if (len<0) perror_exit("recvfrom");
+
+ return len;
+}
diff --git a/toys/net/sntp.c b/toys/net/sntp.c
index edccd209..96139e8f 100644
--- a/toys/net/sntp.c
+++ b/toys/net/sntp.c
@@ -6,7 +6,7 @@
modes: oneshot display, oneshot set, persist, serve, multi
-USE_SNTP(NEWTOY(sntp, "m:Sp:asdDqr#<4>17=10[!as]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_SNTP(NEWTOY(sntp, "M:m:Sp:asdDqr#<4>17=10[!as]", TOYFLAG_USR|TOYFLAG_BIN))
config SNTP
bool "sntp"
@@ -21,6 +21,7 @@ config SNTP
-a Adjust system clock gradually
-S Serve time instead of querying (bind to SERVER address if specified)
-m Wait for updates from multicast ADDRESS (RFC 4330 says use 224.0.1.1)
+ -M Multicast server on ADDRESS
-d Daemonize (run in background re-querying )
-D Daemonize but stay in foreground: re-query time every 1000 seconds
-r Retry shift (every 1<<SHIFT seconds)
@@ -32,50 +33,12 @@ config SNTP
GLOBALS(
long r;
- char *p, *m;
+ char *p, *m, *M;
)
// Seconds from 1900 to 1970, including appropriate leap days
#define SEVENTIES 2208988800L
-union socksaddr {
- struct sockaddr_in in;
- struct sockaddr_in6 in6;
-};
-
-// timeout in milliseconds
-int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
-{
- socklen_t sl = sizeof(*sa);
-
- if (timeout >= 0) {
- struct pollfd pfd;
-
- pfd.fd = fd;
- pfd.events = POLLIN;
- if (!xpoll(&pfd, 1, timeout)) return 0;
- }
-
- len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
- if (len<0) perror_exit("recvfrom");
-
- return len;
-}
-
-// Adjust timespec by nanosecond offset
-static void nanomove(struct timespec *ts, long long offset)
-{
- long long nano = ts->tv_nsec + offset, secs = nano/1000000000;
-
- ts->tv_sec += secs;
- nano %= 1000000000;
- if (nano<0) {
- ts->tv_sec--;
- nano += 1000000000;
- }
- ts->tv_nsec = nano;
-}
-
// Get time and return ntptime (saving timespec in pointer if not null)
// NTP time is high 32 bits = seconds since 1970 (blame RFC 868), low 32 bits
// fraction of a second.