modules_s/usrloc/ucontact.h

00001 /* 
00002  * $Id$ 
00003  *
00004  * Usrloc contact structure
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser 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  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  * History:
00030  * ---------
00031  * 2003-03-12 added replication mark and three zombie states (nils)
00032  * 2005-02-25 incoming socket is saved in ucontact record (bogdan)
00033  */
00034 
00035 
00036 #ifndef UCONTACT_H
00037 #define UCONTACT_H
00038 
00039 
00040 #include <stdio.h>
00041 #include <time.h>
00042 #include "../../qvalue.h"
00043 #include "../../str.h"
00044 #include "../../usr_avp.h"
00045 
00046 
00047 typedef enum cstate {
00048         CS_NEW,        /* New contact - not flushed yet */
00049         CS_SYNC,       /* Synchronized contact with the database */
00050         CS_DIRTY       /* Update contact - not flushed yet */
00051 } cstate_t;
00052 
00053 
00054 /*
00055  * Flags that can be associated with a Contact
00056  */
00057 typedef enum flags {
00058         FL_NONE        = 0,          /* No flags set */
00059         FL_NAT         = 1 << 0,     /* Contact is behind NAT */
00060         FL_INVITE      = 1 << 1,     /* Contact supports INVITE and related methods */
00061         FL_N_INVITE    = 1 << 2,     /* Contact doesn't support INVITE and related methods */
00062         FL_MESSAGE     = 1 << 3,     /* Contact supports MESSAGE */
00063         FL_N_MESSAGE   = 1 << 4,     /* Contact doesn't support MESSAGE */
00064         FL_SUBSCRIBE   = 1 << 5,     /* Contact supports SUBSCRIBE and NOTIFY */
00065         FL_N_SUBSCRIBE = 1 << 6,     /* Contact doesn't support SUBSCRIBE and NOTIFY */
00066         FL_PERMANENT   = 1 << 7,     /* Permanent contact (does not expire) */
00067         FL_MEM         = 1 << 8,     /* Update memory only -- used for REGISTER replication */
00068         FL_ALL         = 0xFFFFFFFF  /* All flags set */
00069 } flags_t;
00070 
00071 
00072 typedef struct ucontact {
00073         str* domain;              /* Pointer to domain name */
00074         str* uid;                 /* UID of owner of contact*/
00075         str  aor;                 /* Address of record */
00076         str c;                    /* Contact address */
00077         str received;             /* IP, port, and protocol we received the REGISTER from */
00078         struct socket_info* sock; /* Socket to be used when sending SIP messages to this contact */
00079         time_t expires;           /* expires parameter */
00080         qvalue_t q;               /* q parameter */
00081         str callid;               /* Call-ID header field */
00082         int cseq;                 /* CSeq value */
00083         cstate_t state;           /* State of the contact */
00084         unsigned int flags;       /* Various flags (NAT, supported methods etc) */
00085         str user_agent;           /* User-Agent header field */
00086         str instance;             /* sip.instance parameter */
00087         int server_id;            /* ID of the server within a cluster responsible for the contact */
00088         struct ucontact* next;    /* Next contact in the linked list */
00089         struct ucontact* prev;    /* Previous contact in the linked list */
00090         avp_t *avps;
00091 } ucontact_t;
00092 
00093 
00094 /*
00095  * Valid contact is a contact that either didn't expire yet or is permanent
00096  */
00097 #define VALID_CONTACT(c, t) (((c->expires > t) || (c->flags & FL_PERMANENT)))
00098 
00099 
00100 /*
00101  * Create a new contact structure
00102  */
00103 int new_ucontact(str* _dom, str* _uid, str* aor, str* _contact, time_t _e, qvalue_t _q, 
00104                                  str* _callid, int _cseq, unsigned int _flags, ucontact_t** _c, 
00105                                  str* _ua, str* _recv, struct socket_info* sock, str* _inst, int sid);
00106 
00107 
00108 /*
00109  * Free all memory associated with given contact structure
00110  */
00111 void free_ucontact(ucontact_t* _c);
00112 
00113 
00114 /*
00115  * Print contact, for debugging purposes only
00116  */
00117 void print_ucontact(FILE* _f, ucontact_t* _c);
00118 
00119 
00120 /*
00121  * Update existing contact in memory with new values
00122  */
00123 int mem_update_ucontact(ucontact_t* _c, str* _u, str* aor, time_t _e, qvalue_t _q, str* _cid, int _cs,
00124                         unsigned int _set, unsigned int _res, str* _ua, str* _recv,
00125                         struct socket_info* sock, str* _inst);
00126 
00127 
00128 /* ===== State transition functions - for write back cache scheme ======== */
00129 
00130 
00131 /*
00132  * Update state of the contact if we
00133  * are using write-back scheme
00134  */
00135 void st_update_ucontact(ucontact_t* _c);
00136 
00137 
00138 /*
00139  * Update state of the contact if we
00140  * are using write-back scheme
00141  * Returns 1 if the contact should be
00142  * deleted from memory immediately,
00143  * 0 otherwise
00144  */
00145 int st_delete_ucontact(ucontact_t* _c);
00146 
00147 
00148 /*
00149  * Called when the timer is about to delete
00150  * an expired contact, this routine returns
00151  * 1 if the contact should be removed from
00152  * the database and 0 otherwise
00153  */
00154 int st_expired_ucontact(ucontact_t* _c);
00155 
00156 
00157 /*
00158  * Called when the timer is about flushing the contact,
00159  * updates contact state and returns 1 if the contact
00160  * should be inserted, 2 if updated and 0 otherwise
00161  */
00162 int st_flush_ucontact(ucontact_t* _c);
00163 
00164 
00165 /* ==== Database related functions ====== */
00166 
00167 
00168 /*
00169  * Insert contact into the database
00170  */
00171 int db_store_ucontact(ucontact_t* _c);
00172 
00173 
00174 /*
00175  * Delete contact from the database
00176  */
00177 int db_delete_ucontact(ucontact_t* _c);
00178 
00179 
00180 /* ====== Module interface ====== */
00181 
00182 
00183 /*
00184  * Update ucontact with new values without replication
00185  */
00186 typedef int (*update_ucontact_t)(ucontact_t* _c, str* _u, str* aor, time_t _e,
00187                                                                         qvalue_t _q, str* _cid, int _cs,
00188                                                                  unsigned int _set, unsigned int _reset,
00189                                                                  str* _ua, str* _recv,
00190                                                                  struct socket_info* sock, str* _inst,
00191                                                                  int sid);
00192 int update_ucontact(ucontact_t* _c, str* _u, str* aor, time_t _e, qvalue_t _q,
00193                                                                 str* _cid, int _cs, unsigned int _set,
00194                                                                 unsigned int _reset,
00195                                                                 str* _ua, str* _recv,
00196                                                                 struct socket_info* sock, str* _inst, int sid);
00197 
00198 #endif /* UCONTACT_H */