支持SSL的封装类--CAsyncSocketEx
2007-09-07 15:45
531 查看
Introduction
CAsyncSocketExis an MFC-less replacement for
CAsyncSocketwhich does also offer a flexible layer code. This class was written because
CAsyncSocketis not the fastest WinSock wrapper and it's very hard to add new functionality to
CAsyncSocketderived classes. This class supports most of the features of
CAsyncSocketand can be extended quite easily. With the layer system, you can implement transparent layers like proxy or SSL layers easily.
One benefit is, that
CAsyncSocketExworks without MFC, but if MFC is enabled (
_AFXis defined),
CAsyncSocketExoffers some functions taking a
CStringas argument, so that
CAsyncSocketExis compatible with
CAsyncSocket.
CAsyncProxySocketLayeris a layer class for
CAsyncSocketEx. With it's help, you can connect through proxy servers. This layer is almost transparent, so you won't have to modify much code to use it. Other layers can be written quite easily as well, just derive new layer classes from
CAsyncSocketExLayer.
Another layer class is CAsyncSslSocketLayer, which can be used to establish SSL connections to servers.
Using the code
To useCAsyncSocketEx, just replace all occurrences of
CAsyncSocketin your code with
CAsyncSocketEx. If you did not enhance
CAsyncSocketyourself in any way, you won't have to change anything else in your code. However, problems can occur if you're using
IOCtlor
SetSockOptin your code,
CAsyncSocketExand the layer classes do not recognize if you change any options using
IOCtlor
SetSockOpt.
CAsyncSocketExprovides some useful new functions:
// Triggers an event on the socket // Any combination of FD_READ, FD_WRITE, FD_CLOSE, //FD_ACCEPT, FD_CONNECT and FD_FORCEREAD is valid for lEvent. BOOL TriggerEvent(long lEvent);
With
TriggerEventyou can explicitly trigger the notification messages handled in
OnReceive,
OnSend, ...
TriggerEvent(FD_SEND);for example posts a message to the internal helper window and returns. Later, if this message is processed,
OnSendwill be called.
There's something special about
FD_READ:
OnRecievewill only be called if there is really any data available. To trigger a call to
OnReceiveregardless of data waiting or not, use
FD_FORCEREADinstead.
The other new functions are for the new layer system.
Using the layer system
Using the layer system is really simple. In most cases, it is sufficient to create a new instance of a layer class and attach it to aCAsyncSocketExinstance.
//Attaches a new layer to the socket. BOOL AddLayer(CAsyncSocketExLayer *pLayer);
This adds a new layer derived from
CAsyncSocketExLayerto the socket. You can add more than one layer. The layer last added to the layer list, works directly on the socket and the other layers operate on the previous one. All calls you make to
CAsyncSocketExare first handled by the layer first added to the list. Example: If you want to establish an SSL connection to a server over a proxy server, first add the SSL layer (see the CAsyncSslSocketLayer article), then add the proxy layer.
//Resets layer chain. void RemoveAllLayers()
This function detached all layers from the socket. But it does not destroy the layer class instances, you are still responsible for destroying the layer instances. In theory, it would be possible to add a layer previously detached from a socket to a new socket, but this is not recommended. You should create and attach a new instance of the layer for each established connection.
//Called by the layers to notify application of some events virtual int OnLayerCallback(const CAsyncSocketExLayer *pLayer, int nType, int nParam1, int nParam2);
This function is called by the layers to notify the program about more or less important events of the layer. The first parameter identifies the layer which did send the notification message. The second parameter is the type of the notification. Valid notification types are:
LAYERCALLBACK_STATECHANGE
This notification is sent if the state of the layer has changed.
nParam1contains the new state,
nParam2contains the old state. Valid states are:
0 - Not a valid socket
1 - Unconnected
2 - Connecting
3 - Listening
4 - Connected
5 - Closed
6 - Aborted
LAYERCALLBACK_LAYERSPECIFIC
This notification is sent on events specific to a each layer. See the layer's description for the meaning and the parameters of this notification message.
CAsyncProxySocketLayer
With this layer you can connect through proxy servers. This class supports SOCKS4/5 as well as HTTP/1.1 (using CONNECT method) proxies. For more information about SOCKS4/5 go to http://www.socks.nec.com/socksprot.html, for more information about HTTP 1.1 go to http://www.rfc-editor.org/ and search for RFC2616.You don't have to change much, in your already existing code, to use
CAsyncProxySocketLayer. To use it, create an instance of
CAsyncProxySocketLayer, call
SetProxyand attach it to a
CAsyncSocketExinstance. You have to process
OnLayerCallbackin your
CAsyncSocketExinstance as it will receive all layer notifications.
The following notifications are sent (of type
LAYERCALLBACK_LAYERSPECIFIC):
Error codes:
PROXYERROR_NOERROR 0 | |
PROXYERROR_NOCONN 1 | Can't connect to proxy server, use GetLastErrorfor more information |
PROXYERROR_REQUESTFAILED 2 | Request failed, can't send data |
PROXYERROR_AUTHREQUIRED 3 | Authentication required |
PROXYERROR_AUTHTYPEUNKNOWN 4 | Authtype unknown or not supported |
PROXYERROR_AUTHFAILED 5 | Authentication failed |
PROXYERROR_AUTHNOLOGON 6 | |
PROXYERROR_CANTRESOLVEHOST 7 |
PROXYSTATUS_LISTENSOCKETCREATED 8 | Called when a listen socket was created successfully. Unlike the normal listen function, a socksified socket has to connect to the proxy to negotiate the details with the server on which the listen socket will be created. The two parameters will contain the IP and port of the listen socket on the server. |
CAsyncProxySocketLayerto create a listen socket, you have to use this overloaded function:
BOOL PrepareListen(unsigned long serverIp);
serverIPis the IP of the server you are already connected through the SOCKS proxy. You can't use listen sockets over a SOCKS proxy without a primary connection. Listen sockets are only supported by SOCKS proxies, this won't work with HTTP proxies.
When the listen socket is created successfully, the
PROXYSTATUS_LISTENSOCKETCREATEDnotification is sent. The parameters will tell you the IP and the port of the listen socket. After it, you have to handle the
OnAcceptmessage and accept the connection.
Be careful when calling
Accept:
rConnectedsocket will NOT be filled! Instead use the instance which created the listen socket, it will handle the data connection.
If you want to accept more than one connection, you have to create a listing socket for each of them!
Description of important functions and their parameters:
void SetProxy(int nProxyType); void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort); void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort, CString ProxyUser, CString ProxyPass);
Call one of this functions to set the proxy type.
Parameters:
nProxyTypespecifies the Proxy Type.
ProxyHostand
nProxyPortspecify the address of the proxy
ProxyUserand
ProxyPassare only available for SOCKS5 proxies.
Supported proxy types:
PROXYTYPE_NOPROXY
PROXYTYPE_SOCKS4
PROXYTYPE_SOCKS4A
PROXYTYPE_SOCKS5
PROXYTYPE_HTTP11
There are also some other functions:
int GetProxyType();
Returns the used proxy.
const int GetLastProxyError() const;
Returns the last proxy error, see above for a list of available error codes.
相关文章推荐
- 支持SSL的封装类--CAsyncSslSocketLayer
- 为 PHP 的 socket 启用 SSL 支持
- javamail发邮件封装(支持SSL)
- 新版python logging 封装,支持同时向console,file,socket输出
- 090828项目进展:学习emule源码4,CAsyncSocketEx
- 封装良好的java mail工具,支持附件,群发,抄送,认证,无认证,ssl,html格式
- 基于boost asio实现的支持ssl的通用socket框架
- Android Socket 封装,支持 TCP/UDP 客户端和服务端,支持自定义粘包处理、验证处理、解析处理。
- 构造HttpClient三部曲之一:支持代理的Socket封装
- 基于boost asio实现的支持ssl的通用socket框架
- eMule中的 CAsyncSocketEx 类
- 基于boost asio实现的支持ssl的通用socket框架
- CentOS 7 下sendEmail发邮件失败,提示invalid SSL_version specified at /usr/share/perl5/vendor_perl/IO/Socket/SSL.pm line 415.
- 如何在Ubuntu 14.04 上为Apache 2.4 安装SSL支持
- Java网络编程-用SSL构建安全的Socket
- CAS Server部署(支持Tomcat的SSL)
- win2003 AD域 支持 LDAP SSL
- Nginx server之Nginx添加ssl支持
- VC++ 编译libcurl 支持SSL,GZIP(有脚本)
- SSL握手通信详解及linux下c/c++ SSL Socket代码举例