00001 /* 00002 * 00003 * 00004 * Inter Asterisk Exchange 2 00005 * 00006 * A class to describe the node we are talking to. 00007 * 00008 * Open Phone Abstraction Library (OPAL) 00009 * 00010 * Copyright (c) 2005 Indranet Technologies Ltd. 00011 * 00012 * The contents of this file are subject to the Mozilla Public License 00013 * Version 1.0 (the "License"); you may not use this file except in 00014 * compliance with the License. You may obtain a copy of the License at 00015 * http://www.mozilla.org/MPL/ 00016 * 00017 * Software distributed under the License is distributed on an "AS IS" 00018 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00019 * the License for the specific language governing rights and limitations 00020 * under the License. 00021 * 00022 * The Original Code is Open Phone Abstraction Library. 00023 * 00024 * The Initial Developer of the Original Code is Indranet Technologies Ltd. 00025 * 00026 * The author of this code is Derek J Smithies 00027 * 00028 * $Log: remote.h,v $ 00029 * Revision 1.8 2007/04/19 06:17:21 csoutheren 00030 * Fixes for precompiled headers with gcc 00031 * 00032 * Revision 1.7 2007/01/23 02:08:25 dereksmithies 00033 * Handle Vnak frames correctly. handle iseqno and oseqno correctly. 00034 * 00035 * Revision 1.6 2006/08/09 03:46:40 dereksmithies 00036 * Add ability to register to a remote Asterisk box. The iaxProcessor class is split 00037 * into a callProcessor and a regProcessor class. 00038 * Big thanks to Stephen Cook, (sitiveni@gmail.com) for this work. 00039 * 00040 * Revision 1.5 2005/08/28 23:51:29 dereksmithies 00041 * Improve test for massaging timestamps in fullframes. Thanks to Adrian Sietsma 00042 * 00043 * Revision 1.4 2005/08/26 03:07:38 dereksmithies 00044 * Change naming convention, so all class names contain the string "IAX2" 00045 * 00046 * Revision 1.3 2005/08/24 13:06:18 rjongbloed 00047 * Added configuration define for AEC encryption 00048 * 00049 * Revision 1.2 2005/08/24 01:38:38 dereksmithies 00050 * Add encryption, iax2 style. Numerous tidy ups. Use the label iax2, not iax 00051 * 00052 * Revision 1.1 2005/07/30 07:01:32 csoutheren 00053 * Added implementation of IAX2 (Inter Asterisk Exchange 2) protocol 00054 * Thanks to Derek Smithies of Indranet Technologies Ltd. for 00055 * writing and contributing this code 00056 * 00057 * 00058 * 00059 * 00060 */ 00061 00062 #ifndef REMOTE_H 00063 #define REMOTE_H 00064 00065 #ifndef _PTLIB_H 00066 #include <ptlib.h> 00067 #endif 00068 00069 #include <ptlib/sockets.h> 00070 00071 #if P_SSL_AES 00072 #include <openssl/aes.h> 00073 #endif 00074 00075 #ifdef P_USE_PRAGMA 00076 #pragma interface 00077 #endif 00078 00079 class IAX2FullFrame; 00080 00081 00084 class IAX2Remote : public PObject 00085 { 00086 PCLASSINFO(IAX2Remote, PObject); 00087 00088 public: 00089 00091 IAX2Remote(); 00092 00093 virtual ~IAX2Remote() { }; 00094 00097 PINDEX DestCallNumber() { return destCallNumber; } 00098 00102 PINDEX SourceCallNumber() { return sourceCallNumber; } 00103 00105 virtual void PrintOn(ostream & strm) const; 00106 00108 enum { 00109 callNumberUndefined = 0xffff 00110 }; 00111 00113 PIPSocket::Address RemoteAddress() { return remoteAddress; } 00114 00116 PINDEX RemotePort() { return remotePort; } 00117 00119 void Assign(IAX2Remote &); 00120 00122 void SetRemoteAddress(PIPSocket::Address &newVal) { remoteAddress = newVal; } 00123 00125 void SetRemoteAddress(int newVal) { remoteAddress = newVal; } 00126 00128 void SetRemotePort (PINDEX newVal) { remotePort = newVal; } 00129 00131 void SetSourceCallNumber(PINDEX newVal) { sourceCallNumber = newVal; } 00132 00134 void SetDestCallNumber(PINDEX newVal) { destCallNumber = newVal; } 00135 00139 BOOL operator == (IAX2Remote & other); 00140 00143 BOOL operator *= (IAX2Remote & other); 00144 00145 00147 BOOL operator != (IAX2Remote & other); 00148 00149 00150 protected: 00152 PINDEX sourceCallNumber; 00153 00155 PINDEX destCallNumber; 00156 00158 PIPSocket::Address remoteAddress; 00159 00161 PINDEX remotePort; 00162 00163 }; 00164 00166 00175 class IAX2FrameIdValue : public PObject 00176 { 00177 PCLASSINFO(IAX2FrameIdValue, PObject); 00178 public: 00180 IAX2FrameIdValue (PINDEX timeStamp, PINDEX seqVal); 00181 00183 IAX2FrameIdValue (PINDEX val); 00184 00186 PINDEX GetTimeStamp() const; 00187 00189 PINDEX GetSequenceVal() const; 00190 00194 PINDEX GetPlainSequence() const; 00195 00197 virtual void PrintOn(ostream & strm) const; 00198 00201 virtual Comparison Compare(const PObject & obj) const; 00202 00203 protected: 00204 00207 PUInt64 value; 00208 }; 00209 00211 00216 PDECLARE_SORTED_LIST(IAX2PacketIdList, IAX2FrameIdValue) 00217 #ifdef DOC_PLUS_PLUS 00218 class IAX2PacketIdList : public PSortedList 00219 { 00220 #endif 00221 00224 BOOL Contains(IAX2FrameIdValue &src); 00225 00228 PINDEX GetFirstValue(); 00229 00231 void AppendNewFrame(IAX2FullFrame &src); 00232 00234 virtual void PrintOn(ostream & strm) const; 00235 00236 protected: 00242 void RemoveOldContiguousValues(); 00243 }; 00244 00246 00247 class IAX2SequenceNumbers 00248 { 00249 public: 00253 enum IncomingOrder { 00254 InSequence, 00255 SkippedFrame, 00256 RepeatedFrame 00257 }; 00258 00259 00260 00262 IAX2SequenceNumbers() 00263 { ZeroAllValues(); }; 00264 00266 virtual ~IAX2SequenceNumbers() { } 00267 00269 void ZeroAllValues(); 00270 00272 PINDEX InSeqNo(); 00273 00275 PINDEX OutSeqNo(); 00276 00279 BOOL IsSequenceNosZero(); 00280 00282 void SetInSeqNo(PINDEX newVal); 00283 00285 void SetOutSeqNo(PINDEX newVal); 00286 00289 void SetInOutSeqNo(PINDEX inVal, PINDEX outVal); 00290 00293 void SetAckSequenceInfo(IAX2SequenceNumbers & other); 00294 00296 BOOL operator !=(IAX2SequenceNumbers &other); 00297 00299 BOOL operator ==(IAX2SequenceNumbers &other); 00300 00302 void MassageSequenceForSending(IAX2FullFrame &src /*<!src will be transmitted to the remote node */ 00303 ); 00304 00308 void WrapAroundFrameSequence(IAX2SequenceNumbers & src); 00309 00313 IncomingOrder IncomingMessageInOrder 00314 (IAX2FullFrame &src /*<!frame to be compared with current data base.*/ ); 00315 00317 void CopyContents(IAX2SequenceNumbers &src); 00318 00320 PString AsString() const; 00321 00323 virtual void PrintOn(ostream & strm) const; 00324 00327 BOOL IsFirstReply() { return (inSeqNo == 1) && (outSeqNo == 0); } 00328 00330 void AddWrapAroundValue(PINDEX newOffset); 00331 00332 00333 protected: 00334 00336 enum sequenceDefines { 00337 minSpacing = 3 00338 }; 00339 00341 PINDEX inSeqNo; 00342 00344 PINDEX outSeqNo; 00345 00347 PMutex mutex; 00348 00350 PINDEX lastSentTimeStamp; 00351 00353 IAX2PacketIdList receivedLog; 00354 }; 00355 00357 00358 class IAX2Encryption : public PObject 00359 { 00360 PCLASSINFO(IAX2Encryption, PObject); 00361 public: 00363 IAX2Encryption(); 00364 00366 void SetEncryptionOn (BOOL newState = TRUE); 00367 00369 void SetEncryptionKey(PString & newKey); 00370 00372 void SetChallengeKey(PString & newKey); 00373 00375 const PString & EncryptionKey() const; 00376 00378 const PString & ChallengeKey() const; 00379 00381 const BOOL IsEncrypted() const; 00382 00383 #if P_SSL_AES 00384 00385 AES_KEY *AesEncryptKey(); 00386 00388 AES_KEY *AesDecryptKey(); 00389 #endif 00390 00391 protected: 00394 void CalculateAesKeys(); 00395 00397 PString encryptionKey; 00398 00400 PString challengeKey; 00401 00403 BOOL encryptionEnabled; 00404 00405 #if P_SSL_AES 00406 00407 AES_KEY aesEncryptKey; 00408 00410 AES_KEY aesDecryptKey; 00411 #endif 00412 }; 00413 00415 00416 #endif 00417 00418 /* The comment below is magic for those who use emacs to edit this file. */ 00419 /* With the comment below, the tab key does auto indent to 4 spaces. */ 00420 00421 /* 00422 * Local Variables: 00423 * mode:c 00424 * c-file-style:linux 00425 * c-basic-offset:2 00426 * End: 00427 */ 00428