author | viric@llimona |
Tue, 30 May 2006 01:10:00 +0200 | |
changeset 53 | 667cd5966695 |
parent 39 | 60858d13b22c |
permissions | -rw-r--r-- |
35 | 1 |
#include <sys/types.h> |
2 |
#include <sys/socket.h> |
|
3 |
#include <netdb.h> |
|
4 |
#include <string.h> |
|
5 |
#include <stdbool.h> |
|
6 |
#include <stdio.h> |
|
7 |
#include <unistd.h> |
|
8 |
#include <errno.h> |
|
9 |
||
10 |
#include "syslog.h" |
|
11 |
||
12 |
/* Prototypes */ |
|
13 |
static int init_socket(const char * restrict node, |
|
14 |
const char * restrict service); |
|
15 |
||
16 |
/* Local globals */ |
|
17 |
static int out_socket; |
|
18 |
static bool socket_enabled = false; |
|
19 |
||
20 |
||
21 |
/* Returns -2 when the socket wasn't initialized by config, 0 when all was ok, |
|
22 |
* -1 on more important error */ |
|
23 |
int init_out_udp() |
|
24 |
{ |
|
25 |
char node[MAX_STRING]; |
|
26 |
char service[MAX_STRING]; |
|
27 |
||
28 |
/* Get the configuration */ |
|
29 |
get_config(TO_UDP_HOST, node, MAX_STRING); |
|
30 |
get_config(TO_UDP_PORT, service, MAX_STRING); |
|
31 |
||
32 |
if (strncmp(node, "disabled", MAX_STRING) == 0) |
|
33 |
return -2; |
|
34 |
||
35 |
if (strncmp(service, "disabled", MAX_STRING) == 0) |
|
36 |
return -2; |
|
37 |
||
38 |
return init_socket(node, service); |
|
39 |
} |
|
40 |
||
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
41 |
/* Returns 0 when all was ok, -1 on error */ |
35 | 42 |
static int init_socket(const char * restrict node, |
43 |
const char * restrict service) |
|
44 |
{ |
|
45 |
int res; |
|
46 |
struct addrinfo *addr_res, *ptr; |
|
47 |
struct addrinfo hints; |
|
48 |
||
49 |
/* Prepare the hints for datagram */ |
|
50 |
memset(&hints, 0, sizeof(hints)); |
|
51 |
hints.ai_flags = AI_ADDRCONFIG; |
|
52 |
hints.ai_family = AF_UNSPEC; |
|
53 |
hints.ai_socktype = SOCK_DGRAM; |
|
54 |
hints.ai_addr = NULL; |
|
55 |
hints.ai_canonname = NULL; |
|
56 |
hints.ai_next = NULL; |
|
57 |
||
58 |
/* Ask the system for the service */ |
|
59 |
res = getaddrinfo(node, service, &hints, &addr_res); |
|
60 |
if (res != 0) |
|
61 |
{ |
|
62 |
fprintf(stderr, "Error trying to resolve the out_udp" |
|
63 |
" name: %s\n", gai_strerror(res)); |
|
64 |
if (res == EAI_SYSTEM) |
|
65 |
{ |
|
66 |
fprintf(stderr, " More system info: %s\n", |
|
67 |
strerror(errno)); |
|
68 |
} |
|
69 |
return -1; |
|
70 |
} |
|
71 |
||
72 |
ptr = addr_res; |
|
73 |
do |
|
74 |
{ |
|
75 |
res = -1; /* For further error catching */ |
|
76 |
out_socket = socket(ptr->ai_family, ptr->ai_socktype, |
|
77 |
ptr->ai_protocol); |
|
78 |
if (out_socket == -1) |
|
79 |
continue; |
|
80 |
res = connect(out_socket, ptr->ai_addr, ptr->ai_addrlen); |
|
81 |
if (res == 0) |
|
82 |
{ |
|
83 |
break; |
|
84 |
} |
|
85 |
else /* Unsuccesful connect */ |
|
86 |
{ |
|
87 |
close(out_socket); |
|
88 |
continue; |
|
89 |
} |
|
90 |
||
91 |
} while((ptr = ptr->ai_next) != NULL); |
|
92 |
||
93 |
freeaddrinfo(addr_res); |
|
94 |
||
95 |
/* If everything is fine */ |
|
96 |
if (res == 0) |
|
97 |
{ |
|
98 |
socket_enabled = true; |
|
99 |
} |
|
100 |
||
101 |
return res; |
|
102 |
} |
|
103 |
||
104 |
||
105 |
/* Return -2 when the socket is not enabled. Otherwise, the output of send().*/ |
|
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
106 |
int write_out_udp(const char * restrict buf, const int len) |
35 | 107 |
{ |
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
108 |
int res; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
109 |
|
35 | 110 |
if (!socket_enabled) |
111 |
return -2; |
|
112 |
||
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
113 |
res = send(out_socket, buf, len, 0); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
114 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
115 |
if (res == -1) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
116 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
117 |
fprintf(stderr, "Error sending to the UDP socket: %s\n", |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
118 |
strerror(errno)); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
119 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
120 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
121 |
return res; |
35 | 122 |
} |
123 |
||
124 |
/* Return -2 when the socket is not enabled. Otherwise, the output of close().*/ |
|
125 |
int close_out_udp() |
|
126 |
{ |
|
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
127 |
int res; |
35 | 128 |
if (!socket_enabled) |
129 |
return -2; |
|
130 |
||
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
131 |
res = close(out_socket); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
132 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
133 |
if (res == -1) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
134 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
135 |
fprintf(stderr, "Error closing the output UDP socket: %s\n", |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
136 |
strerror(errno)); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
137 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
138 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
35
diff
changeset
|
139 |
return res; |
35 | 140 |
} |