-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsayhello.c
97 lines (88 loc) · 2.73 KB
/
sayhello.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* cc -o sayhello.c '-DHELLO_ADDRESS="foo.example.com"' -DHELLO_PORT=4567
sends a UDP packet containing this machine's hostname (and nothing else)
to HELLO_ADDRESS:HELLO_PORT every five seconds.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#ifndef HELLO_ADDRESS
#define HELLO_ADDRESS "hello.example.com"
#endif
#ifndef HELLO_PORT
#define HELLO_PORT 1234
#endif
#define DIE_S2(s) #s
#define DIE_S1(s) DIE_S2(s)
#define DIE(...) \
do { \
int _errno = errno; \
fprintf(stderr, __VA_ARGS__); \
errno = _errno; \
perror("File: " __FILE__ "\nLine: " DIE_S1(__LINE__)); \
abort(); \
} while (0)
#define EH(LABEL, ...) \
do { \
if (errno == EINTR) { \
goto LABEL; \
} \
DIE(__VA_ARGS__); \
} while (0)
int main(int argc, char **argv)
{
ssize_t hostnamelen = 1;
char *hostname = malloc(hostnamelen);
if (hostname == NULL) {
DIE("malloc hates me.\n");
}
while (gethostname(hostname, hostnamelen)) {
if (errno != ENAMETOOLONG) {
DIE("gethostname fail.\n");
}
hostnamelen <<= 1;
free(hostname);
hostname = malloc(hostnamelen);
if (hostname == NULL) {
DIE("malloc hates me.\n");
}
}
hostnamelen = strlen(hostname) + 1;
socklen_t addrlen;
struct sockaddr *addr;
struct addrinfo ai_hints, *ai_lookup;
memset(&ai_hints, 0, sizeof(ai_hints));
ai_hints.ai_family = PF_INET;
ai_hints.ai_socktype = SOCK_DGRAM;
ai_hints.ai_flags = AI_NUMERICSERV;
if (getaddrinfo("tigger", "8087", &ai_hints, &ai_lookup)) {
DIE("Looking up 'tigger' failed.\n");
}
addrlen = ai_lookup->ai_addrlen;
addr = malloc((size_t)addrlen);
if (addr == NULL) {
DIE("malloc hates me.\n");
}
memcpy(addr, ai_lookup->ai_addr, (size_t)addrlen);
freeaddrinfo(ai_lookup);
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
DIE("Can't get a udp4 socket.\n");
}
for (;;) {
ssize_t r = sendto(s, hostname, hostnamelen, 0, addr, addrlen);
if (r != hostnamelen) {
EH(stopped, "sendto returned %zd.\n", r);
}
if (sleep(5)) {
EH(stopped, "nightmare\n");
}
}
stopped:
free(addr);
return 0;
}