mirror of
https://github.com/troglobit/finit.git
synced 2026-10-17 10:49:34 +00:00
Refactor RFC 868, time protocol plugin
- Always do a dummy read to get peer address. - Refactor to new format used by other built-ins Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
d4fdf50636
commit
5723b2e75b
1 file changed
+43
-18
+43
-18
@@ -32,33 +32,58 @@
|
||||
#define EPOCH_OFFSET 2208988800ULL
|
||||
|
||||
/* Return number of seconds since midnight, 1st Jan, 1900 */
|
||||
static int cb(int type)
|
||||
static char *rfctime(char *buf, size_t *len)
|
||||
{
|
||||
int sd = STDIN_FILENO;
|
||||
time_t now;
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t sa_len = sizeof(sa);
|
||||
|
||||
if (SOCK_DGRAM == type) {
|
||||
int dummy;
|
||||
|
||||
/* Read empty datagram in UDP mode before sending reply. */
|
||||
if (-1 == recvfrom(sd, &dummy, sizeof(dummy), MSG_DONTWAIT, (struct sockaddr *)&sa, &sa_len))
|
||||
return -1; /* On error, close connection. */
|
||||
}
|
||||
|
||||
now = time(NULL);
|
||||
if ((time_t)-1 == now)
|
||||
return -1; /* On error, close connection. */
|
||||
return NULL;
|
||||
|
||||
/* Account for UNIX epoch offset */
|
||||
/*
|
||||
* Account for UNIX epoch offset, and
|
||||
* convert to network byte order
|
||||
*/
|
||||
now += EPOCH_OFFSET;
|
||||
now = htonl(now);
|
||||
|
||||
/* Convert to network byte order, not explicitly stated in RFC */
|
||||
now = htonl(now);
|
||||
*len = sizeof(now);
|
||||
memcpy(buf, &now, *len);
|
||||
|
||||
/* Send reply, ignore error, simply close connection. */
|
||||
return sendto(sd, &now, sizeof(now), MSG_DONTWAIT, (struct sockaddr *)&sa, sa_len);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int recv_peer(int sd, char *buf, ssize_t len, struct sockaddr *sa, socklen_t *sa_len)
|
||||
{
|
||||
len = recvfrom(sd, buf, sizeof(buf), MSG_DONTWAIT, sa, sa_len);
|
||||
if (-1 == len)
|
||||
return -1; /* On error, close connection. */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int send_peer(int sd, char *buf, ssize_t len, struct sockaddr *sa, socklen_t sa_len)
|
||||
{
|
||||
char *now;
|
||||
|
||||
now = rfctime(buf, &len);
|
||||
if (!now)
|
||||
return -1; /* On error, close connection. */
|
||||
|
||||
return sendto(sd, now, len, MSG_DONTWAIT, sa, sa_len);
|
||||
}
|
||||
|
||||
static int cb(int type)
|
||||
{
|
||||
int sd = STDIN_FILENO;
|
||||
char buf[BUFSIZ];
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t sa_len = sizeof(sa);
|
||||
|
||||
if (recv_peer(sd, buf, sizeof(buf), (struct sockaddr *)&sa, &sa_len))
|
||||
return -1; /* On error, close connection. */
|
||||
|
||||
return send_peer(sd, buf, sizeof(buf), (struct sockaddr *)&sa, sa_len);
|
||||
}
|
||||
|
||||
static plugin_t plugin = {
|
||||
|
||||
Reference in new issue
Block a user