libmsn 4.2
/home/salem/libmsn/tags/libmsn-4.2/msn/p2p.h
00001 #ifndef __msn_p2p_h__
00002 #define __msn_p2p_h__
00003 /*
00004  * p2p.h
00005  * libmsn
00006  *
00007  * Crated by Tiago Salem Herrmann on 08/2007.
00008  * Copyright (c) 2007 Tiago Salem Herrmann. All rights reserved
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 
00026 #include <msn/connection.h>
00027 #include <msn/authdata.h>
00028 #include <msn/errorcodes.h>
00029 #include <msn/buddy.h>
00030 #include <msn/passport.h>
00031 #include <msn/util.h>
00032 #include <stdexcept>
00033 
00034 #include <vector>
00035 #include <fstream>
00036 #include <iostream>
00037 
00038 #include "libmsn_export.h"
00039 
00040 namespace MSN
00041 {
00042     class SwitchboardServerConnection;
00043     class FileTransferConnectionP2P;
00044 
00047     class LIBMSN_EXPORT P2P
00048     {
00049         public:
00050             P2P();
00051             virtual ~P2P();
00052             unsigned int rand_helper;
00053             enum {
00054                 DIRECTION_SENDING = 0,
00055                 DIRECTION_RECEIVING = 1
00056             };
00057             enum {
00058                 STEP_INVITATION_SENT,
00059                 STEP_ACK_INVITATION_SENT,
00060                 STEP_200OK_SENT,
00061                 STEP_200OK_ACK_SENT,
00062                 STEP_603DECLINE_SENT,
00063                 STEP_603DECLINE_ACK_SENT,
00064                 STEP_DC_INVITE_SENT, // direct connection
00065                 STEP_DC_INVITE_ACK_SENT, // direct connection
00066                 STEP_DC_200OK_SENT,
00067                 STEP_DC_200OKACK_SENT,
00068                 STEP_DATA_PREPARATION_SENT,
00069                 STEP_DATA_PREPARATION_ACK,
00070                 STEP_SENDING,
00071                 STEP_RECEIVING,
00072                 STEP_RECEIVING_FINISHED,
00073                 STEP_DATA_TRANSFER_ACK,
00074                 STEP_BYE_SENT,
00075                 STEP_BYE_ACK
00076             };
00077             typedef enum {
00078                 APP_NONE = 0,
00079                 APP_WEBCAM = 4,
00080                 APP_FILE_TRANSFER = 2,
00081                 APP_DISPLAY_PICTURE = 1,
00082                 APP_EMOTICON = 11,
00083                 APP_DISPLAY_PICTURE2 = 12, // MSNP15 uses 12 instead 1
00084                 APP_VOICE_CLIP = 20, // MSNP15 uses 12 instead 1
00085                 APP_WINK = 98, // non standard
00086                 APP_INK = 99 // non standard
00087             } p2pTransferObj;
00088             enum {
00089                 FLAG_NOP = 0x0,
00090                 FLAG_ACK = 0x2,
00091                 FLAG_ERROR = 0x8,
00092                 FLAG_DATA_EMOTICONS = 0x20,
00093                 FLAG_DATA_PICTURE = 0x20,
00094                 FLAG_FILE_DATA = 0x01000030,
00095                 FLAG_FILE_DATA2 = 0x01000020
00096             };
00097             
00098             struct p2pPacket {
00099                 struct {
00100                     unsigned int sessionID;
00101                     unsigned int identifier;
00102                     unsigned long long dataOffset;
00103                     unsigned long long totalDataSize;
00104                     unsigned int messageLength;
00105                     unsigned int flag;
00106                     unsigned int ackID;
00107                     unsigned int ackUID;
00108                     unsigned long long ackDataSize;
00109                 }p2pHeader;
00110                 std::string body;
00111                 struct {
00112                     unsigned int appID;
00113                 }p2pFooter;
00114 
00115                 p2pPacket() {
00116                     p2pHeader.sessionID = 0;
00117                     p2pHeader.identifier = 0;
00118                     p2pHeader.dataOffset = 0;
00119                     p2pHeader.totalDataSize = 0;
00120                     p2pHeader.messageLength = 0;
00121                     p2pHeader.flag = 0;
00122                     p2pHeader.ackID = 0;
00123                     p2pHeader.ackUID = 0;
00124                     p2pHeader.ackDataSize = 0;
00125                     p2pFooter.appID = 0;
00126                 }
00127             };
00128 
00129             struct p2pSession {
00130                 bool sending; // sending or receiving, if sending, so true
00131                 unsigned long long totalDataSize;
00132                 unsigned int step; // step at the moment
00133                 unsigned int currentIdentifier;
00134                 unsigned int baseIdentifier; // baseIdentifier
00135                 unsigned int CSeq;
00136                 unsigned int sessionID;
00137                 unsigned int appID;
00138                 MSN::FileTransferConnectionP2P *fileTransfer;
00139                 std::string from;
00140                 std::string to;
00141                 std::string CallID;
00142                 std::string Via;
00143                 std::string ContentType;
00144                 std::string Context; // can be the file preview or msnobject
00145                 std::string filename; // filename to transfer
00146                 std::ifstream *out_stream; // file to send
00147                 std::ofstream *in_stream; // file to receive
00148                 std::string ConnType;
00149                 std::string Bridges;
00150                 std::string NetID;
00151                 std::string UPnPNat;
00152                 std::string Listening;
00153                 std::string ICF;
00154                 std::string IPv4InternalAddrs;
00155                 std::string IPv4InternalPort;
00156                 std::string IPv4ExternalAddrs;
00157                 std::string IPv4ExternalPort;
00158                 p2pTransferObj typeTransfer;
00159                 std::string emoticonAlias;
00160 
00161                 p2pPacket tempPacket; // this is used for general purposes
00162                 std::string ink;
00163 
00164                 p2pSession() {
00165                     sending = false;
00166                     totalDataSize = 0;
00167                     step = 0;
00168                     currentIdentifier = 0;
00169                     baseIdentifier = 0;
00170                     CSeq = 0;
00171                     sessionID = 0;
00172                     appID = 0;
00173                     fileTransfer = 0;
00174                     out_stream = 0;
00175                     in_stream = 0;
00176                     typeTransfer = APP_NONE;
00177                 }
00178             };
00179 
00180             typedef void (P2P::*P2PCallbacks)(MSN::SwitchboardServerConnection &conn, 
00181                     unsigned int sessionID, 
00182                     p2pPacket &packet);
00183 
00184             std::map<unsigned int, std::pair<P2PCallbacks, unsigned int> > callbacks;
00185 
00186             virtual void addCallback(P2PCallbacks, unsigned int sessionID, 
00187                     unsigned int ackID);
00188 
00189             virtual void removeCallback(unsigned int ackID);
00190 
00191             std::map<unsigned int, p2pPacket> pendingP2PMsg;
00192             std::map<unsigned int, p2pSession> startedSessions;
00193 
00194             void sendFile(MSN::SwitchboardServerConnection &conn, 
00195                           MSN::fileTransferInvite ft);
00196 
00197             void handleP2Pmessage(MSN::SwitchboardServerConnection &conn, 
00198                           std::vector<std::string> & args, 
00199                           std::string mime, std::string body);
00200 
00201             void sendACK(MSN::SwitchboardServerConnection &conn, 
00202                       p2pPacket &packet,
00203                       p2pSession &session);
00204 
00205             void sendP2PPacket(MSN::SwitchboardServerConnection &conn, 
00206                     p2pPacket &packet, 
00207                     p2pSession &session);
00208 
00209             void sendP2PData(MSN::SwitchboardServerConnection &conn, 
00210                     p2pSession &session,
00211                     p2pPacket &packet);
00212 
00213             void receiveP2PData(MSN::SwitchboardServerConnection &conn, 
00214                     p2pPacket &packet);
00215 
00216             void handle_negotiation(MSN::SwitchboardServerConnection &conn, 
00217                     p2pPacket &packet);
00218 
00219             void handle_INVITE(MSN::SwitchboardServerConnection &conn, 
00220                     p2pPacket &packet);
00221 
00222             void handle_603Decline(MSN::SwitchboardServerConnection &conn, 
00223                     p2pPacket &packet);
00224 
00225             void handle_INVITE_ACK(MSN::SwitchboardServerConnection &conn, 
00226                     unsigned int sessionID, 
00227                     p2pPacket &packet);
00228 
00229             void handle_200OK(MSN::SwitchboardServerConnection &conn, 
00230                     p2pPacket &packet);
00231 
00232             void handle_BYE(MSN::SwitchboardServerConnection &conn, 
00233                     p2pPacket &packet);
00234 
00235             void send_200OK(MSN::SwitchboardServerConnection &conn, 
00236                     p2pSession &session,
00237                     std::string body);
00238 
00239             void send_BYE(MSN::SwitchboardServerConnection &conn, 
00240                     p2pPacket &packet,
00241                     p2pSession &session);
00242 
00243             void send_603Decline(MSN::SwitchboardServerConnection &conn, 
00244                     p2pSession &session);
00245 
00246             void handle_p2pACK(MSN::SwitchboardServerConnection &conn, 
00247                     p2pPacket &packet);
00248 
00249             void handle_200OKACK(MSN::SwitchboardServerConnection &conn, 
00250                     unsigned int sessionID, 
00251                     p2pPacket &packet);
00252 
00253             void handle_603DeclineACK(MSN::SwitchboardServerConnection &conn, 
00254                     unsigned int sessionID, 
00255                     p2pPacket &packet);
00256 
00257             void handle_DataPreparationACK(MSN::SwitchboardServerConnection &conn, 
00258                     unsigned int sessionID, 
00259                     p2pPacket &packet);
00260 
00261             void handle_DataACK(MSN::SwitchboardServerConnection &conn, 
00262                     unsigned int sessionID, 
00263                     p2pPacket &packet);
00264 
00265             void handle_BYEACK(MSN::SwitchboardServerConnection &conn, 
00266                     unsigned int sessionID, 
00267                     p2pPacket &packet);
00268 
00269             void handle_MSGACKReceived(MSN::SwitchboardServerConnection &conn,
00270                     unsigned int sessionID,
00271                     std::string fromPassport);
00272 
00273             void handle_fileTransferResponse(MSN::SwitchboardServerConnection &conn,
00274                     unsigned int sessionID,
00275                     std::string filename,
00276                     bool response);
00277 
00278             void handle_session_changes(MSN::SwitchboardServerConnection &conn, 
00279                     p2pPacket &packet, 
00280                     p2pSession &session);
00281 
00282             void requestFile(MSN::SwitchboardServerConnection &conn, 
00283                     unsigned int sessionID, 
00284                     std::string filename, 
00285                     std::string msnobject,
00286                     p2pTransferObj obj);
00287 
00288             void requestDisplayPicture(MSN::SwitchboardServerConnection &conn, 
00289                     unsigned int sessionID, 
00290                     std::string filename, 
00291                     std::string msnobject);
00292 
00293             void sendInk(MSN::SwitchboardServerConnection &conn, 
00294                     std::string image);
00295 
00296             void cancelTransfer(MSN::SwitchboardServerConnection &conn, 
00297                     unsigned int sessionID);
00298 
00299             void requestEmoticon(MSN::SwitchboardServerConnection &conn, 
00300                     unsigned int sessionID, 
00301                     std::string filename, 
00302                     std::string msnobject,
00303                     std::string alias);
00304 
00305             void requestVoiceClip(MSN::SwitchboardServerConnection &conn, 
00306                     unsigned int sessionID, 
00307                     std::string filename, 
00308                     std::string msnobject);
00309 
00310             void requestWink(MSN::SwitchboardServerConnection &conn, 
00311                     unsigned int sessionID, 
00312                     std::string filename, 
00313                     std::string msnobject);
00314 
00315             void requestInk(MSN::SwitchboardServerConnection &conn, 
00316                     unsigned int sessionID, 
00317                     std::string filename, 
00318                     std::string msnobject);
00319     };
00320 }
00321 
00322 #endif
 All Classes Namespaces Functions Variables Enumerations Enumerator