File: socket.h

package info (click to toggle)
botan1.10 1.10.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,288 kB
  • sloc: cpp: 61,853; python: 1,687; asm: 1,247; ansic: 252; perl: 89; sh: 52; makefile: 38; lisp: 34
file content (257 lines) | stat: -rw-r--r-- 5,864 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Unix Socket
* (C) 2004-2010 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#ifndef SOCKET_WRAPPER_H__
#define SOCKET_WRAPPER_H__

#include <stdexcept>

#if defined(_MSC_VER)
  #define SOCKET_IS_WINSOCK 1
#endif

#if !defined(SOCKET_IS_WINSOCK)
  #define SOCKET_IS_WINSOCK 0
#endif

#if SOCKET_IS_WINSOCK
  #include <winsock.h>

  typedef SOCKET socket_t;
  const socket_t invalid_socket = INVALID_SOCKET;
  #define socket_error_code WSAGetLastError()
  typedef int ssize_t;

  class SocketInitializer
     {
     public:
        SocketInitializer()
           {
           WSADATA wsadata;
           WSAStartup(MAKEWORD(2, 2), &wsadata);
           }

        ~SocketInitializer()
           {
           WSACleanup();
           }
     };
#else
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <sys/time.h>
  #include <netinet/in.h>
  #include <netdb.h>
  #include <unistd.h>
  #include <errno.h>

  typedef int socket_t;
  const socket_t invalid_socket = -1;
  #define socket_error_code errno
  #define closesocket close

  class SocketInitializer {};
#endif

#if !defined(MSG_NOSIGNAL)
  #define MSG_NOSIGNAL 0
#endif

#include <string.h>

class Socket
   {
   public:
      size_t read(unsigned char[], size_t);
      void write(const unsigned char[], size_t);

      std::string peer_id() const { return peer; }

      void close()
         {
         if(sockfd != invalid_socket)
            {
            if(::closesocket(sockfd) != 0)
               throw std::runtime_error("Socket::close failed");
            sockfd = invalid_socket;
            }
         }

      Socket(socket_t fd, const std::string& peer_id = "") :
         peer(peer_id), sockfd(fd)
         {
         }

      Socket(const std::string&, unsigned short);
      ~Socket() { close(); }
   private:
      std::string peer;
      socket_t sockfd;
   };

class Server_Socket
   {
   public:
      /**
      * Accept a new connection
      */
      Socket* accept()
         {
         socket_t retval = ::accept(sockfd, 0, 0);
         if(retval == invalid_socket)
            throw std::runtime_error("Server_Socket: accept failed");
         return new Socket(retval);
         }

      void close()
         {
         if(sockfd != invalid_socket)
            {
            if(::closesocket(sockfd) != 0)
               throw std::runtime_error("Server_Socket::close failed");
            sockfd = invalid_socket;
            }
         }

      Server_Socket(unsigned short);
      ~Server_Socket() { close(); }
   private:
      socket_t sockfd;
   };

/**
* Unix Socket Constructor
*/
Socket::Socket(const std::string& host, unsigned short port) : peer(host)
   {
   sockfd = invalid_socket;

   hostent* host_addr = ::gethostbyname(host.c_str());

   if(host_addr == 0)
      throw std::runtime_error("Socket: gethostbyname failed for " + host);
   if(host_addr->h_addrtype != AF_INET) // FIXME
      throw std::runtime_error("Socket: " + host + " has IPv6 address");

   socket_t fd = ::socket(PF_INET, SOCK_STREAM, 0);
   if(fd == invalid_socket)
      throw std::runtime_error("Socket: Unable to acquire socket");

   sockaddr_in socket_info;
   ::memset(&socket_info, 0, sizeof(socket_info));
   socket_info.sin_family = AF_INET;
   socket_info.sin_port = htons(port);

   ::memcpy(&socket_info.sin_addr,
            host_addr->h_addr,
            host_addr->h_length);

   socket_info.sin_addr = *(struct in_addr*)host_addr->h_addr; // FIXME

   if(::connect(fd, (sockaddr*)&socket_info, sizeof(struct sockaddr)) != 0)
      {
      ::closesocket(fd);
      throw std::runtime_error("Socket: connect failed");
      }

   sockfd = fd;
   }

/**
* Read from a Unix socket
*/
size_t Socket::read(unsigned char buf[], size_t length)
   {
   if(sockfd == invalid_socket)
      throw std::runtime_error("Socket::read: Socket not connected");

   size_t got = 0;

   while(length)
      {
      ssize_t this_time = ::recv(sockfd, (char*)buf + got,
                                 length, MSG_NOSIGNAL);

      if(this_time == 0)
         break;

      if(this_time == -1)
         {
         if(socket_error_code == EINTR)
            this_time = 0;
         else
            throw std::runtime_error("Socket::read: Socket read failed");
         }

      got += this_time;
      length -= this_time;
      }
   return got;
   }

/**
* Write to a Unix socket
*/
void Socket::write(const unsigned char buf[], size_t length)
   {
   if(sockfd == invalid_socket)
      throw std::runtime_error("Socket::write: Socket not connected");

   size_t offset = 0;
   while(length)
      {
      ssize_t sent = ::send(sockfd, (const char*)buf + offset,
                            length, MSG_NOSIGNAL);

      if(sent == -1)
         {
         if(socket_error_code == EINTR)
            sent = 0;
         else
            throw std::runtime_error("Socket::write: Socket write failed");
         }

      offset += sent;
      length -= sent;
      }
   }

/**
* Unix Server Socket Constructor
*/
Server_Socket::Server_Socket(unsigned short port)
   {
   sockfd = invalid_socket;

   socket_t fd = ::socket(PF_INET, SOCK_STREAM, 0);
   if(fd == invalid_socket)
      throw std::runtime_error("Server_Socket: Unable to acquire socket");

   sockaddr_in socket_info;
   ::memset(&socket_info, 0, sizeof(socket_info));
   socket_info.sin_family = AF_INET;
   socket_info.sin_port = htons(port);

   // FIXME: support limiting listeners
   socket_info.sin_addr.s_addr = INADDR_ANY;

   if(::bind(fd, (sockaddr*)&socket_info, sizeof(struct sockaddr)) != 0)
      {
      ::closesocket(fd);
      throw std::runtime_error("Server_Socket: bind failed");
      }

   if(::listen(fd, 100) != 0) // FIXME: totally arbitrary
      {
      ::closesocket(fd);
      throw std::runtime_error("Server_Socket: listen failed");
      }

   sockfd = fd;
   }

#endif