#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <dirent.h>
#include <errno.h>
-#include "net.h"
+#include "nwho.h"
struct hostrec {
struct hostrec *next;
static int sock, port;
static struct hostrec *first_host;
static time_t now, last_local_scan;
-
-static void die(char *msg, ...) __attribute__((noreturn));
-
-static void
-die(char *msg, ...)
-{
- va_list args;
-
- va_start(args, msg);
- fprintf(stderr, "nwhod: ");
- vfprintf(stderr, msg, args);
- fputc('\n', stderr);
- exit(1);
-}
+static char hostname[64];
static void
net_init(char *name)
if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
die("Cannot connect socket: %m");
}
+
+ if (gethostname(hostname, sizeof(hostname)) < 0)
+ die("Unable to get my own host name: %m");
}
static void
static void
save_pkt(char *name, struct nwho_pkt *pkt)
{
- int len = sizeof(pkt) - (MAX_USERS - ntohl(pkt->num_users))*sizeof(struct userinfo);
+ int len = nwho_pkt_size(pkt);
int fd = open(name, O_WRONLY | O_CREAT, 0666);
if (fd < 0)
{
if (r < n ||
pkt.magic != htonl(NWHO_MAGIC) ||
ntohl(pkt.num_users) > MAX_USERS ||
- r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
+ r < nwho_pkt_size(&pkt))
{
syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
return;
local_scan(void)
{
struct nwho_pkt pkt;
- static char hostname[64];
make_pkt(&pkt);
- if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
- die("gethostname: %m");
save_pkt(hostname, &pkt);
}
{
now = time(NULL);
make_pkt(&pkt);
- if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
+ if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0)
syslog(LOG_ERR, "sendmsg: %m");
sleep(DEFAULT_SEND_TIME);
}
int
main(int argc, char **argv)
{
+ if (argc == 2 && !strcmp(argv[1], "--version"))
+ {
+ printf("nwho " STRINGIFY(VERSION) "\n");
+ return 0;
+ }
+
if (argc == 2)
client(argv[1]);
else if (argc == 1)