LCOV - code coverage report
Current view: top level - libaddr - unix.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 16 100.0 %
Date: 2022-03-01 21:05:13 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2012-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/libaddr
       4             : //
       5             : // Permission is hereby granted, free of charge, to any person obtaining a
       6             : // copy of this software and associated documentation files (the
       7             : // "Software"), to deal in the Software without restriction, including
       8             : // without limitation the rights to use, copy, modify, merge, publish,
       9             : // distribute, sublicense, and/or sell copies of the Software, and to
      10             : // permit persons to whom the Software is furnished to do so, subject to
      11             : // the following conditions:
      12             : //
      13             : // The above copyright notice and this permission notice shall be included
      14             : // in all copies or substantial portions of the Software.
      15             : //
      16             : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      17             : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      18             : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      19             : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      20             : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      21             : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      22             : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      23             : #pragma once
      24             : 
      25             : /** \file
      26             :  * \brief The Unix libaddr class.
      27             :  *
      28             :  * This header defines the Unix address class. This is used to connect to
      29             :  * Unix sockets.
      30             :  *
      31             :  * The library supports all three types of Unix addresses supported by
      32             :  * Linux:
      33             :  *
      34             :  * * File based: a path to a file on disk.
      35             :  * * Abstract: a abstract path.
      36             :  * * Unnamed: a completely unnamed socket.
      37             :  *
      38             :  * The first one (File) is what most users are expected to use.
      39             :  *
      40             :  * The second one (Abstract) is what many X11 tools use to communicate.
      41             :  * Especially, it is called the "bus" in Gnome.
      42             :  *
      43             :  * The third one (Unnamed) is only useful for parent/child communication.
      44             :  * The child can be a process created by fork() and fork() + exec().
      45             :  */
      46             : 
      47             : // C++ library
      48             : //
      49             : #include    <memory>
      50             : #include    <string>
      51             : #include    <vector>
      52             : 
      53             : 
      54             : // C library
      55             : //
      56             : #include    <sys/un.h>
      57             : #include    <sys/socket.h>
      58             : 
      59             : 
      60             : 
      61             : namespace addr
      62             : {
      63             : 
      64             : 
      65             : 
      66             : 
      67             : /** \brief Initialize a Unix address as such.
      68             :  *
      69             :  * This function initializes a sockaddr_un with all zeroes except
      70             :  * for the sun_family which is set to AF_UNIX.
      71             :  *
      72             :  * return The initialized Unix address.
      73             :  */
      74         534 : constexpr struct sockaddr_un init_un()
      75             : {
      76         534 :     struct sockaddr_un un = sockaddr_un();
      77         534 :     un.sun_family = AF_UNIX;
      78         534 :     return un;
      79             : }
      80             : 
      81             : 
      82             : class unix
      83             : {
      84             : public:
      85             :     typedef std::shared_ptr<unix>   pointer_t;
      86             :     typedef std::vector<unix>       vector_t;
      87             :     typedef int                     socket_flag_t;
      88             : 
      89             :                                     unix();
      90             :                                     unix(sockaddr_un const & un);
      91             :                                     unix(std::string const & address, bool abstract = false);
      92             : 
      93             :     void                            set_un(sockaddr_un const & un);
      94             :     void                            make_unnamed();
      95             :     void                            set_file(std::string const & address);
      96             :     void                            set_abstract(std::string const & address);
      97             :     void                            set_uri(std::string const & address);
      98             :     bool                            set_from_socket(int s);
      99             : 
     100             :     bool                            is_file() const;
     101             :     bool                            is_abstract() const;
     102             :     bool                            is_unnamed() const;
     103             :     void                            get_un(sockaddr_un & un) const;
     104             :     std::string                     to_string() const;
     105             :     std::string                     to_uri() const;
     106             :     int                             unlink();
     107             : 
     108             :     bool                            operator == (unix const & rhs) const;
     109             :     bool                            operator != (unix const & rhs) const;
     110             :     bool                            operator <  (unix const & rhs) const;
     111             :     bool                            operator <= (unix const & rhs) const;
     112             :     bool                            operator >  (unix const & rhs) const;
     113             :     bool                            operator >= (unix const & rhs) const;
     114             : 
     115             : private:
     116             :     std::string                     verify_path(std::string const & path, bool abstract);
     117             : 
     118             :     sockaddr_un                     f_address = init_un();
     119             : };
     120             : 
     121             : 
     122             : 
     123             : 
     124             : 
     125             : }
     126             : // namespace addr
     127             : 
     128             : 
     129           9 : inline bool operator == (sockaddr_un const & a, sockaddr_un const & b)
     130             : {
     131           9 :     return memcmp(&a, &b, sizeof(sockaddr_un)) == 0;
     132             : }
     133             : 
     134             : 
     135           6 : inline bool operator != (sockaddr_un const & a, sockaddr_un const & b)
     136             : {
     137           6 :     return memcmp(&a, &b, sizeof(sockaddr_un)) != 0;
     138             : }
     139             : 
     140             : 
     141           6 : inline bool operator < (sockaddr_un const & a, sockaddr_un const & b)
     142             : {
     143           6 :     return memcmp(&a, &b, sizeof(sockaddr_un)) < 0;
     144             : }
     145             : 
     146             : 
     147           6 : inline bool operator <= (sockaddr_un const & a, sockaddr_un const & b)
     148             : {
     149           6 :     return memcmp(&a, &b, sizeof(sockaddr_un)) <= 0;
     150             : }
     151             : 
     152             : 
     153           6 : inline bool operator > (sockaddr_un const & a, sockaddr_un const & b)
     154             : {
     155           6 :     return memcmp(&a, &b, sizeof(sockaddr_un)) > 0;
     156             : }
     157             : 
     158             : 
     159           6 : inline bool operator >= (sockaddr_un const & a, sockaddr_un const & b)
     160             : {
     161           6 :     return memcmp(&a, &b, sizeof(sockaddr_un)) >= 0;
     162             : }
     163             : 
     164             : 
     165             : 
     166             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13