]> git.sesse.net Git - cubemap/blob - udpstream.cpp
Fix the vertical label of the cubemap_input Munin plugin.
[cubemap] / udpstream.cpp
1 #include <sys/socket.h>
2 #include <sys/types.h>
3
4 #include "log.h"
5 #include "markpool.h"
6 #include "udpstream.h"
7 #include "util.h"
8
9 UDPStream::UDPStream(const sockaddr_in6 &dst, MarkPool *mark_pool)
10         : dst(dst),
11           mark_pool(mark_pool),
12           fwmark(0)
13 {
14         sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
15         if (sock == -1) {
16                 // Oops. Ignore this output, then.
17                 log_perror("socket");
18                 return;
19         }
20
21         if (mark_pool != NULL) {
22                 fwmark = mark_pool->get_mark();
23                 if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {                          
24                         if (fwmark != 0) {
25                                 log_perror("setsockopt(SO_MARK)");
26                         }
27                 }
28         }
29 }
30
31 UDPStream::~UDPStream()
32 {
33         if (sock != -1) {
34                 safe_close(sock);
35         }
36         if (mark_pool != NULL) {
37                 mark_pool->release_mark(fwmark);
38         }
39 }
40
41 void UDPStream::send(const char *data, size_t bytes)
42 {
43         if (sock == -1) {
44                 return;
45         }
46         ssize_t err = sendto(sock, data, bytes, 0, reinterpret_cast<sockaddr *>(&dst), sizeof(dst));
47         if (err == -1) {
48                 log_perror("sendto");
49         }
50 }