Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 141 - (hide annotations) (download) (as text)
Wed Aug 4 05:58:45 2021 UTC (2 years, 8 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3324 byte(s)
Support TLSv1.2 later.
1 z0rac 50 #ifndef H_WINSOCK /* -*- mode: c++ -*- */
2     /*
3 z0rac 117 * Copyright (C) 2009-2013 TSUBAKIMOTO Hiroya <z0rac@users.sourceforge.jp>
4 z0rac 50 *
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 z0rac 67 #include <wininet.h>
14 z0rac 50 #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 z0rac 55 static struct addrinfo* getaddrinfo(const string& host, const string& port,
32 z0rac 59 int domain = AF_UNSPEC);
33 z0rac 50 static void freeaddrinfo(struct addrinfo* info) { _free(info); }
34 z0rac 117 static string idn(const string& host);
35     static string idn(LPCWSTR host);
36 z0rac 50 public:
37 z0rac 67 // tcpclient - TCP client socket
38 z0rac 51 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 z0rac 59 tcpclient& connect(const string& host, const string& port, int domain = AF_UNSPEC);
48 z0rac 52 tcpclient& shutdown();
49 z0rac 51 size_t recv(char* buf, size_t size);
50     size_t send(const char* data, size_t size);
51 z0rac 52 tcpclient& timeout(int sec);
52 z0rac 51 bool wait(int op, int sec = -1);
53     };
54    
55     // tlsclient - transport layer security
56     class tlsclient {
57 z0rac 50 CredHandle _cred;
58     CtxtHandle _ctx;
59     SecPkgContext_StreamSizes _sizes;
60     bool _avail;
61 z0rac 51 string _recvq;
62 z0rac 50 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 z0rac 53 void _sendtoken(const char* data, size_t size);
75 z0rac 50 size_t _copyextra(size_t i, size_t size);
76     public:
77 z0rac 141 tlsclient(DWORD proto = SP_PROT_ALL);
78 z0rac 51 virtual ~tlsclient();
79 z0rac 53 bool avail() const { return _avail; }
80 z0rac 66 tlsclient& connect();
81     tlsclient& shutdown();
82 z0rac 67 bool verify(const string& cn, DWORD ignore = 0);
83 z0rac 51 size_t recv(char* buf, size_t size);
84     size_t send(const char* data, size_t size);
85 z0rac 50 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