#include "sys_defs.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <msg.h>
#include <iostuff.h>
#include <biff_notify.h>
void biff_notify(const char *text, ssize_t len)
{
static struct sockaddr_in sin;
static int sock = -1;
struct hostent *hp;
struct servent *sp;
if (sin.sin_family == 0) {
if ((sp = getservbyname("biff", "udp")) == 0) {
msg_warn("service not found: biff/udp");
return;
}
if ((hp = gethostbyname("localhost")) == 0) {
msg_warn("host not found: localhost");
return;
}
if ((int) hp->h_length > (int) sizeof(sin.sin_addr)) {
msg_warn("bad address size %d for localhost", hp->h_length);
return;
}
sin.sin_family = hp->h_addrtype;
sin.sin_port = sp->s_port;
memcpy((void *) &sin.sin_addr, hp->h_addr_list[0], hp->h_length);
}
if (sock < 0) {
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
msg_warn("socket: %m");
return;
}
close_on_exec(sock, CLOSE_ON_EXEC);
}
if (sendto(sock, text, len, 0, (struct sockaddr *) &sin, sizeof(sin)) != len)
msg_warn("biff_notify: %m");
}