ddc
string_socket.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 //
3 // DDC originally by Alexey Sokirko
4 // Changes and modifications 2011-2019 by Bryan Jurish
5 //
6 // This file is part of DDC.
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef _string_socket_h
22 #define _string_socket_h
23 
24 #include "utilit.h"
25 #include "ddcLog.h"
26 
27 #include <pthread.h>
28 #include <arpa/inet.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <netdb.h>
34 #include <stdio.h>
35 #include <fcntl.h>
36 #define SOCKET int
37 
38 //-- debugging
39 //#define DDC_DEBUG_FDLEAK 1
40 #undef DDC_DEBUG_FDLEAK
41 
42 //======================================================================
43 // Types
44 
46  neSuccess = 0,
48  neTimeout = 2,
57 };
58 
61  stUnknown = 0,
62  stINET = 1,
63  stUNIX = 2
64 };
65 
66 #define SOCKET int
67 #ifdef WIN32
68 struct sockaddr_in;
69 #endif
70 
71 //======================================================================
72 // Constants
73 
74 // default socket chunk size (for recv() loops)
75 extern const int ddcSocketChunkSize;
76 
77 //======================================================================
78 // Constants
79 
82 {
84  string m_Addr;
85  int m_Port;
86 
88  CSocketAddr(SocketTypeEnum sockType=stUnknown, const string& sockAddr="", int sockPort=-1)
89  : m_Type(sockType), m_Addr(sockAddr), m_Port(sockPort)
90  {};
91 
94  : m_Type(X.m_Type), m_Addr(X.m_Addr), m_Port(X.m_Port)
95  {};
96 
98  CSocketAddr(const string& Str, bool parsePort=true)
99  : m_Type(stUnknown), m_Addr(), m_Port(-1)
100  { ParseSocketAddr(Str, parsePort); };
101 
104  {
105  m_Type = X.m_Type;
106  m_Addr = X.m_Addr;
107  m_Port = X.m_Port;
108  return *this;
109  }
110 
112  void Clear();
113 
115  bool IsValid() const;
116 
118  inline operator bool() const
119  { return IsValid(); };
120 
130  void ParseSocketAddr(const string& Str, bool parsePort=true);
131 
136  void ParseAddrPort();
137 
139  string toString() const;
140 };
141 
142 
143 //======================================================================
144 // Utilities
145 
146 /* moo / ddc-2.0.0: packet length arguments sent over sockets are ALWAYS 32-bit unsigned integers in little-endian order
147  * + big-endian ("network") order or fixed-width ASCII decimal would make more sense, but would break too much existing client code
148  */
149 extern uint32_t size_to_lsb(uint32_t sz);
150 extern uint32_t lsb_to_size(uint32_t sz_lsb);
151 
152 extern SOCKET create_socket (const CSocketAddr& Addr, bool bBind, mode_t bindPerms, string& strError); //-- generic
153 extern SOCKET create_inet_socket (const char* HostAddr, int Port, bool bBind, string& strError);
154 extern SOCKET create_unix_socket(const string& socket_path, bool bBind, mode_t bindPerms, string& strError);
155 extern SOCKET create_socket(const char* HostAddr, int Port, bool bBind, string& strError); //-- compatibility wrapper for create_inet_socket()
156 
157 extern void flush_socket (SOCKET sock);
158 extern bool SocketInitialize(bool bReadFromLocalFile);
159 extern bool SocketDeinitialize();
160 
162 extern bool CloseSocket(int l_socket, bool linger=false, int timeout=5);
163 
165 extern bool CloseSocketRef(SOCKET& rConnectedSocket, bool linger=false, int timeout=5);
166 
167 extern string GetNetworkErrorString(const NetworkErrorsEnum& t);
168 
169 //-- generic socket protocol utilities (pure-functions, used by ddc_daemon via CHost and ddc_search)
170 extern void SocketPeer(SOCKET sock, string& PeerAddr, int& PeerPort);
171 extern string SocketPeer(SOCKET sock);
172 extern bool SendString (int client_fd, const char* Str, uint32_t StrLen, string& ErrorStr);
173 extern bool SendString (int client_fd, const std::string &str, string& ErrorStr);
174 extern NetworkErrorsEnum receive_buffer(int client_fd, char* Buffer,int Length, int& have_read, int TimeOut);
175 extern NetworkErrorsEnum ReceiveString (int client_fd, string& Result, int TimeOut, uint32_t maxPacketLength=0);
176 
177 
178 #endif
179 
180 /*--- emacs style variables ---
181  * Local Variables:
182  * mode: C++
183  * c-file-style: "ellemtel"
184  * c-basic-offset: 4
185  * tab-width: 8
186  * indent-tabs-mode: nil
187  * End:
188  */
uint32_t lsb_to_size(uint32_t sz_lsb)
Definition: string_socket.cpp:181
void SocketPeer(int sock, string &PeerAddr, int &PeerPort)
Definition: string_socket.cpp:209
NetworkErrorsEnum receive_buffer(int client_fd, char *Buffer, int Length, int &have_read, int TimeOut)
Definition: string_socket.cpp:285
#define SOCKET
Definition: string_socket.h:66
NetworkErrorsEnum
Definition: string_socket.h:45
string GetNetworkErrorString(const NetworkErrorsEnum &t)
Definition: string_socket.cpp:540
Definition: string_socket.h:53
Definition: string_socket.h:63
Definition: string_socket.h:49
Definition: string_socket.h:46
Definition: string_socket.h:81
bool CloseSocketRef(int &rConnectedSocket, bool linger=false, int timeout=5)
Definition: string_socket.cpp:156
uint32_t size_to_lsb(uint32_t sz)
Definition: string_socket.cpp:170
void flush_socket(int sock)
Definition: string_socket.cpp:513
void ParseAddrPort()
Definition: string_socket.cpp:111
Definition: string_socket.h:52
CSocketAddr & operator=(const CSocketAddr &X)
Definition: string_socket.h:103
bool SocketDeinitialize()
Definition: string_socket.cpp:534
Definition: string_socket.h:55
Definition: string_socket.h:51
Definition: string_socket.h:50
NetworkErrorsEnum ReceiveString(int client_fd, string &Result, int TimeOut, uint32_t maxPacketLength=0)
Definition: string_socket.cpp:312
CSocketAddr(const string &Str, bool parsePort=true)
Definition: string_socket.h:98
SocketTypeEnum m_Type
socket domain
Definition: string_socket.h:83
string toString() const
Definition: string_socket.cpp:123
Definition: string_socket.h:47
CSocketAddr(SocketTypeEnum sockType=stUnknown, const string &sockAddr="", int sockPort=-1)
Definition: string_socket.h:88
bool SendString(int client_fd, const char *Str, uint32_t StrLen, string &ErrorStr)
Definition: string_socket.cpp:261
bool IsValid() const
Definition: string_socket.cpp:54
int create_unix_socket(const string &socket_path, bool bBind, mode_t bindPerms, string &strError)
Definition: string_socket.cpp:372
void Clear()
Definition: string_socket.cpp:46
bool SocketInitialize(bool bReadFromLocalFile)
Definition: string_socket.cpp:527
Definition: string_socket.h:54
Definition: string_socket.h:48
int create_socket(const CSocketAddr &Addr, bool bBind, mode_t bindPerms, string &strError)
Definition: string_socket.cpp:358
bool CloseSocket(int l_socket, bool linger=false, int timeout=5)
Definition: string_socket.cpp:139
const int ddcSocketChunkSize
Definition: string_socket.cpp:30
void ParseSocketAddr(const string &Str, bool parsePort=true)
Definition: string_socket.cpp:67
Definition: string_socket.h:62
Definition: string_socket.h:61
Definition: string_socket.h:56
int m_Port
unix:filler, tcp:port number
Definition: string_socket.h:85
CSocketAddr(const CSocketAddr &X)
Definition: string_socket.h:93
string m_Addr
unix:filesystem path, tcp:host address
Definition: string_socket.h:84
int create_inet_socket(const char *HostAddr, int Port, bool bBind, string &strError)
Definition: string_socket.cpp:431
SocketTypeEnum
enum for socket schemes
Definition: string_socket.h:60