-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.c
79 lines (65 loc) · 1.82 KB
/
notifier.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "notifier.h"
#include "logger.h"
#define SERVER_PORT_NUMBER 55000
struct {
char* host;
} notifier_ctl = {
NULL
};
char** notifier_host()
{
return ¬ifier_ctl.host;
}
int send_notification(const char* message, ...)
{
if (notifier_ctl.host == NULL)
return 0;
// Retrieve the address of this host
struct hostent *hp = gethostbyname(notifier_ctl.host);
if (hp == NULL) {
notify(ERROR, "could not retrieve host information about %s [%s]",
notifier_ctl.host, strerror(errno));
return -1;
}
// Create a socket
int sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) {
notify(ERROR, "could not create socket [%s]", strerror(errno));
return -1;
}
// Fill the server address and connect to the target host
const int portNumber = SERVER_PORT_NUMBER;
struct sockaddr_in pin;
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(portNumber);
if (connect(sd, (struct sockaddr *)&pin, sizeof(pin)) < 0) {
notify(WARNING, "could not connect to peer [%s]", strerror(errno));
return -1;
}
// Format the message.
char buffer[128];
va_list args;
va_start(args, message);
vsprintf(buffer, message, args);
va_end(args);
// Send the message
if (send(sd, buffer, strlen(buffer), 0) == -1) {
notify(ERROR, "could not send message [%s]", strerror(errno));
return -1;
}
// Close the socket
shutdown(sd, SHUT_WR);
close(sd);
return 0;
}