op.h 9.51 KB
Newer Older
1
/*
2
 * op.h
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 */
19

20 21
#ifndef _L_SAL_OP_H_
#define _L_SAL_OP_H_
22 23 24 25

#include <bctoolbox/list.h>
#include <bctoolbox/port.h>
#include <belle-sip/types.h>
26 27

#include "c-wrapper/internal/c-sal.h"
28
#include "content/content.h"
29
#include "logger/logger.h"
30
#include "sal/sal.h"
31 32 33 34 35

LINPHONE_BEGIN_NAMESPACE

class SalOp {
public:
36 37
	SalOp (Sal *sal);
	virtual ~SalOp ();
38

39 40
	SalOp *ref ();
	void *unref ();
41

42
	Sal *getSal () const { return mRoot; }
43

44 45
	void setUserPointer (void *value) { mUserPointer = value; }
	void *getUserPointer () const { return mUserPointer; }
46

47 48
	void setSubject (const std::string &value) { mSubject = value; }
	const std::string &getSubject () const { return mSubject; }
49

50
	void setFrom (const std::string &value);
51
	void setFromAddress (const SalAddress *value);
52
	const std::string &getFrom () const { return mFrom; }
53
	const SalAddress *getFromAddress () const { return mFromAddress; }
54

55
	void setTo (const std::string &value);
56
	void setToAddress (const SalAddress *value);
57
	const std::string &getTo () const { return mTo; }
58
	const SalAddress *getToAddress () const { return mToAddress; }
59

60 61
	void setContactAddress (const SalAddress* value);
	const SalAddress *getContactAddress() const { return mContactAddress; }
62

63
	void setRoute (const std::string &value);
64
	void setRouteAddress (const SalAddress *value);
65
	const std::list<SalAddress *> &getRouteAddresses () const { return mRouteAddresses; }
66
	void addRouteAddress (const SalAddress *address);
67

68 69
	void setDiversionAddress (const SalAddress *value);
	const SalAddress *getDiversionAddress () const { return mDiversionAddress; }
70

71 72
	void setServiceRoute (const SalAddress *value);
	const SalAddress *getServiceRoute () const { return mServiceRoute; }
73

74
	void setManualRefresherMode (bool value) { mManualRefresher = value; }
75

76 77
	void setEntityTag (const std::string &value) { mEntityTag = value; }
	const std::string &getEntityTag() const { return mEntityTag; }
78

79
	void setEvent (const std::string &eventName);
80

81 82
	void setPrivacy (SalPrivacyMask value) { mPrivacy = value; }
	SalPrivacyMask getPrivacy() const { return mPrivacy; }
83

84
	void setRealm (const std::string &value) { mRealm = value; }
85

86
	void setSentCustomHeaders (SalCustomHeader *ch);
87

88 89
	void enableCnxIpTo0000IfSendOnly (bool value) { mCnxIpTo0000IfSendOnlyEnabled = value; }
	bool cnxIpTo0000IfSendOnlyEnabled () const { return mCnxIpTo0000IfSendOnlyEnabled; }
90

91 92 93 94
	const std::string &getProxy () const { return mRoute; }
	const std::string &getNetworkOrigin () const { return mOrigin; }
	const std::string &getCallId () const { return  mCallId; }
	std::string getDialogId () const;
95 96
	int getAddressFamily () const;
	const SalCustomHeader *getRecvCustomHeaders () const { return mRecvCustomHeaders; }
97
	const std::string &getRemoteContact () const { return mRemoteContact; }
98
	const SalAddress *getRemoteContactAddress () const { return mRemoteContactAddress; }
99
	const std::string &getRemoteUserAgent () const { return mRemoteUserAgent; }
100

101 102 103 104 105 106
	const char *getPublicAddress (int *port) {
		return mRefresher ? belle_sip_refresher_get_public_address(mRefresher, port) : nullptr;
	}
	const char *getLocalAddress (int *port) {
		return mRefresher ? belle_sip_refresher_get_local_address(mRefresher, port) : nullptr;
	}
107

108 109
	const SalErrorInfo *getErrorInfo () const { return &mErrorInfo; }
	const SalErrorInfo *getReasonErrorInfo () const { return &mReasonErrorInfo; }
110

111
	bool isForkedOf (const SalOp *op) const {
112
		return !mCallId.empty() && !op->mCallId.empty() && (mCallId == op->mCallId);
113
	}
114
	bool isIdle () const;
115

116 117 118 119 120 121 122
	void stopRefreshing () {
		if (mRefresher)
			belle_sip_refresher_stop(mRefresher);
	}
	int refresh ();
	void killDialog ();
	void release ();
123

124 125 126 127
	virtual void authenticate (const SalAuthInfo *info) { 
        processAuthentication(); }
	void cancelAuthentication () { lFatal() << "SalOp::cancelAuthentication not implemented yet"; }
	SalAuthInfo *getAuthRequested () { return mAuthInfo; }
128

129 130
	int ping (const std::string &from, const std::string &to);
	int sendInfo (const SalBodyHandler *bodyHandler);
131

132 133 134 135
protected:
	enum class State {
		Early = 0,
		Active,
136
		Terminating, // This state is used to wait until a proceeding state, so we can send the cancel
137 138
		Terminated
	};
139

140
	static std::string toString (const State value);
141 142 143 144 145

	enum class Dir {
		Incoming = 0,
		Outgoing
	};
146

147 148 149 150 151 152 153
	enum class Type {
		Unknown,
		Register,
		Call,
		Message,
		Presence,
		Publish,
154
		Subscribe,
155
		Refer // For out of dialog refer only
156
	};
157

158
	static std::string toString (const Type type);
159

160
	using ReleaseCb = void (*) (SalOp *op);
161

162 163 164 165
	virtual void fillCallbacks () {}
	void releaseImpl ();
	void processAuthentication ();
	int processRedirect ();
166

167
	belle_sip_request_t *buildRequest (const std::string &method);
168
	int sendRequest (belle_sip_request_t *request);
169
	int sendRequestWithContact (belle_sip_request_t *request, bool addContact);
170 171 172
	int sendRequestWithExpires (belle_sip_request_t *request, int expires);
	void resendRequest (belle_sip_request_t *request);
	int sendRequestAndCreateRefresher (belle_sip_request_t *request, int expires, belle_sip_refresher_listener_t listener);
173

174 175
	void setReasonErrorInfo (belle_sip_message_t *message);
	void setErrorInfoFromResponse (belle_sip_response_t *response);
176

177 178
	void setReferredBy (belle_sip_header_referred_by_t *referredByHeader);
	void setReplaces (belle_sip_header_replaces_t *replacesHeader);
179

180 181
	void setRemoteContact (const std::string &value);
	void setNetworkOrigin (const std::string &value);
182 183 184
	void setNetworkOriginAddress (SalAddress *value);
	void setPrivacyFromMessage (belle_sip_message_t *message);
	void setRemoteUserAgent (belle_sip_message_t *message);
185

186 187 188 189
	belle_sip_response_t *createResponseFromRequest (belle_sip_request_t *request, int code) {
		return mRoot->createResponseFromRequest(request, code);
	}
	belle_sip_header_contact_t *createContact ();
190

191 192 193
	void setOrUpdateDialog (belle_sip_dialog_t *dialog);
	belle_sip_dialog_t *linkOpWithDialog (belle_sip_dialog_t *dialog);
	void unlinkOpFromDialog (belle_sip_dialog_t *dialog);
194

195
	SalBodyHandler *getBodyHandler (belle_sip_message_t *message);
196

197
	void assignRecvHeaders (belle_sip_message_t *message);
198

199
	bool isSecure () const;
200 201 202
	void addHeaders (belle_sip_header_t *h, belle_sip_message_t *message);
	void addCustomHeaders (belle_sip_message_t *message);
	int unsubscribe ();
203

204 205 206
	void processIncomingMessage (const belle_sip_request_event_t *event);
	int replyMessage (SalReason reason);
	void addMessageAccept (belle_sip_message_t *message);
207

208
	static bool isExternalBody (belle_sip_header_content_type_t* contentType);
209

210 211
	static void assignAddress (SalAddress **address, const std::string &value);
	static void addInitialRouteSet (belle_sip_request_t *request, const std::list<SalAddress *> &routeAddresses);
212

213
	// SalOpBase
214
	Sal *mRoot = nullptr;
215 216
	std::string mRoute; // Or request-uri for REGISTER
	std::list<SalAddress *> mRouteAddresses;
217
	SalAddress *mContactAddress = nullptr;
218 219
	std::string mSubject;
	std::string mFrom;
220
	SalAddress* mFromAddress = nullptr;
221
	std::string mTo;
222
	SalAddress *mToAddress = nullptr;
223
	std::string mOrigin;
224 225
	SalAddress *mOriginAddress = nullptr;
	SalAddress *mDiversionAddress = nullptr;
226
	std::string mRemoteUserAgent;
227
	SalAddress *mRemoteContactAddress = nullptr;
228
	std::string mRemoteContact;
229
	void *mUserPointer = nullptr;
230 231
	std::string mCallId;
	std::string mRealm;
232 233 234
	SalAddress *mServiceRoute = nullptr; // As defined by rfc3608, might be a list
	SalCustomHeader *mSentCustomHeaders = nullptr;
	SalCustomHeader *mRecvCustomHeaders = nullptr;
235
	std::string mEntityTag; // As defined by rfc3903 (I.E publih)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	ReleaseCb mReleaseCb = nullptr;

	const belle_sip_listener_callbacks_t *mCallbacks = nullptr;
	SalErrorInfo mErrorInfo;
	SalErrorInfo mReasonErrorInfo;
	belle_sip_client_transaction_t *mPendingAuthTransaction = nullptr;
	belle_sip_server_transaction_t *mPendingServerTransaction = nullptr;
	belle_sip_server_transaction_t *mPendingUpdateServerTransaction = nullptr;
	belle_sip_client_transaction_t *mPendingClientTransaction = nullptr;
	SalAuthInfo *mAuthInfo = nullptr;
	belle_sip_dialog_t *mDialog = nullptr;
	belle_sip_header_replaces_t *mReplaces = nullptr;
	belle_sip_header_referred_by_t *mReferredBy = nullptr;
	SalMediaDescription *mResult = nullptr;
	belle_sdp_session_description_t *mSdpAnswer = nullptr;
	State mState = State::Early;
	Dir mDir = Dir::Incoming;
	belle_sip_refresher_t *mRefresher = nullptr;
	int mRef = 0;
	Type mType = Type::Unknown;
	SalPrivacyMask mPrivacy = SalPrivacyNone;
	belle_sip_header_event_t *mEvent = nullptr; // Used by SalOpSubscribe kinds
	SalOpSDPHandling mSdpHandling = SalOpSDPNormal;
	int mAuthRequests = 0; // number of auth requested for this op
260 261 262 263 264 265 266 267
	bool mCnxIpTo0000IfSendOnlyEnabled = false;
	bool mAutoAnswerAsked = false;
	bool mSdpOffering = false;
	bool mCallReleased = false;
	bool mManualRefresher = false;
	bool mHasAuthPending = false;
	bool mSupportsSessionTimers = false;
	bool mOpReleased = false;
268

269 270 271 272 273
	friend class Sal;
};

LINPHONE_END_NAMESPACE

274
#endif // ifndef _L_SAL_OP_H_