2008年11月6日
#
摘要: 這部分的作用是客戶端可以存放任意格式的私有xml數(shù)據(jù)到服務(wù)器上,并且在想要查看的時(shí)候取回!1.協(xié)議 - XEP-0049 - http://xmpp.org/extensions/xep-0049.html2. jabber client可以保存任意的xml數(shù)據(jù)到服務(wù)器,通過(guò)一個(gè)<iq type="set">的請(qǐng)求.該請(qǐng)求需要包含一個(gè)名稱(chēng)空間為jabber:iq:priva...
閱讀全文
摘要: #jabber協(xié)議中如何注冊(cè)一個(gè)用戶?首先要與服務(wù)器建立一個(gè)連接, 在完成TLS握手之后就可以進(jìn)行注冊(cè)了,為什么不需要SASL握手呢?因?yàn)镾ASL握手只針對(duì)已經(jīng)注冊(cè)的用戶在登陸服務(wù)器的時(shí)候使用.(修改密碼和刪除用戶的時(shí)候需要SASL握手)下面以openfire作為服務(wù)器,注冊(cè)一個(gè)用戶的過(guò)程如下:(假設(shè)已經(jīng)完成了TLS握手)1. ( C->S ) <stream:stream  ...
閱讀全文
2008年11月3日
#
摘要: 1. websiteSSL(secure Socket Layer)TLS(transport Layer Security) - SSL3.0基礎(chǔ)之上提出的安全通信標(biāo)準(zhǔn),目前版本是1.0openssl 主頁(yè) -> http://www.openssl.org/openssl 中文文檔 -> http://www.chinaunix.net/jh/13/478901.html
2. 如何編譯O...
閱讀全文
2008年10月29日
#
2008年10月22日
#
摘要: 1. 使用OpenSSL內(nèi)置的數(shù)據(jù)結(jié)構(gòu)BIO可以方便的創(chuàng)建安全和非安全鏈接,在IBM Web上的"使用OpenSSL進(jìn)行安全編程"系列的三篇文章是個(gè)不錯(cuò)的入門(mén):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抽象連接類(lèi),表示一個(gè)socket連接。3個(gè)數(shù)據(jù)成員:
server和ip - 需要連接到的目標(biāo)地址和端口(在ConnectionTcpServer是要綁定的本地地址)
connectionState - 連接的當(dāng)前狀態(tài) (一個(gè)三種狀態(tài)disconnected | connecting | connected)
connectionDataHandler - 處理三類(lèi)ConnectionBase中的三類(lèi)事件 ( 數(shù)據(jù)接收 | 連接建立 | 連接斷開(kāi) )
5個(gè)主要方法:
connect - 建立一個(gè)socket連接
disconnect - 斷開(kāi)一個(gè)socket連接
send - 發(fā)送數(shù)據(jù),該方法直到要發(fā)送的數(shù)據(jù)全部發(fā)送完畢才返回
recv( int timeout ) - 接收數(shù)據(jù),接收到數(shù)據(jù)或者timeout返回
receive - 把connection設(shè)置成"接收模式",也就是循環(huán)的調(diào)用recv接收數(shù)據(jù),直到連接斷開(kāi)該方法返回,
該方法保證這個(gè)連接可以循環(huán)的接收數(shù)據(jù).
注意: recv和receive的區(qū)別。
2. ConnectionDataHandler處理繼承自ConnectionBase的對(duì)象的事件handlerReceivedData - 接收到數(shù)據(jù)
handlerConnect - 一個(gè)原始的套接字連接建立
handlerDisconnect - 一個(gè)原始的套接字連接斷開(kāi)
3. ConnectionTcpBase一個(gè)基本的TCP連接,繼承自ConnectionBase數(shù)據(jù)成員
bool cancel - 表示連接是否應(yīng)該斷開(kāi),默認(rèn)為true. (true表示連接斷開(kāi))
函數(shù)disconnect也就是僅僅設(shè)置cancel為true.
在函數(shù)receive循環(huán)接收數(shù)據(jù)的邏輯中會(huì)檢測(cè)cancel,如果為true,則表示連接斷開(kāi),則返回,退出"接收模式"
int bufSize - buf的大小,默認(rèn)是1024
char* buf - 緩沖區(qū),大小事bufSize + 1 (在ConnectionTcpBase中并沒(méi)有用到這個(gè)緩沖區(qū),供上層使用E.g. ConnectionTcpClient的recv來(lái)接收數(shù)據(jù))
實(shí)現(xiàn)的主要方法:
send - 發(fā)送數(shù)據(jù)
receive - "循環(huán)"接收數(shù)據(jù)
dataAvailable( int timeout ) - 一個(gè)私有方法,作用是測(cè)試socket上目前是否有數(shù)據(jù)可讀,如果有返回true. 可以供recv調(diào)用. 避免了阻塞的調(diào)用原始套接字方法::receive或者::accept.
4. ConnectionTcpClient實(shí)現(xiàn)一個(gè)基本的TCP連接,繼承自ConnectionTcpBase實(shí)現(xiàn)的主要方法
connect - 連接到目標(biāo)地址,如果成功,則調(diào)用handler->handlerConnect,失敗則調(diào)用handler->handlerDisconnect
recv - 接收數(shù)據(jù),如果接收到數(shù)據(jù),馬上調(diào)用handler->handlerReceiveData,接收發(fā)生錯(cuò)誤,表示連接斷開(kāi),調(diào)用handler->handlerDisconnect
5. ConnectionHandler一個(gè)抽象類(lèi),用來(lái)接收一個(gè)客戶端連接的請(qǐng)求,該類(lèi)用于ConnectionTcpServer
只有一個(gè)接口:
handleIncomingConnection( ConnectionBase *connect ) - &connect表示新建立的連接請(qǐng)求,哪原始的socket做類(lèi)比相當(dāng)于accept返回的socket.
注意: 區(qū)別于ConnectionDataHandler用于處理一個(gè)特定連接的中的事件(數(shù)據(jù)到來(lái),連接斷開(kāi),連接建立),ConnectionListener用于XMPP Stream.
6. ConnectionTcpServer,實(shí)質(zhì)上就是一個(gè)偵聽(tīng)socket,同原始的socket做類(lèi)比就是一個(gè)調(diào)用了bind/listen之后處于偵聽(tīng)狀態(tài)的socket.主要數(shù)據(jù)成員:
connectionHandler - 接收新到來(lái)的連接,接收到之后調(diào)用handlerIncomingConnection
主要方法:
connect - 建立套接字,綁定到本地地址,偵聽(tīng)bind/listen
recv - 調(diào)用accept接收一個(gè)socket連接,如果成功,則用accpet返回的socket創(chuàng)建一個(gè)ConnectionTCPClient,調(diào)用handlerIncomingConnection來(lái)處理新進(jìn)的連接.
總結(jié):
ConnectionTcpServer和ConnectionTcpClient的區(qū)別:
connect (client) - 連接到服務(wù)器,使connection處于可以接收數(shù)據(jù)的狀態(tài)
connect(server) - 綁定到本地地址,偵聽(tīng),使connection處于可以接收新連接的狀態(tài)(accept)
recv(client) - 接收數(shù)據(jù),調(diào)用connectionDataHandler->handlerReceivedData
recv(server) - 接收新連接,調(diào)用connectionHandler->handlerIncomingConnection
receive(Client) - 接收數(shù)據(jù)狀態(tài),循環(huán)接收數(shù)據(jù),調(diào)用disconnect時(shí)退出
receive(Server) - 接收連接狀態(tài),循環(huán)接收連接,調(diào)用disconnect時(shí)退出.
所以ConnectionTcpClient有發(fā)送數(shù)據(jù)和循環(huán)接收數(shù)據(jù)的能力,ConnectionTcpServer有偵聽(tīng)客戶端連接的能力。當(dāng)有特定的事件發(fā)生的時(shí)候,會(huì)自動(dòng)觸發(fā)相關(guān)的handler來(lái)進(jìn)行處理,如數(shù)據(jù)到來(lái),連接建立\斷開(kāi),新連接到來(lái)等等。
問(wèn)題:
這種設(shè)計(jì)模式使receive處于一個(gè)無(wú)限的while循環(huán),不斷的調(diào)用select+accpet或者select+receive,是否利于send之類(lèi)的方法在同一個(gè)socket上發(fā)送數(shù)據(jù)?
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
默認(rèn)編譯的時(shí)候出錯(cuò):
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