• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 21336882381

25 Jan 2026 05:47PM UTC coverage: 45.06% (-0.7%) from 45.72%
21336882381

Pull #1984

github

web-flow
Merge b8c8613eb into 704337b09
Pull Request #1984: Add conditionals for Protobuf 22+ API changes

8556 of 19814 branches covered (43.18%)

5 of 6 new or added lines in 3 files covered. (83.33%)

324 existing lines in 56 files now uncovered.

22100 of 49046 relevant lines covered (45.06%)

48.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/include/ola/network/SocketAddress.h
1
/*
2
 * This library is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU Lesser General Public
4
 * License as published by the Free Software Foundation; either
5
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 * This library is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 * Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with this library; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
 *
16
 * SocketAddress.h
17
 * Represents a sockaddr structure.
18
 * Copyright (C) 2012 Simon Newton
19
 */
20

21
/**
22
 * @addtogroup network
23
 * @{
24
 * @file SocketAddress.h
25
 * @brief Represents Socket Addresses.
26
 * @}
27
 */
28

29
#ifndef INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
30
#define INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
31

32
#include <ola/network/IPV4Address.h>
33
#include <ola/base/Macro.h>
34
#include <stdint.h>
35
#ifdef _WIN32
36
#define VC_EXTRALEAN
37
#define WIN32_LEAN_AND_MEAN
38
#include <ola/win/CleanWinSock2.h>
39
#else
40
#include <sys/socket.h>
41
#endif  // _WIN32
42
#include <sstream>
43
#include <string>
44

45
namespace ola {
46
namespace network {
47

48
/**
49
 * @addtogroup network
50
 * @{
51
 */
52

53
/**
54
 * @brief The base SocketAddress.
55
 *
56
 * One day if we support V6 there will be another derived class.
57
 **/
58
class SocketAddress {
368✔
59
 public:
60
    virtual ~SocketAddress() {}
336✔
61

62
    virtual uint16_t Family() const = 0;
63
    virtual bool ToSockAddr(struct sockaddr *addr, unsigned int size) const = 0;
64
    virtual std::string ToString() const = 0;
65

66
    friend std::ostream& operator<<(std::ostream &out,
17✔
67
                                    const SocketAddress &address) {
68
      return out << address.ToString();
34✔
69
    }
70
};
71

72

73
/**
74
 * @brief An IPv4 SocketAddress.
75
 *
76
 * Wraps a sockaddr_in.
77
 */
78
class IPV4SocketAddress: public SocketAddress {
79
 public:
80
    IPV4SocketAddress()
105✔
81
        : SocketAddress(),
98✔
82
          m_host(),
105✔
83
          m_port(0) {
105✔
84
    }
×
85

86
    IPV4SocketAddress(const IPV4Address &host, uint16_t port)
199✔
87
        : SocketAddress(),
130✔
88
          m_host(host),
207✔
89
          m_port(port) {
207✔
90
    }
91
    IPV4SocketAddress(const IPV4SocketAddress &other)
21✔
92
        : SocketAddress(),
15✔
93
          m_host(other.m_host),
21✔
94
          m_port(other.m_port) {
21✔
95
    }
96

UNCOV
97
    ~IPV4SocketAddress() {}
✔
98

99
    IPV4SocketAddress& operator=(const IPV4SocketAddress &other) {
75✔
100
      if (this != &other) {
75✔
101
        m_host = other.m_host;
75✔
102
        m_port = other.m_port;
75✔
103
      }
104
      return *this;
75✔
105
    }
106

107
    bool operator==(const IPV4SocketAddress &other) const {
11✔
108
      return m_host == other.m_host && m_port == other.m_port;
13✔
109
    }
110

111
    bool operator!=(const IPV4SocketAddress &other) const {
2✔
112
      return !(*this == other);
4✔
113
    }
114

115
    /**
116
     * @brief Less than operator for partial ordering.
117
     *
118
     * Sorts by host, then port.
119
     */
120
    bool operator<(const IPV4SocketAddress &other) const {
4✔
121
      if (m_host == other.m_host)
4✔
122
        return m_port < other.m_port;
2✔
123
      else
124
        return m_host < other.m_host;
2✔
125
    }
126

127
    /**
128
     * @brief Greater than operator.
129
     *
130
     * Sorts by host, then port.
131
     */
132
    bool operator>(const IPV4SocketAddress &other) const {
4✔
133
      if (m_host == other.m_host)
4✔
134
        return m_port > other.m_port;
2✔
135
      else
136
        return m_host > other.m_host;
2✔
137
    }
138

139
    uint16_t Family() const { return AF_INET; }
41✔
140
    const IPV4Address& Host() const { return m_host; }
137✔
141
    void Host(const IPV4Address &host) { m_host = host; }
142
    uint16_t Port() const { return m_port; }
95✔
143
    void Port(uint16_t port) { m_port = port; }
144

145
    std::string ToString() const;
146

147
    static bool FromString(const std::string &str,
148
                           IPV4SocketAddress *socket_address);
149

150
    // useful for testing
151
    static IPV4SocketAddress FromStringOrDie(const std::string &address);
152

153
    bool ToSockAddr(struct sockaddr *addr, unsigned int size) const;
154

155
 private:
156
    IPV4Address m_host;
157
    uint16_t m_port;
158
};
159

160

161
/**
162
 * @brief a Generic Socket Address
163
 *
164
 * Wraps a struct sockaddr.
165
 */
166
class GenericSocketAddress: public SocketAddress {
35✔
167
 public:
168
    explicit GenericSocketAddress(const struct sockaddr &addr)
32✔
169
      : m_addr(addr) {
32✔
170
    }
32✔
171

172
    GenericSocketAddress() {
×
173
      memset(reinterpret_cast<uint8_t*>(&m_addr), 0, sizeof(m_addr));
×
174
    }
×
175

176
    GenericSocketAddress(const GenericSocketAddress& other) {
3✔
177
      memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
3✔
178
    }
179

180
    bool IsValid() const;
181

182
    uint16_t Family() const {
50✔
183
      return m_addr.sa_family;
50✔
184
    }
185

186
    GenericSocketAddress& operator=(const GenericSocketAddress &other) {
187
      if (this != &other) {
188
        memcpy(&m_addr, &(other.m_addr), sizeof(m_addr));
189
      }
190
      return *this;
191
    }
192

193
    bool ToSockAddr(struct sockaddr *addr,
5✔
194
                    OLA_UNUSED unsigned int size) const {
195
      *addr = m_addr;
5✔
196
      return true;
5✔
197
    }
198

199
    std::string ToString() const;
200

201
    // Return a IPV4SocketAddress object, only valid if Family() is AF_INET
202
    IPV4SocketAddress V4Addr() const;
203
    // Add V6 here as well
204

205
 private:
206
    struct sockaddr m_addr;
207
};
208
/**
209
 * @}
210
 */
211
}  // namespace network
212
}  // namespace ola
213
#endif  // INCLUDE_OLA_NETWORK_SOCKETADDRESS_H_
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc