2008年11月6日
#
摘要: 這部分的作用是客戶端可以存放任意格式的私有xml數據到服務器上,并且在想要查看的時候取回!1.協議 - XEP-0049 - http://xmpp.org/extensions/xep-0049.html2. jabber client可以保存任意的xml數據到服務器,通過一個<iq type="set">的請求.該請求需要包含一個名稱空間為jabber:iq:priva...
閱讀全文
摘要: #jabber協議中如何注冊一個用戶?首先要與服務器建立一個連接, 在完成TLS握手之后就可以進行注冊了,為什么不需要SASL握手呢?因為SASL握手只針對已經注冊的用戶在登陸服務器的時候使用.(修改密碼和刪除用戶的時候需要SASL握手)下面以openfire作為服務器,注冊一個用戶的過程如下:(假設已經完成了TLS握手)1. ( C->S ) <stream:stream  ...
閱讀全文
2008年11月3日
#
摘要: 1. websiteSSL(secure Socket Layer)TLS(transport Layer Security) - SSL3.0基礎之上提出的安全通信標準,目前版本是1.0openssl 主頁 -> http://www.openssl.org/openssl 中文文檔 -> http://www.chinaunix.net/jh/13/478901.html
2. 如何編譯O...
閱讀全文
2008年10月29日
#
2008年10月22日
#
摘要: 1. 使用OpenSSL內置的數據結構BIO可以方便的創建安全和非安全鏈接,在IBM Web上的"使用OpenSSL進行安全編程"系列的三篇文章是個不錯的入門:http://www.ibm.com/developerworks/cn/linux/l-openssl.htmlhttp://www.ibm.com/developerworks/cn/linux/l-openssl2.htmlhttp:...
閱讀全文
2008年10月18日
#
2008年10月17日
#
主要分析gloox的connect模塊,涉及的文件:
connectionbase.h
connectiondatahander.h
connectionhandler.h
connectiontcpbase.h (connectiontcpbase.cpp)
connectiontcpclient.h (connectiontcpclient.cpp)
connectiontcpserver.h (connectiontcpserver.cpp)
1. ConnectionBase抽象連接類,表示一個socket連接。3個數據成員:
server和ip - 需要連接到的目標地址和端口(在ConnectionTcpServer是要綁定的本地地址)
connectionState - 連接的當前狀態 (一個三種狀態disconnected | connecting | connected)
connectionDataHandler - 處理三類ConnectionBase中的三類事件 ( 數據接收 | 連接建立 | 連接斷開 )
5個主要方法:
connect - 建立一個socket連接
disconnect - 斷開一個socket連接
send - 發送數據,該方法直到要發送的數據全部發送完畢才返回
recv( int timeout ) - 接收數據,接收到數據或者timeout返回
receive - 把connection設置成"接收模式",也就是循環的調用recv接收數據,直到連接斷開該方法返回,
該方法保證這個連接可以循環的接收數據.
注意: recv和receive的區別。
2. ConnectionDataHandler處理繼承自ConnectionBase的對象的事件handlerReceivedData - 接收到數據
handlerConnect - 一個原始的套接字連接建立
handlerDisconnect - 一個原始的套接字連接斷開
3. ConnectionTcpBase一個基本的TCP連接,繼承自ConnectionBase數據成員
bool cancel - 表示連接是否應該斷開,默認為true. (true表示連接斷開)
函數disconnect也就是僅僅設置cancel為true.
在函數receive循環接收數據的邏輯中會檢測cancel,如果為true,則表示連接斷開,則返回,退出"接收模式"
int bufSize - buf的大小,默認是1024
char* buf - 緩沖區,大小事bufSize + 1 (在ConnectionTcpBase中并沒有用到這個緩沖區,供上層使用E.g. ConnectionTcpClient的recv來接收數據)
實現的主要方法:
send - 發送數據
receive - "循環"接收數據
dataAvailable( int timeout ) - 一個私有方法,作用是測試socket上目前是否有數據可讀,如果有返回true. 可以供recv調用. 避免了阻塞的調用原始套接字方法::receive或者::accept.
4. ConnectionTcpClient實現一個基本的TCP連接,繼承自ConnectionTcpBase實現的主要方法
connect - 連接到目標地址,如果成功,則調用handler->handlerConnect,失敗則調用handler->handlerDisconnect
recv - 接收數據,如果接收到數據,馬上調用handler->handlerReceiveData,接收發生錯誤,表示連接斷開,調用handler->handlerDisconnect
5. ConnectionHandler一個抽象類,用來接收一個客戶端連接的請求,該類用于ConnectionTcpServer
只有一個接口:
handleIncomingConnection( ConnectionBase *connect ) - &connect表示新建立的連接請求,哪原始的socket做類比相當于accept返回的socket.
注意: 區別于ConnectionDataHandler用于處理一個特定連接的中的事件(數據到來,連接斷開,連接建立),ConnectionListener用于XMPP Stream.
6. ConnectionTcpServer,實質上就是一個偵聽socket,同原始的socket做類比就是一個調用了bind/listen之后處于偵聽狀態的socket.主要數據成員:
connectionHandler - 接收新到來的連接,接收到之后調用handlerIncomingConnection
主要方法:
connect - 建立套接字,綁定到本地地址,偵聽bind/listen
recv - 調用accept接收一個socket連接,如果成功,則用accpet返回的socket創建一個ConnectionTCPClient,調用handlerIncomingConnection來處理新進的連接.
總結:
ConnectionTcpServer和ConnectionTcpClient的區別:
connect (client) - 連接到服務器,使connection處于可以接收數據的狀態
connect(server) - 綁定到本地地址,偵聽,使connection處于可以接收新連接的狀態(accept)
recv(client) - 接收數據,調用connectionDataHandler->handlerReceivedData
recv(server) - 接收新連接,調用connectionHandler->handlerIncomingConnection
receive(Client) - 接收數據狀態,循環接收數據,調用disconnect時退出
receive(Server) - 接收連接狀態,循環接收連接,調用disconnect時退出.
所以ConnectionTcpClient有發送數據和循環接收數據的能力,ConnectionTcpServer有偵聽客戶端連接的能力。當有特定的事件發生的時候,會自動觸發相關的handler來進行處理,如數據到來,連接建立\斷開,新連接到來等等。
問題:
這種設計模式使receive處于一個無限的while循環,不斷的調用select+accpet或者select+receive,是否利于send之類的方法在同一個socket上發送數據?
2008年10月16日
#
1. Enviroment
"gloox - 0.9.9.5" + "Windows Server 2008" + "VC++ 2008"
2. config.h.win
HAVE_WINTLS
HAVE_WINDNS_H
HAVE_SETSOCKOPT
默認編譯的時候出錯:
dns.obj : error LNK2019: unresolved external symbol
_DnsFree@8 referenced in function "public: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,int> > > __cdecl gloox::DNS::resolve(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class gloox::LogSink const &)" (
?resolve@DNS@gloox@@SA?AV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@std@@@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@00ABVLogSink@2@@Z)
dns.obj : error LNK2019: unresolved external symbol
_DnsQuery_A@24 referenced in function "public: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,int> > > __cdecl gloox::DNS::resolve(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class gloox::LogSink const &)" (
?resolve@DNS@gloox@@SA?AV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@std@@@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@00ABVLogSink@2@@Z)
tlsschannel.obj : error LNK2019: unresolved external symbol
_EncryptMessage@16 referenced in function "public: virtual bool __thiscall gloox::SChannel::encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (
?encrypt@SChannel@gloox@@UAE_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
tlsschannel.obj : error LNK2019: unresolved external symbol
_DecryptMessage@16 referenced in function "public: virtual int __thiscall gloox::SChannel::decrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (
?decrypt@SChannel@gloox@@UAEHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__FreeCredentialsHandle@4 referenced in function "public: virtual void __thiscall gloox::SChannel::cleanup(void)" (
?cleanup@SChannel@gloox@@UAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__DeleteSecurityContext@4 referenced in function "public: virtual void __thiscall gloox::SChannel::cleanup(void)" (
?cleanup@SChannel@gloox@@UAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
_FreeContextBuffer@4 referenced in function "public: virtual bool __thiscall gloox::SChannel::handshake(void)" (
?handshake@SChannel@gloox@@UAE_NXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
_InitializeSecurityContextA@48 referenced in function "public: virtual bool __thiscall gloox::SChannel::handshake(void)" (
?handshake@SChannel@gloox@@UAE_NXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
_AcquireCredentialsHandleA@36 referenced in function "public: virtual bool __thiscall gloox::SChannel::handshake(void)" (
?handshake@SChannel@gloox@@UAE_NXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
_QueryContextAttributesA@12 referenced in function "private: void __thiscall gloox::SChannel::setSizes(void)" (
?setSizes@SChannel@gloox@@AAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__CertFreeCertificateChain@4 referenced in function "private: void __thiscall gloox::SChannel::validateCert(void)" (
?validateCert@SChannel@gloox@@AAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__CertVerifyCertificateChainPolicy@16 referenced in function "private: void __thiscall gloox::SChannel::validateCert(void)" (
?validateCert@SChannel@gloox@@AAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__CertGetCertificateChain@32 referenced in function "private: void __thiscall gloox::SChannel::validateCert(void)" (
?validateCert@SChannel@gloox@@AAEXXZ)
tlsschannel.obj : error LNK2019: unresolved external symbol
__imp__CertNameToStrA@20 referenced in function "private: void __thiscall gloox::SChannel::certData(void)" (
?certData@SChannel@gloox@@AAEXXZ)
3. 加入需要的lib, 編譯成功
ws2_32.lib Dnsapi.lib Secur32.lib Crypt32.lib