eXosip.h 14.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
/*
  eXosip - This is the eXtended osip library.
  Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com

  eXosip 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.

  eXosip 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
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  In addition, as a special exception, the copyright holders give
  permission to link the code of portions of this program with the
  OpenSSL library under certain conditions as described in each
  individual source file, and distribute linked combinations
  including the two.
  You must obey the GNU General Public License in all respects
  for all of the code used other than OpenSSL.  If you modify
  file(s) with this exception, you may extend this exception to your
  version of the file(s), but you are not obligated to do so.  If you
  do not wish to do so, delete this exception statement from your
  version.  If you delete this exception statement from all source
  files in the program, then also delete it here.
*/

#ifdef ENABLE_MPATROL
#include <mpatrol.h>
#endif

#ifndef __EXOSIP_H__
#define __EXOSIP_H__

#include <eXosip2/eX_setup.h>
#include <eXosip2/eX_register.h>
#include <eXosip2/eX_call.h>
#include <eXosip2/eX_options.h>
#include <eXosip2/eX_subscribe.h>
#include <eXosip2/eX_message.h>
#include <eXosip2/eX_publish.h>

#include <osipparser2/osip_parser.h>
#include <osipparser2/sdp_message.h>
#include <time.h>

/**
 * @file eXosip.h
 * @brief eXosip API
 *
 * eXosip is a high layer library for rfc3261: the SIP protocol.
 * It offers a simple API to make it easy to use. eXosip2 offers
 * great flexibility for implementing SIP endpoint like:
 * <ul>
 * <li>SIP User-Agents</li>
 * <li>SIP Voicemail or IVR</li>
 * <li>any SIP server acting as an endpoint (music server...)</li>
 * </ul>
 *
 * If you need to implement proxy or complex SIP applications,
 * you should consider using osip instead.
 *
 * Here are the eXosip capabilities:
 * <pre>
 *    REGISTER                 to handle registration.
 *    INVITE/BYE               to start/stop VoIP sessions.
 *    INFO                     to send DTMF within a VoIP sessions.
 *    OPTIONS                  to simulate VoIP sessions.
 *    re-INVITE                to modify VoIP sessions.
 *    REFER/NOTIFY             to transfer calls.
 *    MESSAGE                  to send Instant Message.
 *    SUBSCRIBE/REFER/NOTIFY   to handle presence capabilities.
 *    any other request        to handle what you want (outside dialogs)!
 * </pre>
 * <P>
 */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Structure for event description.
 * @var eXosip_event_t
 */
typedef struct eXosip_event eXosip_event_t;

/**
 * @defgroup eXosip2_authentication eXosip2 authentication API
 * @ingroup eXosip2_msg
 * @{
 */

/**
 * Add authentication credentials. These are used when an outgoing
 * request comes back with an authorization required response.
 *
 * @param excontext    eXosip_t instance.
 * @param username     username
 * @param userid       login (usually equals the username)
 * @param passwd       password
 * @param ha1          MD5 ha1
 * @param realm        realm within which credentials apply, or NULL to apply credentials to unrecognized realms
 */
int eXosip_add_authentication_info(struct eXosip_t *excontext, const char *username, const char *userid, const char *passwd, const char *ha1, const char *realm);

/**
 * Remove authentication credentials.
 *
 * @param excontext    eXosip_t instance.
 * @param username     username
 * @param realm        realm must be exact same arg as for eXosip_add_authentication_info
 */
int eXosip_remove_authentication_info(struct eXosip_t *excontext, const char *username, const char *realm);

/**
 * Clear all authentication credentials stored in eXosip
 *
 * @param excontext    eXosip_t instance.
 */
int eXosip_clear_authentication_info(struct eXosip_t *excontext);

/**
 * Initiate some default actions:
 *
 *  Retry with credentials upon reception of 401/407.
 *  Retry with Contact header upon reception of 3xx request.
 *
 *  Usefull & required when eXosip_automatic_action() can't do the automatic action:
 *  1/ if you receive a 401 or 407 for BYE (event EXOSIP_CALL_MESSAGE_REQUESTFAILURE).
 *  2/ if you receive 401 or 407 for any sip request outside of dialog (EXOSIP_MESSAGE_REQUESTFAILURE)
 *
 * @param excontext    eXosip_t instance.
 * @param je           event to work on.
 */
int eXosip_default_action(struct eXosip_t *excontext, eXosip_event_t *je);

/**
 * Initiate some automatic actions:
 *
 *  Retry with credentials upon reception of 401/407.
 *  Retry with higher Session-Expires upon reception of 422.
 *  Refresh REGISTER and SUBSCRIBE/REFER before the expiration delay.
 *  Retry with Contact header upon reception of 3xx request.
 *  Send automatic UPDATE for session-timer feature.
 *
 * @param excontext    eXosip_t instance.
 */
void eXosip_automatic_action(struct eXosip_t *excontext);

#ifndef MINISIZE
/**
 * Automatic internal handling of dialog package.
 *
 * @param excontext    eXosip_t instance.
 * @param evt          Incoming SUBSCRIBE for dialog package.
 */
int eXosip_insubscription_automatic(struct eXosip_t *excontext, eXosip_event_t *evt);
#endif

/**
 * Generate random string: (only digit, and maximum unsigned int)
 *
 * @param buf         destination buffer for random string.
 * @param buf_size    size of destination buffer
 */
int eXosip_generate_random(char *buf, int buf_size);

/**
 * Generate random string: (low entropy, only hexa)
 *
 * @param buf         destination buffer for random string.
 * @param buf_size    size of destination buffer
 * @param str1        random1 input string
 * @param str2        random2 input string
 * @param str3        random3 input string
 */
int eXosip_hexa_generate_random(char *buf, int buf_size, char *str1, char *str2, char *str3);

/**
 * Generate random string: (high entropy when compiled with openssl)
 *
 * @param buf         destination buffer for random string.
 * @param buf_size    size of destination buffer
 */
int eXosip_byte_generate_random(char *buf, int buf_size);

/** @} */

/**
 * @defgroup eXosip2_sdp eXosip2 SDP helper API.
 * @ingroup eXosip2_msg
 * @{
 */

/**
 * Get remote SDP body for the latest INVITE of call.
 *
 * @param excontext    eXosip_t instance.
 * @param did          dialog id of call.
 */
sdp_message_t *eXosip_get_remote_sdp(struct eXosip_t *excontext, int did);

/**
 * Get local SDP body for the latest INVITE of call.
 *
 * @param excontext    eXosip_t instance.
 * @param did          dialog id of call.
 */
sdp_message_t *eXosip_get_local_sdp(struct eXosip_t *excontext, int did);

/**
 * Get local SDP body for the previous latest INVITE of call.
 *
 * @param excontext    eXosip_t instance.
 * @param did          dialog id of call.
 */
sdp_message_t *eXosip_get_previous_local_sdp(struct eXosip_t *excontext, int did);

/**
 * Get remote SDP body for the latest INVITE of call.
 *
 * @param excontext    eXosip_t instance.
 * @param tid          transction id of transaction.
 */
sdp_message_t *eXosip_get_remote_sdp_from_tid(struct eXosip_t *excontext, int tid);

/**
 * Get local SDP body for the latest INVITE of call.
 *
 * @param excontext    eXosip_t instance.
 * @param tid          transction id of transaction.
 */
sdp_message_t *eXosip_get_local_sdp_from_tid(struct eXosip_t *excontext, int tid);

/**
 * Get local SDP body for the given message.
 *
 * @param message      message containing the SDP.
 */
sdp_message_t *eXosip_get_sdp_info(osip_message_t *message);

/**
 * Get audio connection information for call.
 *
 * @param sdp     sdp information.
 */
sdp_connection_t *eXosip_get_audio_connection(sdp_message_t *sdp);

/**
 * Get audio media information for call.
 *
 * @param sdp     sdp information.
 */
sdp_media_t *eXosip_get_audio_media(sdp_message_t *sdp);

/**
 * Get video connection information for call.
 *
 * @param sdp     sdp information.
 */
sdp_connection_t *eXosip_get_video_connection(sdp_message_t *sdp);

/**
 * Get video media information for call.
 *
 * @param sdp     sdp information.
 */
sdp_media_t *eXosip_get_video_media(sdp_message_t *sdp);

/**
 * Get media connection information for call.
 *
 * @param sdp     sdp information.
 * @param media   media to search.
 */
sdp_connection_t *eXosip_get_connection(sdp_message_t *sdp, const char *media);

/**
 * Get media information for call.
 *
 * @param sdp     sdp information.
 * @param media   media to search.
 */
sdp_media_t *eXosip_get_media(sdp_message_t *sdp, const char *media);

/** @} */

/**
 * @defgroup eXosip2_event eXosip2 event API
 * @ingroup eXosip2_setup
 * @{
 */

/**
 * Structure for event type description
 * @var eXosip_event_type
 */
typedef enum eXosip_event_type {
  /* REGISTER related events */
  EXOSIP_REGISTRATION_SUCCESS, /**< user is successfully registred.  */
  EXOSIP_REGISTRATION_FAILURE, /**< user is not registred.           */

  /* INVITE related events within calls */
  EXOSIP_CALL_INVITE,   /**< announce a new call                   */
  EXOSIP_CALL_REINVITE, /**< announce a new INVITE within call     */

  EXOSIP_CALL_NOANSWER,       /**< announce no answer within the timeout */
  EXOSIP_CALL_PROCEEDING,     /**< announce processing by a remote app   */
  EXOSIP_CALL_RINGING,        /**< announce ringback                     */
  EXOSIP_CALL_ANSWERED,       /**< announce start of call                */
  EXOSIP_CALL_REDIRECTED,     /**< announce a redirection                */
  EXOSIP_CALL_REQUESTFAILURE, /**< announce a request failure            */
  EXOSIP_CALL_SERVERFAILURE,  /**< announce a server failure             */
  EXOSIP_CALL_GLOBALFAILURE,  /**< announce a global failure             */
  EXOSIP_CALL_ACK,            /**< ACK received for 200ok to INVITE      */

  EXOSIP_CALL_CANCELLED, /**< announce that call has been cancelled */

  /* request related events within calls (except INVITE) */
  EXOSIP_CALL_MESSAGE_NEW,            /**< announce new incoming request. */
  EXOSIP_CALL_MESSAGE_PROCEEDING,     /**< announce a 1xx for request. */
  EXOSIP_CALL_MESSAGE_ANSWERED,       /**< announce a 200ok  */
  EXOSIP_CALL_MESSAGE_REDIRECTED,     /**< announce a failure. */
  EXOSIP_CALL_MESSAGE_REQUESTFAILURE, /**< announce a failure. */
  EXOSIP_CALL_MESSAGE_SERVERFAILURE,  /**< announce a failure. */
  EXOSIP_CALL_MESSAGE_GLOBALFAILURE,  /**< announce a failure. */

  EXOSIP_CALL_CLOSED, /**< a BYE was received for this call      */

  /* for both UAS & UAC events */
  EXOSIP_CALL_RELEASED, /**< call context is cleared.            */

  /* events received for request outside calls */
  EXOSIP_MESSAGE_NEW,            /**< announce new incoming request. */
  EXOSIP_MESSAGE_PROCEEDING,     /**< announce a 1xx for request. */
  EXOSIP_MESSAGE_ANSWERED,       /**< announce a 200ok  */
  EXOSIP_MESSAGE_REDIRECTED,     /**< announce a failure. */
  EXOSIP_MESSAGE_REQUESTFAILURE, /**< announce a failure. */
  EXOSIP_MESSAGE_SERVERFAILURE,  /**< announce a failure. */
  EXOSIP_MESSAGE_GLOBALFAILURE,  /**< announce a failure. */

  /* Presence and Instant Messaging */
  EXOSIP_SUBSCRIPTION_NOANSWER,       /**< announce no answer              */
  EXOSIP_SUBSCRIPTION_PROCEEDING,     /**< announce a 1xx                  */
  EXOSIP_SUBSCRIPTION_ANSWERED,       /**< announce a 200ok                */
  EXOSIP_SUBSCRIPTION_REDIRECTED,     /**< announce a redirection          */
  EXOSIP_SUBSCRIPTION_REQUESTFAILURE, /**< announce a request failure      */
  EXOSIP_SUBSCRIPTION_SERVERFAILURE,  /**< announce a server failure       */
  EXOSIP_SUBSCRIPTION_GLOBALFAILURE,  /**< announce a global failure       */
  EXOSIP_SUBSCRIPTION_NOTIFY,         /**< announce new NOTIFY request     */

  EXOSIP_IN_SUBSCRIPTION_NEW, /**< announce new incoming SUBSCRIBE/REFER.*/

  EXOSIP_NOTIFICATION_NOANSWER,       /**< announce no answer              */
  EXOSIP_NOTIFICATION_PROCEEDING,     /**< announce a 1xx                  */
  EXOSIP_NOTIFICATION_ANSWERED,       /**< announce a 200ok                */
  EXOSIP_NOTIFICATION_REDIRECTED,     /**< announce a redirection          */
  EXOSIP_NOTIFICATION_REQUESTFAILURE, /**< announce a request failure      */
  EXOSIP_NOTIFICATION_SERVERFAILURE,  /**< announce a server failure       */
  EXOSIP_NOTIFICATION_GLOBALFAILURE,  /**< announce a global failure       */

  EXOSIP_EVENT_COUNT /**< MAX number of events              */
} eXosip_event_type_t;

/**
 * Structure for event description
 * @struct eXosip_event
 */
struct eXosip_event {
  eXosip_event_type_t type; /**< type of the event */
  char textinfo[256];       /**< text description of event */
  void *external_reference; /**< external reference (for calls) */

  osip_message_t *request;  /**< request within current transaction */
  osip_message_t *response; /**< last response within current transaction */
  osip_message_t *ack;      /**< ack within current transaction */

  int tid; /**< unique id for transactions (to be used for answers) */
  int did; /**< unique id for SIP dialogs */

  int rid; /**< unique id for registration */
  int cid; /**< unique id for SIP calls (but multiple dialogs!) */
  int sid; /**< unique id for outgoing subscriptions */
  int nid; /**< unique id for incoming subscriptions */

  int ss_status; /**< current Subscription-State for subscription */
  int ss_reason; /**< current Reason status for subscription */
};

/**
 * Free ressource in an eXosip event.
 *
 * @param je    event to work on.
 */
void eXosip_event_free(eXosip_event_t *je);

/**
 * Wait for an eXosip event.
 *
 * @param excontext eXosip_t instance.
 * @param tv_s      timeout value (seconds).
 * @param tv_ms     timeout value (mseconds).
 */
eXosip_event_t *eXosip_event_wait(struct eXosip_t *excontext, int tv_s, int tv_ms);

/**
 * Wait for next eXosip event.
 * **DEPRECATED API**
 * This API will block - You should use eXosip_event_wait instead which is more convenient.
 * limitation: This method will not process automatic 200ok retransmission for INVITE transaction.
 *
 * @param excontext    eXosip_t instance.
 */
eXosip_event_t *eXosip_event_get(struct eXosip_t *excontext);

/**
 * This socket receive some data yhen an event happens internally.
 * NOTE: you must call eXosip_event_wait until there is no more events
 * in the fifo.
 *
 * @param excontext    eXosip_t instance.
 */
int eXosip_event_geteventsocket(struct eXosip_t *excontext);

/** @} */

#ifdef __cplusplus
}
#endif
#endif