Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 61 - (show annotations) (download) (as text)
Tue Jan 19 19:52:29 2010 UTC (14 years, 2 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3160 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 <ws2tcpip.h>
14 #define SECURITY_WIN32
15 #include <security.h>
16 #include <schannel.h>
17
18 using namespace std;
19
20 // winsock - winsock handler
21 class winsock {
22 typedef int (WSAAPI *_get_t)(const char*, const char*,
23 const struct addrinfo*, struct addrinfo**);
24 typedef void (WSAAPI *_free_t)(struct addrinfo*);
25 static _get_t _get;
26 static _free_t _free;
27 public:
28 winsock();
29 ~winsock() { WSACleanup(); }
30 static struct addrinfo* getaddrinfo(const string& host, const string& port,
31 int domain = AF_UNSPEC);
32 static void freeaddrinfo(struct addrinfo* info) { _free(info); }
33 public:
34 // tcp - TCP client socket
35 class tcpclient {
36 SOCKET _socket;
37 tcpclient(const tcpclient&); void operator=(const tcpclient&); // disable to copy
38 public:
39 tcpclient(SOCKET socket = INVALID_SOCKET) : _socket(socket) {}
40 ~tcpclient() { shutdown(); }
41 tcpclient& operator()(SOCKET s) { _socket = s; return *this; }
42 SOCKET release() { SOCKET s = _socket; _socket = INVALID_SOCKET; return s; }
43 operator SOCKET() const { return _socket; }
44 tcpclient& connect(const string& host, const string& port, int domain = AF_UNSPEC);
45 tcpclient& shutdown();
46 size_t recv(char* buf, size_t size);
47 size_t send(const char* data, size_t size);
48 tcpclient& timeout(int sec);
49 bool wait(int op, int sec = -1);
50 };
51
52 // tlsclient - transport layer security
53 class tlsclient {
54 CredHandle _cred;
55 CtxtHandle _ctx;
56 SecPkgContext_StreamSizes _sizes;
57 bool _avail;
58 string _recvq;
59 string::size_type _rest;
60 string _extra;
61 struct buf {
62 char* data;
63 buf() : data(NULL) {}
64 ~buf() { delete [] data; }
65 char* operator()(size_t n)
66 { delete [] data, data = NULL; return data = new char[n]; }
67 } _buf;
68 static string _emsg(SECURITY_STATUS ss);
69 SECURITY_STATUS _ok(SECURITY_STATUS ss) const;
70 SECURITY_STATUS _token(SecBufferDesc* inb = NULL);
71 void _sendtoken(const char* data, size_t size);
72 size_t _copyextra(size_t i, size_t size);
73 public:
74 tlsclient(DWORD proto = SP_PROT_SSL3 | SP_PROT_TLS1);
75 virtual ~tlsclient();
76 bool avail() const { return _avail; }
77 void connect();
78 void shutdown();
79 size_t recv(char* buf, size_t size);
80 size_t send(const char* data, size_t size);
81 protected:
82 virtual size_t _recv(char* buf, size_t size) = 0;
83 virtual size_t _send(const char* data, size_t size) = 0;
84 };
85
86 // error - exception type
87 class error : public exception {
88 string _msg;
89 public:
90 error() : _msg(emsg()) {}
91 error(const string& msg) : _msg(msg) {}
92 ~error() throw() {}
93 const char* what() const throw() { return _msg.c_str(); }
94 static string emsg();
95 };
96 };
97
98 #endif

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