Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 67 - (show annotations) (download) (as text)
Tue Jan 26 19:26:42 2010 UTC (14 years, 2 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3254 byte(s)


1 #ifndef H_WINSOCK /* -*- mode: c++ -*- */
2 /*
3 * Copyright (C) 2009-2010 TSUBAKIMOTO Hiroya <zorac@4000do.co.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 public:
35 // tcpclient - TCP client socket
36 class tcpclient {
37 SOCKET _socket;
38 tcpclient(const tcpclient&); void operator=(const tcpclient&); // disable to copy
39 public:
40 tcpclient(SOCKET socket = INVALID_SOCKET) : _socket(socket) {}
41 ~tcpclient() { shutdown(); }
42 tcpclient& operator()(SOCKET s) { _socket = s; return *this; }
43 SOCKET release() { SOCKET s = _socket; _socket = INVALID_SOCKET; return s; }
44 operator SOCKET() const { return _socket; }
45 tcpclient& connect(const string& host, const string& port, int domain = AF_UNSPEC);
46 tcpclient& shutdown();
47 size_t recv(char* buf, size_t size);
48 size_t send(const char* data, size_t size);
49 tcpclient& timeout(int sec);
50 bool wait(int op, int sec = -1);
51 };
52
53 // tlsclient - transport layer security
54 class tlsclient {
55 CredHandle _cred;
56 CtxtHandle _ctx;
57 SecPkgContext_StreamSizes _sizes;
58 bool _avail;
59 string _recvq;
60 string::size_type _rest;
61 string _extra;
62 struct buf {
63 char* data;
64 buf() : data(NULL) {}
65 ~buf() { delete [] data; }
66 char* operator()(size_t n)
67 { delete [] data, data = NULL; return data = new char[n]; }
68 } _buf;
69 static string _emsg(SECURITY_STATUS ss);
70 SECURITY_STATUS _ok(SECURITY_STATUS ss) const;
71 SECURITY_STATUS _token(SecBufferDesc* inb = NULL);
72 void _sendtoken(const char* data, size_t size);
73 size_t _copyextra(size_t i, size_t size);
74 public:
75 tlsclient(DWORD proto = SP_PROT_SSL3 | SP_PROT_TLS1);
76 virtual ~tlsclient();
77 bool avail() const { return _avail; }
78 tlsclient& connect();
79 tlsclient& shutdown();
80 bool verify(const string& cn, DWORD ignore = 0);
81 size_t recv(char* buf, size_t size);
82 size_t send(const char* data, size_t size);
83 protected:
84 virtual size_t _recv(char* buf, size_t size) = 0;
85 virtual size_t _send(const char* data, size_t size) = 0;
86 };
87
88 // error - exception type
89 class error : public exception {
90 string _msg;
91 public:
92 error() : _msg(emsg()) {}
93 error(const string& msg) : _msg(msg) {}
94 ~error() throw() {}
95 const char* what() const throw() { return _msg.c_str(); }
96 static string emsg();
97 };
98 };
99
100 #endif

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