socket_linux.h
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef RCDISCOVER_SOCKET_LINUX_H
37 #define RCDISCOVER_SOCKET_LINUX_H
38 
39 #include "socket.h"
40 
41 #include <string>
42 
43 #include <netinet/in.h>
44 
45 namespace rcdiscover
46 {
47 
51 class SocketLinux : public Socket<SocketLinux>
52 {
53  friend class Socket<SocketLinux>;
54 
55  public:
59  typedef int SocketType;
60 
61  public:
69  static SocketLinux create(in_addr_t dst_ip, uint16_t port, std::string iface_name);
70 
77  static std::vector<SocketLinux> createAndBindForAllInterfaces(uint16_t port);
78 
88  SocketLinux(int domain, int type, int protocol,
89  in_addr_t dst_ip, uint16_t port,
90  std::string iface_name);
91  SocketLinux(SocketLinux &&other);
93  ~SocketLinux();
94 
99  const sockaddr_in& getDestSockAddr() const;
100 
105  static const in_addr_t &getBroadcastAddr();
106 
107  protected:
112  const int &getHandleImpl() const;
113 
118  void bindImpl(const sockaddr_in &addr);
119 
124  void sendImpl(const std::vector<uint8_t> &sendbuf);
125 
129  void enableBroadcastImpl();
130 
134  void enableNonBlockingImpl();
135 
136  private:
144  void bindToDevice(const std::string &device);
145 
146  private:
147  int sock_;
148  sockaddr_in dst_addr_;
149 };
150 
151 }
152 
153 #endif // RCDISCOVER_SOCKET_LINUX_H
void bindToDevice(const std::string &device)
Binds this socket to a specific device (root privileges are required).
const int & getHandleImpl() const
Returns the native socket handle.
static std::vector< SocketLinux > createAndBindForAllInterfaces(uint16_t port)
Creates sockets for all interfaces and binds them to the respective interface.
Definition: socket_linux.cc:76
const sockaddr_in & getDestSockAddr() const
Returns the sockaddr to which the socket is bound.
Definition: socket_linux.cc:64
int SocketType
Type representing the native socket handle type.
Definition: socket_linux.h:59
void sendImpl(const std::vector< uint8_t > &sendbuf)
Sends data.
CRTP class for platform specific socket implementation.
Definition: socket.h:52
SocketLinux(int domain, int type, int protocol, in_addr_t dst_ip, uint16_t port, std::string iface_name)
Constructor.
static const in_addr_t & getBroadcastAddr()
Returns the broadcast address.
Definition: socket_linux.cc:58
void enableNonBlockingImpl()
Enables non-blocking operation for this socket.
void bindImpl(const sockaddr_in &addr)
Binds the socket to a specific sockaddr.
static SocketLinux create(in_addr_t dst_ip, uint16_t port, std::string iface_name)
Create a new socket.
Definition: socket_linux.cc:69
void enableBroadcastImpl()
Enables broadcast for this socket.
Socket implementation for Linux.
Definition: socket_linux.h:51
SocketLinux & operator=(SocketLinux &&other)


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32