libptpmgmt  1.3
libptpmgmt library that provides the functionality of linuxptp pmc
sock.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-3.0-or-later
2  SPDX-FileCopyrightText: Copyright © 2021 Erez Geva <ErezGeva2@gmail.com> */
3 
19 #ifndef __PTPMGMT_SOCK_H
20 #define __PTPMGMT_SOCK_H
21 
22 #ifdef __cplusplus
23 #include "buf.h"
24 #ifdef __PTPMGMT_HAVE_SYS_UN_H
25 #include <sys/un.h>
26 #endif
27 #ifdef __PTPMGMT_HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef __linux__
31 #include <linux/if_packet.h>
32 #endif
33 #include "cfg.h"
34 #include "ptp.h"
35 
36 __PTPMGMT_NAMESPACE_BEGIN
37 
43 class SockBase
44 {
45  protected:
47  int m_fd;
48  bool m_isInit;
49  SockBase() : m_fd(-1), m_isInit(false) {}
50  bool sendReply(ssize_t cnt, size_t len) const;
51  virtual bool sendBase(const void *msg, size_t len) = 0;
52  virtual ssize_t rcvBase(void *buf, size_t bufSize, bool block) = 0;
53  virtual bool initBase() = 0;
54  virtual void closeChild() {}
55  void closeBase();
56 
57  public:
58  virtual ~SockBase() { closeBase(); }
64  void close() { closeBase(); }
69  bool init() { return initBase(); }
78  bool send(const void *msg, size_t len)
79  { return sendBase(msg, len); }
88  bool send(Buf &buf, size_t len)
89  { return sendBase(buf.get(), len); }
99  bool sendBuf(Buf &buf, size_t len)
100  { return sendBase(buf.get(), len); }
110  ssize_t rcv(void *buf, size_t bufSize, bool block = false)
111  { return rcvBase(buf, bufSize, block); }
120  ssize_t rcv(Buf &buf, bool block = false)
121  { return rcvBase(buf.get(), buf.size(), block); }
131  ssize_t rcvBuf(Buf &buf, bool block = false)
132  { return rcvBase(buf.get(), buf.size(), block); }
140  int getFd() const { return m_fd; }
148  int fileno() const { return m_fd; }
149  #ifdef __PTPMGMT_SWIG_THREAD_START
150  __PTPMGMT_SWIG_THREAD_START;
151  #endif
166  bool poll(uint64_t timeout_ms = 0) const;
184  bool tpoll(uint64_t &timeout_ms) const; /* poll with timeout update */
185  #ifdef __PTPMGMT_SWIG_THREAD_END
186  __PTPMGMT_SWIG_THREAD_END;
187  #endif
188 };
189 
196 class SockUnix : public SockBase
197 {
198  private:
199  std::string m_me, m_peer, m_homeDir, m_lastFrom;
200  sockaddr_un m_peerAddr;
201  bool setPeerInternal(const std::string &str, bool useAbstract);
202  bool sendAny(const void *msg, size_t len, const sockaddr_un &addr) const;
203  static void setUnixAddr(sockaddr_un &addr, const std::string &str);
204  protected:
206  bool sendBase(const void *msg, size_t len) override final;
207  ssize_t rcvBase(void *buf, size_t bufSize, bool block) override final;
208  bool initBase() override final;
209  void closeChild() override final;
212  public:
213  SockUnix() { setUnixAddr(m_peerAddr, m_peer); }
218  const std::string &getPeerAddress() const { return m_peer; }
223  const char *getPeerAddress_c() const { return m_peer.c_str(); }
228  bool isPeerAddressAbstract() const { return isAddressAbstract(m_peer); }
237  bool setPeerAddress(const std::string &string, bool useAbstract = false) {
238  return setPeerInternal(string, useAbstract);
239  }
247  bool setPeerAddress(const ConfigFile &cfg, const std::string &section = "") {
248  return setPeerInternal(cfg.uds_address(section), false);
249  }
254  const std::string &getSelfAddress() const { return m_me; }
259  const char *getSelfAddress_c() const { return m_me.c_str(); }
264  bool isSelfAddressAbstract() const { return isAddressAbstract(m_me); }
274  bool setSelfAddress(const std::string &string);
286  bool setSelfAddress(const std::string &string, bool useAbstract);
296  bool setDefSelfAddress(const std::string &rootBase = "",
297  const std::string &useDef = "");
302  const std::string &getHomeDir();
307  const char *getHomeDir_c();
318  bool sendTo(const void *msg, size_t len, const std::string &addrStr) const;
331  bool sendTo(const void *msg, size_t len, const std::string &addrStr,
332  bool useAbstract) const;
345  bool sendTo(Buf &buf, size_t len, const std::string &addrStr,
346  bool useAbstract = false) const
347  { return sendTo(buf.get(), len, addrStr, useAbstract); }
359  ssize_t rcvFrom(void *buf, size_t bufSize, std::string &from,
360  bool block = false) const;
371  ssize_t rcvFrom(Buf &buf, std::string &from, bool block = false) const
372  { return rcvFrom(buf.get(), buf.size(), from, block); }
383  ssize_t rcvFrom(void *buf, size_t bufSize, bool block = false)
384  { return rcvFrom(buf, bufSize, m_lastFrom, block); }
394  ssize_t rcvFrom(Buf &buf, bool block = false)
395  { return rcvFrom(buf.get(), buf.size(), m_lastFrom, block); }
406  ssize_t rcvBufFrom(Buf &buf, bool block = false)
407  { return rcvFrom(buf.get(), buf.size(), m_lastFrom, block); }
415  const std::string &getLastFrom() const { return m_lastFrom; }
423  const char *getLastFrom_c() const { return m_lastFrom.c_str(); }
428  bool isLastFromAbstract() const { return isAddressAbstract(m_lastFrom); }
434  static bool isAddressAbstract(const std::string &addr) {
435  return !addr.empty() && addr[0] == 0;
436  }
437 };
438 
444 class SockBaseIf : public SockBase
445 {
446  protected:
448  std::string m_ifName; /* interface to use */
449  Binary m_mac;
450  int m_ifIndex;
451  bool m_have_if;
452  bool setInt(const IfInfo &ifObj);
453  SockBaseIf() : m_have_if(false) {}
454  virtual bool setAllBase(const ConfigFile &cfg, const std::string &section) = 0;
457  public:
466  bool setIfUsingName(const std::string &ifName);
475  bool setIfUsingIndex(int ifIndex);
484  bool setIf(const IfInfo &ifObj);
497  bool setAll(const IfInfo &ifObj, const ConfigFile &cfg,
498  const std::string &section = "") {
499  return setIf(ifObj) && setAllBase(cfg, section);
500  }
513  bool setAllInit(const IfInfo &ifObj, const ConfigFile &cfg,
514  const std::string &section = "") {
515  return setAll(ifObj, cfg, section) && initBase();
516  }
517 };
518 
525 class SockIp : public SockBaseIf
526 {
527  protected:
529  int m_domain, m_udp_ttl;
530  /* First for bind then for send */
531  sockaddr *m_addr;
532  size_t m_addr_len;
533  const char *m_mcast_str; /* string form */
534  Binary m_mcast;
535  SockIp(int domain, const char *mcast, sockaddr *addr, size_t len);
536  virtual bool initIp() = 0;
537  bool sendBase(const void *msg, size_t len) override final;
538  ssize_t rcvBase(void *buf, size_t bufSize, bool block) override final;
539  bool initBase() override final;
542  public:
552  bool setUdpTtl(uint8_t udp_ttl);
564  bool setUdpTtl(const ConfigFile &cfg, const std::string &section = "");
565 };
566 
570 class SockIp4 : public SockIp
571 {
572  private:
573  sockaddr_in m_addr4;
574 
575  protected:
577  bool initIp() override final;
578  bool setAllBase(const ConfigFile &cfg,
579  const std::string &section) override final;
580 
581  public:
582  SockIp4();
584 };
585 
589 class SockIp6 : public SockIp
590 {
591  private:
592  sockaddr_in6 m_addr6;
593  int m_udp6_scope;
594 
595  protected:
597  bool initIp() override final;
598  bool setAllBase(const ConfigFile &cfg,
599  const std::string &section) override final;
600 
601  public:
602  SockIp6();
613  bool setScope(uint8_t udp6_scope);
624  bool setScope(const ConfigFile &cfg, const std::string &section = "");
625 };
626 
631 class SockRaw : public SockBaseIf
632 {
633  private:
634  Binary m_ptp_dst_mac;
635  int m_socket_priority;
636  sockaddr_ll m_addr;
637  iovec m_iov_tx[2], m_iov_rx[2];
638  msghdr m_msg_tx, m_msg_rx;
639  ethhdr m_hdr;
640  uint8_t m_rx_buf[sizeof(ethhdr)];
641 
642  protected:
644  bool setAllBase(const ConfigFile &cfg,
645  const std::string &section) override final;
646  bool sendBase(const void *msg, size_t len) override final;
647  ssize_t rcvBase(void *buf, size_t bufSize, bool block) override final;
648  bool initBase() override final;
649 
650  public:
651  SockRaw();
663  bool setPtpDstMacStr(const std::string &string);
672  bool setPtpDstMac(const Binary &ptp_dst_mac);
682  bool setPtpDstMac(const void *ptp_dst_mac, size_t len);
693  bool setPtpDstMac(const uint8_t *ptp_dst_mac, size_t len);
704  bool setPtpDstMac(const ConfigFile &cfg, const std::string &section = "");
715  bool setSocketPriority(uint8_t socket_priority);
726  bool setSocketPriority(const ConfigFile &cfg, const std::string &section = "");
727 };
728 
729 __PTPMGMT_NAMESPACE_END
730 #else /* __cplusplus */
731 #include "c/sock.h"
732 #endif /* __cplusplus */
733 
734 #endif /* __PTPMGMT_SOCK_H */
Buffer for send, receive, build, and parse.
C interface to configuration class.
C interface to network and PHC classes.
C interface to sockets classes.
Hold octets.
Definition: bin.h:28
Definition: buf.h:26
size_t size() const
Definition: buf.h:58
void * get() const
Definition: buf.h:48
Hold configuration parameters.
Definition: cfg.h:92
const std::string & uds_address(const std::string &section="") const
Network interface information.
Definition: ptp.h:44
Base for socket that uses network interface directly.
Definition: sock.h:445
bool setIfUsingIndex(int ifIndex)
bool setIf(const IfInfo &ifObj)
bool setIfUsingName(const std::string &ifName)
bool setAllInit(const IfInfo &ifObj, const ConfigFile &cfg, const std::string &section="")
Definition: sock.h:513
bool setAll(const IfInfo &ifObj, const ConfigFile &cfg, const std::string &section="")
Definition: sock.h:497
Base class for all sockets.
Definition: sock.h:44
int fileno() const
Definition: sock.h:148
ssize_t rcvBuf(Buf &buf, bool block=false)
Definition: sock.h:131
bool init()
Definition: sock.h:69
bool send(const void *msg, size_t len)
Definition: sock.h:78
int getFd() const
Definition: sock.h:140
bool send(Buf &buf, size_t len)
Definition: sock.h:88
bool tpoll(uint64_t &timeout_ms) const
bool sendBuf(Buf &buf, size_t len)
Definition: sock.h:99
ssize_t rcv(void *buf, size_t bufSize, bool block=false)
Definition: sock.h:110
ssize_t rcv(Buf &buf, bool block=false)
Definition: sock.h:120
bool poll(uint64_t timeout_ms=0) const
void close()
Definition: sock.h:64
UDP over IP version 4 socket.
Definition: sock.h:571
UDP over IP version 6 socket.
Definition: sock.h:590
bool setScope(const ConfigFile &cfg, const std::string &section="")
bool setScope(uint8_t udp6_scope)
Base for UDP sockets.
Definition: sock.h:526
bool setUdpTtl(const ConfigFile &cfg, const std::string &section="")
bool setUdpTtl(uint8_t udp_ttl)
Raw socket that uses PTP over Ethernet.
Definition: sock.h:632
bool setPtpDstMac(const void *ptp_dst_mac, size_t len)
bool setPtpDstMac(const ConfigFile &cfg, const std::string &section="")
bool setPtpDstMac(const uint8_t *ptp_dst_mac, size_t len)
bool setSocketPriority(const ConfigFile &cfg, const std::string &section="")
bool setPtpDstMacStr(const std::string &string)
bool setPtpDstMac(const Binary &ptp_dst_mac)
bool setSocketPriority(uint8_t socket_priority)
Unix socket.
Definition: sock.h:197
ssize_t rcvFrom(void *buf, size_t bufSize, std::string &from, bool block=false) const
const char * getSelfAddress_c() const
Definition: sock.h:259
bool setPeerAddress(const ConfigFile &cfg, const std::string &section="")
Definition: sock.h:247
bool sendTo(const void *msg, size_t len, const std::string &addrStr) const
bool sendTo(Buf &buf, size_t len, const std::string &addrStr, bool useAbstract=false) const
Definition: sock.h:345
SockUnix()
Definition: sock.h:213
bool setSelfAddress(const std::string &string)
ssize_t rcvFrom(Buf &buf, bool block=false)
Definition: sock.h:394
static bool isAddressAbstract(const std::string &addr)
Definition: sock.h:434
bool setDefSelfAddress(const std::string &rootBase="", const std::string &useDef="")
ssize_t rcvBufFrom(Buf &buf, bool block=false)
Definition: sock.h:406
bool sendTo(const void *msg, size_t len, const std::string &addrStr, bool useAbstract) const
bool isPeerAddressAbstract() const
Definition: sock.h:228
ssize_t rcvFrom(void *buf, size_t bufSize, bool block=false)
Definition: sock.h:383
bool setSelfAddress(const std::string &string, bool useAbstract)
bool isSelfAddressAbstract() const
Definition: sock.h:264
const char * getPeerAddress_c() const
Definition: sock.h:223
const std::string & getHomeDir()
const std::string & getSelfAddress() const
Definition: sock.h:254
const std::string & getPeerAddress() const
Definition: sock.h:218
bool isLastFromAbstract() const
Definition: sock.h:428
ssize_t rcvFrom(Buf &buf, std::string &from, bool block=false) const
Definition: sock.h:371
const char * getHomeDir_c()
bool setPeerAddress(const std::string &string, bool useAbstract=false)
Definition: sock.h:237
const char * getLastFrom_c() const
Definition: sock.h:423
const std::string & getLastFrom() const
Definition: sock.h:415