Yate
yatemgcp.h
1
20
21#ifndef __YATEMGCP_H
22#define __YATEMGCP_H
23
24#include <yateclass.h>
25#include <yatemime.h>
26
27#ifdef _WINDOWS
28
29#ifdef LIBYMGCP_EXPORTS
30#define YMGCP_API __declspec(dllexport)
31#else
32#ifndef LIBYMGCP_STATIC
33#define YMGCP_API __declspec(dllimport)
34#endif
35#endif
36
37#endif /* _WINDOWS */
38
39#ifndef YMGCP_API
40#define YMGCP_API
41#endif
42
46namespace TelEngine {
47
48class MGCPMessage;
49class MGCPTransaction;
50class MGCPEpInfo;
51class MGCPEndpoint;
52class MGCPEvent;
53class MGCPEngine;
54
60class YMGCP_API MGCPMessage : public RefObject
61{
62 friend class MGCPTransaction;
63public:
74 MGCPMessage(MGCPEngine* engine, const char* name, const char* ep, const char* ver = "MGCP 1.0");
75
84 MGCPMessage(MGCPTransaction* trans, unsigned int code, const char* comment = 0);
85
89 virtual ~MGCPMessage();
90
95 inline bool valid() const
96 { return m_valid; }
97
102 inline const String& name() const
103 { return m_name; }
104
109 inline int code() const
110 { return m_code; }
111
116 inline const String& version() const
117 { return m_version; }
118
123 inline const String& comment() const
124 { return m_comment; }
125
130 inline bool isCommand() const
131 { return code() < 0; }
132
137 inline bool isResponse() const
138 { return 100 <= code(); }
139
144 inline bool isAck() const
145 { return 0 <= code() && code() <= 99; }
146
151 inline unsigned int transactionId() const
152 { return m_transaction; }
153
158 inline const String& endpointId() const
159 { return m_endpoint; }
160
166 void toString(String& dest) const;
167
180 static bool parse(MGCPEngine* engine, ObjList& dest,
181 const unsigned char* buffer, unsigned int len,
182 const char* sdpType = "application/sdp");
183
188
193
194protected:
205 MGCPMessage(MGCPEngine* engine, const char* name, int code,
206 unsigned int transId, const char* epId, const char* ver);
207
208private:
209 MGCPMessage() : params("") {} // Avoid using default constructor
210 // Decode the message line
211 static MGCPMessage* decodeMessage(const char* line, unsigned int len, unsigned int& trans,
212 String& error, MGCPEngine* engine);
213 // Decode message parameters. Return true if found a line containing a dot
214 static bool decodeParams(const unsigned char* buffer, unsigned int len,
215 unsigned int& crt, MGCPMessage* msg, String& error, MGCPEngine* engine);
216
217 String m_name; // Command or string representation of response code
218 bool m_valid; // False if this message is invalid
219 int m_code; // Response code or -1 if this is a command
220 unsigned int m_transaction; // The id of the transaction this message belongs to
221 String m_endpoint; // The id of the endpoint issuing this message
222 String m_version; // The protocol version
223 String m_comment; // The comment attached to a response message
224};
225
230class YMGCP_API MGCPTransaction : public RefObject, public Mutex
231{
232 friend class MGCPEngine; // Process a received message
233 friend class MGCPEvent; // Access to event termination notification
234public:
238 enum State {
239 Invalid = 0, // This is an invalid transaction (constructor failed)
240 Initiated = 1, // An initial command message was sent/received
241 Trying = 2, // Sent or received a provisional response to the initial message
242 Responded = 3, // Sent or received a final response to the initial message
243 Ack = 4, // Response was ack'd
244 Destroying = 5, // Waiting to be removed from the engine
245 };
246
256 const SocketAddr& address, bool engineProcess = true);
257
262
267 inline State state() const
268 { return m_state; }
269
274 inline unsigned int id() const
275 { return m_id; }
276
281 inline bool outgoing() const
282 { return m_outgoing; }
283
288 inline const String& ep() const
289 { return m_endpoint; }
290
295 const SocketAddr& addr() const
296 { return m_address; }
297
302 inline MGCPEngine* engine()
303 { return m_engine; }
304
309 inline const MGCPMessage* initial() const
310 { return m_cmd; }
311
316 inline const MGCPMessage* msgProvisional() const
317 { return m_provisional; }
318
323 inline const MGCPMessage* msgResponse() const
324 { return m_response; }
325
330 inline const MGCPMessage* msgAck() const
331 { return m_ack; }
332
337 inline bool timeout() const
338 { return m_timeout; }
339
344 inline void ackRequest(bool request)
345 { m_ackRequest = request; }
346
351 inline void* userData() const
352 { return m_private; }
353
358 inline void userData(void* data)
359 { m_private = data; }
360
365 inline void setEngineProcess()
366 { m_engineProcess = true; }
367
373 MGCPEvent* getEvent(u_int64_t time = Time());
374
381 bool sendProvisional(int code = 100, const char* comment = 0);
382
390 inline bool setResponse(int code, const char* comment = 0)
391 { return setResponse(new MGCPMessage(this,code,comment)); }
392
403 bool setResponse(int code, const NamedList* params, MimeSdpBody* sdp1 = 0,
404 MimeSdpBody* sdp2 = 0);
405
413
414protected:
418 virtual void destroyed();
419
425
431 MGCPEvent* checkTimeout(u_int64_t time);
432
437 void eventTerminated(MGCPEvent* event);
438
443 void changeState(State newState);
444
449 void setProvisional(int code = 100);
450
455 void send(MGCPMessage* msg);
456
457private:
458 MGCPTransaction() {} // Avoid using default constructor
459 // Check if received any final response. Create an event. Init timeout.
460 // Send a response ACK if requested by the response
461 MGCPEvent* checkResponse(u_int64_t time);
462 // Init timeout for retransmission or transaction termination
463 void initTimeout(u_int64_t time, bool extra);
464 // Remove from engine. Create event. Deref the transaction
465 MGCPEvent* terminate();
466
467 State m_state; // Current state
468 unsigned int m_id; // Transaction id
469 bool m_outgoing; // Transaction direction
470 SocketAddr m_address; // Remote andpoint's address
471 MGCPEngine* m_engine; // The engine owning this transaction
472 MGCPMessage* m_cmd; // The command that created this transaction
473 MGCPMessage* m_provisional; // The provisional response to the command that created this transaction
474 MGCPMessage* m_response; // The response to the command that created this transaction
475 MGCPMessage* m_ack; // The response aknowledgement message sent or received
476 MGCPEvent* m_lastEvent; // The last generated event
477 String m_endpoint; // The endpoint owning this transaction
478 u_int64_t m_nextRetrans; // Retransission or destroy time
479 unsigned int m_crtRetransInterval; // Current retransmission interval
480 unsigned int m_retransCount; // Remainig number of retransmissions
481 bool m_timeout; // Transaction timeout flag
482 bool m_ackRequest; // Remote is requested to send ACK
483 void* m_private; // Data used by this transaction's user
484 String m_debug; // String used to identify the transaction in debug messages
485 bool m_engineProcess; // Process transaction (getEvent) from engine processor
486};
487
492class YMGCP_API MGCPEndpointId
493{
494public:
499 : m_port(0)
500 {}
501
506 inline MGCPEndpointId(const String& src)
507 : m_port(0)
508 { set(src); }
509
514 inline MGCPEndpointId(const MGCPEndpointId& value)
515 : m_id(value.id()), m_endpoint(value.user()),
516 m_host(value.host()), m_port(value.port())
517 { }
518
526 inline MGCPEndpointId(const char* endpoint, const char* host, int port, bool addPort = true)
527 : m_port(0)
528 { set(endpoint,host,port,addPort); }
529
534 inline const String& id() const
535 { return m_id; }
536
541 inline const String& user() const
542 { return m_endpoint; }
543
548 inline const String& host() const
549 { return m_host; }
550
555 inline int port() const
556 { return m_port; }
557
563 inline void port(int newPort, bool addPort = true)
564 { set(m_endpoint,m_host,newPort,addPort); }
565
573 void set(const char* endpoint, const char* host, int port, bool addPort = true);
574
579 inline void set(const String& src) {
580 URI uri(src);
581 set(uri.getUser(),uri.getHost(),uri.getPort());
582 }
583
590 inline bool valid() const {
591 return m_endpoint && m_endpoint.length() < 256 &&
592 m_host && m_host.length() < 256;
593 }
594
595private:
596 String m_id; // The complete id
597 String m_endpoint; // The endpoint's name inside the host
598 String m_host; // Host of this endpoint
599 int m_port; // Port used by this endpoint
600};
601
606class YMGCP_API MGCPEpInfo : public MGCPEndpointId, public GenObject
607{
608public:
616 inline MGCPEpInfo(const char* endpoint, const char* host, int port, bool addPort = true)
617 : MGCPEndpointId(endpoint,host,port,addPort),
618 m_address(AF_INET), m_resolve(true) {
619 m_address.port(port);
620 }
621
626 virtual const String& toString() const
627 { return id(); }
628
633 inline const SocketAddr& address() const
634 { return m_address; }
635
641
646 inline void address(const SocketAddr& addr)
647 { m_resolve = false; m_address = addr; }
648
653
654private:
655 SocketAddr m_address;
656 bool m_resolve;
657};
658
665class YMGCP_API MGCPEndpoint : public RefObject, public MGCPEndpointId, public Mutex
666{
667public:
677 MGCPEndpoint(MGCPEngine* engine, const char* user, const char* host, int port, bool addPort = true);
678
682 virtual ~MGCPEndpoint();
683
688 virtual const String& toString() const
689 { return MGCPEndpointId::id(); }
690
696 { return m_engine; }
697
710 MGCPEpInfo* append(const char* endpoint, const char* host, int port = 0);
711
715 inline void clear()
716 { lock(); m_remote.clear(); unlock(); }
717
723 MGCPEpInfo* find(const String& epId);
724
731
737
738private:
739 MGCPEngine* m_engine; // The engine owning this endpoint
740 ObjList m_remote; // The remote endpoints
741};
742
748class YMGCP_API MGCPEvent
749{
750 friend class MGCPTransaction;
751public:
756
761 inline MGCPTransaction* transaction()
762 { return m_transaction; }
763
768 inline MGCPMessage* message() const
769 { return m_message; }
770
771protected:
777 MGCPEvent(MGCPTransaction* trans, MGCPMessage* msg = 0);
778
779private:
780 MGCPTransaction* m_transaction; // The transaction that generated this event
781 MGCPMessage* m_message; // The message carried by this event, if any
782};
783
784class MGCPPrivateThread;
785
795class YMGCP_API MGCPEngine : public DebugEnabler, public Mutex
796{
797 friend class MGCPPrivateThread;
798 friend class MGCPTransaction;
799public:
807 MGCPEngine(bool gateway, const char* name = 0, const NamedList* params = 0);
808
812 virtual ~MGCPEngine();
813
819 inline bool gateway() const
820 { return m_gateway; }
821
826 inline const SocketAddr& address() const
827 { return m_address; }
828
834 inline unsigned int maxRecvPacket() const
835 { return m_maxRecvPacket; }
836
841 inline bool allowUnkCmd() const
842 { return m_allowUnkCmd; }
843
848 inline unsigned int retransInterval() const
849 { return m_retransInterval; }
850
855 inline unsigned int retransCount() const
856 { return m_retransCount; }
857
862 inline u_int64_t extraTime() const
863 { return m_extraTime; }
864
869 inline bool parseParamToLower() const
870 { return m_parseParamToLower; }
871
876 inline bool provisional() const
877 { return m_provisional; }
878
883 inline bool ackRequest() const
884 { return m_ackRequest; }
885
890 inline void ackRequest(bool request)
891 { m_ackRequest = request; }
892
897 virtual void initialize(const NamedList& params);
898
904 inline bool knownCommand(const String& cmd)
905 { Lock lock(this); return (m_knownCommands.find(cmd) != 0); }
906
911 void addCommand(const char* cmd);
912
918
926 void detach(MGCPEndpoint* ep, bool del = false, bool delTrans = false);
927
934
941
948 MGCPTransaction* findTrans(unsigned int id, bool outgoing);
949
954 unsigned int getNextId();
955
965 MGCPTransaction* sendCommand(MGCPMessage* cmd, const SocketAddr& address,
966 bool engineProcess = true);
967
975 bool receive(unsigned char* buffer, SocketAddr& addr);
976
985 bool process(u_int64_t time = Time());
986
996 bool processTransaction(MGCPTransaction* tr, u_int64_t time = Time());
997
1003
1008
1013
1019 MGCPEvent* getEvent(u_int64_t time = Time());
1020
1029 virtual bool processEvent(MGCPEvent* event);
1030
1039 virtual bool processEvent(MGCPTransaction* trans, MGCPMessage* msg);
1040
1048
1058 void cleanup(bool gracefully = true, const char* text = "Shutdown");
1059
1066 static inline int defaultPort(bool gateway)
1067 { return gateway ? 2427 : 2727; }
1068
1073 virtual void timeout(MGCPTransaction* tr)
1074 { }
1075
1080
1085
1094
1095protected:
1102 bool sendData(const String& msg, const SocketAddr& address);
1103
1108 void appendTrans(MGCPTransaction* trans);
1109
1115 void removeTrans(MGCPTransaction* trans, bool del);
1116
1121
1126
1131
1132private:
1133 // Append a private thread to the list
1134 void appendThread(MGCPPrivateThread* thread);
1135 // Remove private thread from the list without deleting it
1136 void removeThread(MGCPPrivateThread* thread);
1137 // Process ACK received with a message or response
1138 // Return a list of ack'd transactions or 0 if the parameter is incorrect
1139 unsigned int* decodeAck(const String& param, unsigned int & count);
1140
1141 bool m_gateway; // True if this engine is an MGCP gateway, false if call agent
1142 bool m_initialized; // True if the engine was already initialized
1143 unsigned int m_nextId; // Next outgoing transaction id
1144 Socket m_socket; // The socket used to send/receive data
1145 SocketAddr m_address; // The IP address used by this engine
1146 unsigned int m_maxRecvPacket; // The maximum length or received packets
1147 unsigned char* m_recvBuf; // Receiving buffer
1148 bool m_allowUnkCmd; // Allow this engine to send/accept unknown commands
1149 unsigned int m_retransInterval; // Message retransmission interval
1150 unsigned int m_retransCount; // Maximum number of retransmissions for a message
1151 u_int64_t m_extraTime; // Time to live after the transaction terminated gracefully
1152 bool m_parseParamToLower; // Convert received messages' params to lower case
1153 bool m_provisional; // Send provisional responses flag
1154 bool m_ackRequest; // Remote is requested to send ACK
1155 ObjList m_knownCommands; // The list of known commands
1156 ObjList m_threads;
1157};
1158
1159}
1160
1161#endif /* __YATEMGCP_H */
1162
1163/* vi: set ts=8 sw=4 sts=4 noet: */
DebugEnabler(int level=TelEngine::debugLevel(), bool enabled=true)
Definition yateclass.h:319
Class used to iterate the items of a list.
Definition yateclass.h:3828
Ephemeral mutex or semaphore locking object.
Definition yateclass.h:5833
int port() const
Definition yatemgcp.h:555
MGCPEndpointId()
Definition yatemgcp.h:498
bool valid() const
Definition yatemgcp.h:590
const String & id() const
Definition yatemgcp.h:534
void set(const char *endpoint, const char *host, int port, bool addPort=true)
const String & host() const
Definition yatemgcp.h:548
void set(const String &src)
Definition yatemgcp.h:579
void port(int newPort, bool addPort=true)
Definition yatemgcp.h:563
MGCPEndpointId(const String &src)
Definition yatemgcp.h:506
const String & user() const
Definition yatemgcp.h:541
MGCPEndpointId(const MGCPEndpointId &value)
Definition yatemgcp.h:514
MGCPEndpointId(const char *endpoint, const char *host, int port, bool addPort=true)
Definition yatemgcp.h:526
An MGCP endpoint.
Definition yatemgcp.h:666
MGCPEndpoint(MGCPEngine *engine, const char *user, const char *host, int port, bool addPort=true)
virtual ~MGCPEndpoint()
MGCPEpInfo * findAlias(const String &alias)
MGCPEpInfo * find(const String &epId)
virtual const String & toString() const
Definition yatemgcp.h:688
MGCPEpInfo * peer()
MGCPEpInfo * append(const char *endpoint, const char *host, int port=0)
void clear()
Definition yatemgcp.h:715
MGCPEngine * engine()
Definition yatemgcp.h:695
An MGCP engine.
Definition yatemgcp.h:796
void addCommand(const char *cmd)
MGCPEndpoint * findEp(const String &epId)
MGCPEvent * getEvent(u_int64_t time=Time())
bool ackRequest() const
Definition yatemgcp.h:883
bool sendData(const String &msg, const SocketAddr &address)
void runProcess()
bool allowUnkCmd() const
Definition yatemgcp.h:841
MGCPEndpoint * findEp(MGCPEndpoint *ep)
void attach(MGCPEndpoint *ep)
static TokenDict mgcp_commands[]
Definition yatemgcp.h:1079
void ackRequest(bool request)
Definition yatemgcp.h:890
static TokenDict mgcp_reasons[]
Definition yatemgcp.h:1093
ObjList m_transactions
Definition yatemgcp.h:1125
void appendTrans(MGCPTransaction *trans)
unsigned int retransCount() const
Definition yatemgcp.h:855
ObjList m_endpoints
Definition yatemgcp.h:1120
virtual bool processEvent(MGCPEvent *event)
bool provisional() const
Definition yatemgcp.h:876
bool receive(unsigned char *buffer, SocketAddr &addr)
virtual bool processEvent(MGCPTransaction *trans, MGCPMessage *msg)
unsigned int retransInterval() const
Definition yatemgcp.h:848
bool knownCommand(const String &cmd)
Definition yatemgcp.h:904
const SocketAddr & address() const
Definition yatemgcp.h:826
ListIterator m_iterator
Definition yatemgcp.h:1130
u_int64_t extraTime() const
Definition yatemgcp.h:862
void detach(MGCPEndpoint *ep, bool del=false, bool delTrans=false)
static TokenDict mgcp_responses[]
Definition yatemgcp.h:1084
bool gateway() const
Definition yatemgcp.h:819
MGCPTransaction * sendCommand(MGCPMessage *cmd, const SocketAddr &address, bool engineProcess=true)
unsigned int getNextId()
void removeTrans(MGCPTransaction *trans, bool del)
MGCPEngine(bool gateway, const char *name=0, const NamedList *params=0)
void cleanup(bool gracefully=true, const char *text="Shutdown")
virtual void timeout(MGCPTransaction *tr)
Definition yatemgcp.h:1073
bool parseParamToLower() const
Definition yatemgcp.h:869
virtual ~MGCPEngine()
static int defaultPort(bool gateway)
Definition yatemgcp.h:1066
virtual void initialize(const NamedList &params)
void runReceive()
bool process(u_int64_t time=Time())
unsigned int maxRecvPacket() const
Definition yatemgcp.h:834
MGCPTransaction * findTrans(unsigned int id, bool outgoing)
bool processTransaction(MGCPTransaction *tr, u_int64_t time=Time())
void returnEvent(MGCPEvent *event)
void runReceive(SocketAddr &addr)
Remote endpoint info class.
Definition yatemgcp.h:607
const SocketAddr & address()
MGCPEpInfo(const char *endpoint, const char *host, int port, bool addPort=true)
Definition yatemgcp.h:616
const SocketAddr & address() const
Definition yatemgcp.h:633
virtual const String & toString() const
Definition yatemgcp.h:626
void address(const SocketAddr &addr)
Definition yatemgcp.h:646
String alias
Definition yatemgcp.h:652
An MGCP event.
Definition yatemgcp.h:749
MGCPTransaction * transaction()
Definition yatemgcp.h:761
MGCPEvent(MGCPTransaction *trans, MGCPMessage *msg=0)
MGCPMessage * message() const
Definition yatemgcp.h:768
An MGCP command or response.
Definition yatemgcp.h:61
MGCPMessage(MGCPTransaction *trans, unsigned int code, const char *comment=0)
bool isAck() const
Definition yatemgcp.h:144
MGCPMessage(MGCPEngine *engine, const char *name, const char *ep, const char *ver="MGCP 1.0")
void toString(String &dest) const
const String & comment() const
Definition yatemgcp.h:123
bool valid() const
Definition yatemgcp.h:95
int code() const
Definition yatemgcp.h:109
unsigned int transactionId() const
Definition yatemgcp.h:151
ObjList sdp
Definition yatemgcp.h:192
NamedList params
Definition yatemgcp.h:187
static bool parse(MGCPEngine *engine, ObjList &dest, const unsigned char *buffer, unsigned int len, const char *sdpType="application/sdp")
bool isCommand() const
Definition yatemgcp.h:130
MGCPMessage(MGCPEngine *engine, const char *name, int code, unsigned int transId, const char *epId, const char *ver)
const String & name() const
Definition yatemgcp.h:102
bool isResponse() const
Definition yatemgcp.h:137
const String & version() const
Definition yatemgcp.h:116
const String & endpointId() const
Definition yatemgcp.h:158
virtual ~MGCPMessage()
void eventTerminated(MGCPEvent *event)
MGCPEvent * getEvent(u_int64_t time=Time())
void changeState(State newState)
bool setResponse(int code, const char *comment=0)
Definition yatemgcp.h:390
virtual void destroyed()
void ackRequest(bool request)
Definition yatemgcp.h:344
void processMessage(MGCPMessage *msg)
bool setResponse(int code, const NamedList *params, MimeSdpBody *sdp1=0, MimeSdpBody *sdp2=0)
State
Definition yatemgcp.h:238
const MGCPMessage * msgAck() const
Definition yatemgcp.h:330
const MGCPMessage * msgResponse() const
Definition yatemgcp.h:323
const SocketAddr & addr() const
Definition yatemgcp.h:295
void * userData() const
Definition yatemgcp.h:351
void userData(void *data)
Definition yatemgcp.h:358
MGCPTransaction(MGCPEngine *engine, MGCPMessage *msg, bool outgoing, const SocketAddr &address, bool engineProcess=true)
const MGCPMessage * msgProvisional() const
Definition yatemgcp.h:316
const MGCPMessage * initial() const
Definition yatemgcp.h:309
void send(MGCPMessage *msg)
void setEngineProcess()
Definition yatemgcp.h:365
const String & ep() const
Definition yatemgcp.h:288
virtual ~MGCPTransaction()
bool timeout() const
Definition yatemgcp.h:337
bool sendProvisional(int code=100, const char *comment=0)
MGCPEngine * engine()
Definition yatemgcp.h:302
State state() const
Definition yatemgcp.h:267
void setProvisional(int code=100)
bool setResponse(MGCPMessage *msg)
MGCPEvent * checkTimeout(u_int64_t time)
unsigned int id() const
Definition yatemgcp.h:274
bool outgoing() const
Definition yatemgcp.h:281
static int count()
virtual bool unlock()
Mutex(bool recursive=false, const char *name=0)
virtual bool lock(long maxwait=-1)
A named string container class.
Definition yateclass.h:5016
An object list class.
Definition yateclass.h:1454
A socket address holder.
Definition yateclass.h:6400
A generic socket class.
Definition yateclass.h:7361
A C-style string handling class.
Definition yateclass.h:2131
A time holding class.
Definition yateclass.h:3927
Encapsulation for an URI.
Definition yateclass.h:5396
const String & getUser() const
Definition yateclass.h:5475
int getPort() const
Definition yateclass.h:5489
const String & getHost() const
Definition yateclass.h:5482
Definition yatemime.h:34
Definition yateclass.h:848