Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (show annotations) (download) (as text)
Thu May 21 16:19:23 2009 UTC (14 years, 10 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3096 byte(s)
1.0RC
1 #ifndef H_WINSOCK /* -*- mode: c++ -*- */
2 /*
3 * Copyright (C) 2009 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 static void freeaddrinfo(struct addrinfo* info) { _free(info); }
32 public:
33 // tcp - TCP client socket
34 class tcpclient {
35 SOCKET _socket;
36 tcpclient(const tcpclient&); void operator=(const tcpclient&); // disable to copy
37 public:
38 tcpclient(SOCKET socket = INVALID_SOCKET) : _socket(socket) {}
39 ~tcpclient() { shutdown(); }
40 tcpclient& operator()(SOCKET s) { _socket = s; return *this; }
41 SOCKET release() { SOCKET s = _socket; _socket = INVALID_SOCKET; return s; }
42 operator SOCKET() const { return _socket; }
43 tcpclient& connect(const string& host, const string& port);
44 tcpclient& shutdown();
45 size_t recv(char* buf, size_t size);
46 size_t send(const char* data, size_t size);
47 tcpclient& timeout(int sec);
48 bool wait(int op, int sec = -1);
49 };
50
51 // tlsclient - transport layer security
52 class tlsclient {
53 CredHandle _cred;
54 CtxtHandle _ctx;
55 SecPkgContext_StreamSizes _sizes;
56 bool _avail;
57 string _recvq;
58 string::size_type _rest;
59 string _extra;
60 struct buf {
61 char* data;
62 buf() : data(NULL) {}
63 ~buf() { delete [] data; }
64 char* operator()(size_t n)
65 { delete [] data, data = NULL; return data = new char[n]; }
66 } _buf;
67 static string _emsg(SECURITY_STATUS ss);
68 SECURITY_STATUS _ok(SECURITY_STATUS ss) const;
69 SECURITY_STATUS _token(SecBufferDesc* inb = NULL);
70 void _sendtoken(const char* data, size_t size);
71 size_t _copyextra(size_t i, size_t size);
72 public:
73 tlsclient(DWORD proto = SP_PROT_SSL3 | SP_PROT_TLS1);
74 virtual ~tlsclient();
75 bool avail() const { return _avail; }
76 void connect();
77 void shutdown();
78 size_t recv(char* buf, size_t size);
79 size_t send(const char* data, size_t size);
80 protected:
81 virtual size_t _recv(char* buf, size_t size) = 0;
82 virtual size_t _send(const char* data, size_t size) = 0;
83 };
84
85 // error - exception type
86 class error : public exception {
87 string _msg;
88 public:
89 error() : _msg(emsg()) {}
90 error(const string& msg) : _msg(msg) {}
91 ~error() throw() {}
92 const char* what() const throw() { return _msg.c_str(); }
93 static string emsg();
94 };
95 };
96
97 #endif

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