Index: trunk/udplog/udp2log/udp2log.cpp |
— | — | @@ -22,6 +22,7 @@ |
23 | 23 | std::string logFileName("/var/log/udp2log/udp2log.log"); |
24 | 24 | std::string pidFileName("/var/run/udp2log.pid"); |
25 | 25 | std::string daemonUserName("udp2log"); |
| 26 | +std::string multicastAddr("0"); |
26 | 27 | |
27 | 28 | Udp2LogConfig config; |
28 | 29 | |
— | — | @@ -112,10 +113,11 @@ |
113 | 114 | using namespace boost::program_options; |
114 | 115 | unsigned int port = 8420; |
115 | 116 | bool daemon = false; |
116 | | - |
| 117 | + |
117 | 118 | // Process command line |
118 | 119 | options_description optDesc; |
119 | 120 | optDesc.add_options() |
| 121 | + ("multicast", value<string>(&multicastAddr)->default_value(multicastAddr), "multicast address to listen to") |
120 | 122 | ("help", "Show help message.") |
121 | 123 | ("port,p", value<unsigned int>(&port)->default_value(port), "UDP port.") |
122 | 124 | ("config-file,f", value<string>(&configFileName)->default_value(configFileName), |
— | — | @@ -182,13 +184,18 @@ |
183 | 185 | // Open the receiving socket |
184 | 186 | IPAddress any(INADDR_ANY); |
185 | 187 | SocketAddress saddr(any, (unsigned short int)port); |
186 | | - UDPSocket socket; |
187 | | - if (!socket) { |
188 | | - cerr << "Unable to open socket\n"; |
189 | | - return 1; |
190 | | - } |
191 | | - socket.Bind(saddr); |
192 | | - |
| 188 | + Socket socket; |
| 189 | + if(strcmp("0", multicastAddr.c_str())){ //if multicast |
| 190 | + socket = *(new MulticastSocket(any, port, multicastAddr.c_str())); |
| 191 | + } |
| 192 | + else{ |
| 193 | + socket = *(new UDPSocket()); |
| 194 | + if (!socket) { |
| 195 | + cerr << "Unable to open socket\n"; |
| 196 | + return 1; |
| 197 | + } |
| 198 | + socket.Bind(saddr); |
| 199 | + } |
193 | 200 | // Process received packets |
194 | 201 | boost::shared_ptr<SocketAddress> address; |
195 | 202 | const size_t bufSize = 65536; |
Index: trunk/udplog/srclib/Socket.h |
— | — | @@ -15,6 +15,7 @@ |
16 | 16 | class Socket |
17 | 17 | { |
18 | 18 | public: |
| 19 | + Socket(){} |
19 | 20 | Socket(int domain, int type, int protocol) |
20 | 21 | : fd(-1) |
21 | 22 | { |
— | — | @@ -153,4 +154,29 @@ |
154 | 155 | } |
155 | 156 | }; |
156 | 157 | |
| 158 | + |
| 159 | +class MulticastSocket : public Socket |
| 160 | +{ |
| 161 | +public: |
| 162 | + MulticastSocket(int domain = PF_INET) |
| 163 | + : Socket(domain, SOCK_DGRAM, 0) {} |
| 164 | + |
| 165 | + MulticastSocket(IPAddress & addr, unsigned int port, const char* multicastAddr) |
| 166 | + : Socket(addr.GetDomain(), SOCK_DGRAM, 0) |
| 167 | + { |
| 168 | + struct ip_mreq imreq; |
| 169 | + bzero(&imreq, sizeof(struct ip_mreq)); |
| 170 | + boost::shared_ptr<SocketAddress> saddr = addr.NewSocketAddress(port); |
| 171 | + if (Connect(*saddr)) { |
| 172 | + good = false; |
| 173 | + } |
| 174 | + imreq.imr_multiaddr.s_addr = inet_addr(multicastAddr); |
| 175 | + imreq.imr_interface.s_addr = INADDR_ANY; |
| 176 | + |
| 177 | + // JOIN multicast group on default interface |
| 178 | + setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 179 | + (const void *)&imreq, sizeof(struct ip_mreq)); |
| 180 | + |
| 181 | + } |
| 182 | +}; |
157 | 183 | #endif |