Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 51 - (show annotations) (download) (as text)
Tue May 19 20:39:06 2009 UTC (14 years, 10 months ago) by z0rac
File MIME type: text/x-chdr
File size: 2976 byte(s)
Brush up and fix a shutdown bug using OpenSSL.
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 void connect(const string& host, const string& port, bool blocking = true);
44 void shutdown();
45 size_t recv(char* buf, size_t size);
46 size_t send(const char* data, size_t size);
47 bool wait(int op, int sec = -1);
48 };
49
50 // tlsclient - transport layer security
51 class tlsclient {
52 CredHandle _cred;
53 CtxtHandle _ctx;
54 SecPkgContext_StreamSizes _sizes;
55 bool _avail;
56 string _recvq;
57 string::size_type _rest;
58 string _extra;
59 struct buf {
60 char* data;
61 buf() : data(NULL) {}
62 ~buf() { delete [] data; }
63 char* operator()(size_t n)
64 { delete [] data, data = NULL; return data = new char[n]; }
65 } _buf;
66 static string _emsg(SECURITY_STATUS ss);
67 SECURITY_STATUS _ok(SECURITY_STATUS ss) const;
68 SECURITY_STATUS _token(SecBufferDesc* inb = NULL);
69 size_t _copyextra(size_t i, size_t size);
70 public:
71 tlsclient(DWORD proto = SP_PROT_SSL3 | SP_PROT_TLS1);
72 virtual ~tlsclient();
73 void connect();
74 void shutdown();
75 size_t recv(char* buf, size_t size);
76 size_t send(const char* data, size_t size);
77 protected:
78 virtual size_t _recv(char* buf, size_t size) = 0;
79 virtual size_t _send(const char* data, size_t size) = 0;
80 };
81
82 // error - exception type
83 class error : public exception {
84 string _msg;
85 public:
86 error() : _msg(emsg()) {}
87 error(const string& msg) : _msg(msg) {}
88 ~error() throw() {}
89 const char* what() const throw() { return _msg.c_str(); }
90 static string emsg();
91 };
92 };
93
94 #endif

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