Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 143 - (show annotations) (download) (as text)
Wed Aug 4 06:45:49 2021 UTC (2 years, 9 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3314 byte(s)


1 #ifndef H_WINSOCK /* -*- mode: c++ -*- */
2 /*
3 * Copyright (C) 2009-2013 TSUBAKIMOTO Hiroya <z0rac@users.sourceforge.jp>
4 *
5 * This software comes with ABSOLUTELY NO WARRANTY; for details of
6 * the license terms, see the LICENSE.txt file included with the program.
7 */
8 #define H_WINSOCK
9
10 #include <exception>
11 #include <string>
12 #include <winsock2.h>
13 #include <wininet.h>
14 #include <ws2tcpip.h>
15 #define SECURITY_WIN32
16 #include <security.h>
17 #include <schannel.h>
18
19 using namespace std;
20
21 // winsock - winsock handler
22 class winsock {
23 typedef int (WSAAPI *_get_t)(const char*, const char*,
24 const struct addrinfo*, struct addrinfo**);
25 typedef void (WSAAPI *_free_t)(struct addrinfo*);
26 static _get_t _get;
27 static _free_t _free;
28 public:
29 winsock();
30 ~winsock() { WSACleanup(); }
31 static struct addrinfo* getaddrinfo(const string& host, const string& port,
32 int domain = AF_UNSPEC);
33 static void freeaddrinfo(struct addrinfo* info) { _free(info); }
34 static string idn(const string& host);
35 static string idn(LPCWSTR host);
36 public:
37 // tcpclient - TCP client socket
38 class tcpclient {
39 SOCKET _socket;
40 tcpclient(const tcpclient&); void operator=(const tcpclient&); // disable to copy
41 public:
42 tcpclient(SOCKET socket = INVALID_SOCKET) : _socket(socket) {}
43 ~tcpclient() { shutdown(); }
44 tcpclient& operator()(SOCKET s) { _socket = s; return *this; }
45 SOCKET release() { SOCKET s = _socket; _socket = INVALID_SOCKET; return s; }
46 operator SOCKET() const { return _socket; }
47 tcpclient& connect(const string& host, const string& port, int domain = AF_UNSPEC);
48 tcpclient& shutdown();
49 size_t recv(char* buf, size_t size);
50 size_t send(const char* data, size_t size);
51 tcpclient& timeout(int sec);
52 bool wait(int op, int sec = -1);
53 };
54
55 // tlsclient - transport layer security
56 class tlsclient {
57 CredHandle _cred;
58 CtxtHandle _ctx;
59 SecPkgContext_StreamSizes _sizes;
60 bool _avail;
61 string _recvq;
62 string::size_type _rest;
63 string _extra;
64 struct buf {
65 char* data;
66 buf() : data(NULL) {}
67 ~buf() { delete [] data; }
68 char* operator()(size_t n)
69 { delete [] data, data = NULL; return data = new char[n]; }
70 } _buf;
71 static string _emsg(SECURITY_STATUS ss);
72 SECURITY_STATUS _ok(SECURITY_STATUS ss) const;
73 SECURITY_STATUS _token(SecBufferDesc* inb = NULL);
74 void _sendtoken(const char* data, size_t size);
75 size_t _copyextra(size_t i, size_t size);
76 public:
77 tlsclient(DWORD proto = 0);
78 virtual ~tlsclient();
79 bool avail() const { return _avail; }
80 tlsclient& connect();
81 tlsclient& shutdown();
82 bool verify(const string& cn, DWORD ignore = 0);
83 size_t recv(char* buf, size_t size);
84 size_t send(const char* data, size_t size);
85 protected:
86 virtual size_t _recv(char* buf, size_t size) = 0;
87 virtual size_t _send(const char* data, size_t size) = 0;
88 };
89
90 // error - exception type
91 class error : public exception {
92 string _msg;
93 public:
94 error() : _msg(emsg()) {}
95 error(const string& msg) : _msg(msg) {}
96 ~error() throw() {}
97 const char* what() const throw() { return _msg.c_str(); }
98 static string emsg();
99 };
100 };
101
102 #endif

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26