implemented UDP v4 address class
Stefan Schuermans

Stefan Schuermans commited on 2011-11-08 19:52:05
Showing 4 changed files, with 251 additions and 0 deletions.

... ...
@@ -0,0 +1,21 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include "Addr.h"
7
+
8
+namespace Blinker {
9
+
10
+/// constructor
11
+Addr::Addr()
12
+{
13
+}
14
+
15
+/// virtual destructor
16
+Addr::~Addr()
17
+{
18
+}
19
+
20
+} // namespace Blinker
21
+
... ...
@@ -0,0 +1,57 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef ADDR_H
7
+#define ADDR_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// socket address
14
+class Addr
15
+{
16
+public:
17
+  /// constructor
18
+  Addr();
19
+
20
+  /// virtual destructor
21
+  virtual ~Addr();
22
+
23
+public:
24
+  /// return address family
25
+  virtual int getFamily() const = 0;
26
+
27
+  /**
28
+   * @brief parse from string format
29
+   * @param[in] str string format
30
+   * @return if parsing was successful
31
+   */
32
+  virtual bool fromStr(const std::string &str) = 0;
33
+
34
+  /**
35
+   * @brief convert to string format
36
+   * @return string format
37
+   */
38
+  virtual std::string toStr() const = 0;
39
+
40
+  /**
41
+   * @brief set to sockaddr
42
+   * @parma[in] addr sockaddr structure to use
43
+   * @return if using sockaddr was successful
44
+   */
45
+  virtual bool fromSockaddr(const struct sockaddr &addr) = 0;
46
+
47
+  /**
48
+   * @brief convert to sockaddr
49
+   * @return sockaddr structure
50
+   */
51
+  virtual const struct sockaddr & toSockaddr() const = 0;
52
+}; // class Addr
53
+
54
+} // namespace Blinker
55
+
56
+#endif // #ifndef ADDR_H
57
+
... ...
@@ -0,0 +1,108 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <arpa/inet.h>
7
+#include <netinet/in.h>
8
+#include <sstream>
9
+#include <stdlib.h>
10
+#include <string>
11
+#include <sys/socket.h>
12
+#include <sys/types.h>
13
+
14
+#include "Udp4Addr.h"
15
+
16
+namespace Blinker {
17
+
18
+/// constructor
19
+Udp4Addr::Udp4Addr()
20
+{
21
+  m_addr.sin_family = AF_INET;
22
+  m_addr.sin_port = htons(0);
23
+  m_addr.sin_addr.s_addr = htonl(INADDR_NONE);
24
+}
25
+
26
+/// virtual destructor
27
+Udp4Addr::~Udp4Addr()
28
+{
29
+}
30
+
31
+/// return address family
32
+int Udp4Addr::getFamily() const
33
+{
34
+  return AF_INET;
35
+}
36
+
37
+/**
38
+ * @brief parse from string format
39
+ * @param[in] str string format
40
+ * @return if parsing was successful
41
+ */
42
+bool Udp4Addr::fromStr(const std::string &str)
43
+{
44
+  std::string::size_type posColon;
45
+  std::string strIp, strPort;
46
+  struct in_addr iaIp;
47
+  unsigned long iPort;
48
+  const char *szPort;
49
+  char *szPortEnd;
50
+
51
+  // split address into IP and port
52
+  posColon = str.find(':');
53
+  if (posColon == std::string::npos)
54
+    return false;
55
+  strIp = str.substr(0, posColon);
56
+  strPort = str.substr(posColon + 1);
57
+
58
+  // parse IP
59
+  if (!inet_aton(strIp.c_str(), &iaIp))
60
+    return false;
61
+
62
+  // parse port
63
+  szPort = strPort.c_str();
64
+  iPort = strtoul(szPort, &szPortEnd, 10);
65
+  if (*szPort == 0 || szPortEnd != 0)
66
+    return false;
67
+
68
+  m_addr.sin_family = AF_INET;
69
+  m_addr.sin_port = htons(iPort);
70
+  m_addr.sin_addr = iaIp;
71
+  return false;
72
+}
73
+
74
+/**
75
+ * @brief convert to string format
76
+ * @return string format
77
+ */
78
+std::string Udp4Addr::toStr() const
79
+{
80
+  std::stringstream strm;
81
+  strm << inet_ntoa(m_addr.sin_addr) << ":" << ntohs(m_addr.sin_port);
82
+  return strm.str();
83
+}
84
+
85
+/**
86
+ * @brief set to sockaddr
87
+ * @parma[in] addr sockaddr structure to use
88
+ * @return if using sockaddr was successful
89
+ */
90
+bool Udp4Addr::fromSockaddr(const struct sockaddr &addr)
91
+{
92
+  if (addr.sa_family != AF_INET)
93
+    return false;
94
+  m_addr = *(const struct sockaddr_in *)&addr;
95
+  return true;
96
+}
97
+
98
+/**
99
+ * @brief convert to sockaddr
100
+ * @return sockaddr structure
101
+ */
102
+const struct sockaddr & Udp4Addr::toSockaddr() const
103
+{
104
+  return *(const struct sockaddr *)&m_addr;
105
+}
106
+
107
+} // namespace Blinker
108
+
... ...
@@ -0,0 +1,65 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef UDP4ADDR_H
7
+#define UDP4ADDR_H
8
+
9
+#include <netinet/in.h>
10
+#include <string>
11
+#include <sys/socket.h>
12
+#include <sys/types.h>
13
+
14
+#include "Addr.h"
15
+
16
+namespace Blinker {
17
+
18
+/// UDP v4 address
19
+class Udp4Addr: public Addr
20
+{
21
+public:
22
+  /// constructor
23
+  Udp4Addr();
24
+
25
+  /// virtual destructor
26
+  virtual ~Udp4Addr();
27
+
28
+public:
29
+  /// return address family
30
+  virtual int getFamily() const;
31
+
32
+  /**
33
+   * @brief parse from string format
34
+   * @param[in] str string format
35
+   * @return if parsing was successful
36
+   */
37
+  virtual bool fromStr(const std::string &str);
38
+
39
+  /**
40
+   * @brief convert to string format
41
+   * @return string format
42
+   */
43
+  virtual std::string toStr() const;
44
+
45
+  /**
46
+   * @brief set to sockaddr
47
+   * @parma[in] addr sockaddr structure to use
48
+   * @return if using sockaddr was successful
49
+   */
50
+  virtual bool fromSockaddr(const struct sockaddr &addr);
51
+
52
+  /**
53
+   * @brief convert to sockaddr
54
+   * @return sockaddr structure
55
+   */
56
+  virtual const struct sockaddr & toSockaddr() const;
57
+
58
+protected:
59
+  struct sockaddr_in m_addr;
60
+}; // class Udp4Addr
61
+
62
+} // namespace Blinker
63
+
64
+#endif // #ifndef UDP4ADDR_H
65
+
0 66