Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/winsock.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations) (download) (as text)
Wed May 20 19:42:13 2009 UTC (14 years, 10 months ago) by z0rac
File MIME type: text/x-chdr
File size: 3000 byte(s)


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 size_t _copyextra(size_t i, size_t size);
71 public:
72 tlsclient(DWORD proto = SP_PROT_SSL3 | SP_PROT_TLS1);
73 virtual ~tlsclient();
74 void connect();
75 void shutdown();
76 size_t recv(char* buf, size_t size);
77 size_t send(const char* data, size_t size);
78 protected:
79 virtual size_t _recv(char* buf, size_t size) = 0;
80 virtual size_t _send(const char* data, size_t size) = 0;
81 };
82
83 // error - exception type
84 class error : public exception {
85 string _msg;
86 public:
87 error() : _msg(emsg()) {}
88 error(const string& msg) : _msg(msg) {}
89 ~error() throw() {}
90 const char* what() const throw() { return _msg.c_str(); }
91 static string emsg();
92 };
93 };
94
95 #endif

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