Commit 72424b26 authored by Pekka Pessi's avatar Pekka Pessi

nua: using global preferences. Added NUTAG_SHUTDOWN_EVENTS().

darcs-hash:20080206170546-65a35-d9826f24242e4916ec27516dd840a7abf389247d.gz
parent 4f7a2ef0
......@@ -77,19 +77,27 @@ su_inline int nhp_is_any_set(nua_handle_preferences_t const *nhp)
su_inline void nhp_or_set(nua_handle_preferences_t *a,
nua_handle_preferences_t const *b)
{
unsigned *ap = a->nhp_set_.set_unsigned;
unsigned const *bp = b->nhp_set_.set_unsigned;
size_t i;
memcpy(a, b, offsetof(nua_handle_preferences_t, nhp_set));
/* Bitwise or of bitfields, casted to unsigned */
a->nhp_set_.set_unsigned[0] |= b->nhp_set_.set_unsigned[0];
a->nhp_set_.set_unsigned[1] |= b->nhp_set_.set_unsigned[1];
/*
unsigned *ap, const *bp;
size_t i;
ap = a->nhp_set_.set_unsigned;
bp = b->nhp_set_.set_unsigned;
for (i = 0; i < (sizeof a->nhp_set); i += (sizeof *ap))
*ap++ |= *bp++;
*/
}
static int nhp_set_tags(su_home_t *home,
nua_handle_preferences_t *nhp,
int global,
nua_global_preferences_t *ngp,
tagi_t const *tags);
static int nhp_merge_lists(su_home_t *home,
......@@ -101,11 +109,11 @@ static int nhp_merge_lists(su_home_t *home,
int always_merge,
tag_value_t value);
static
nua_handle_preferences_t *nhp_move_params(su_home_t *home,
nua_handle_preferences_t *dst,
static int nhp_save_params(nua_handle_t *nh,
su_home_t *tmphome,
nua_handle_preferences_t const *src);
nua_global_preferences_t *gsrc,
nua_handle_preferences_t *src);
/* ====================================================================== */
/* Magical NUTAG_USER_AGENT() - add NHP_USER_AGENT there if it is not there */
......@@ -198,20 +206,12 @@ int nua_stack_set_from(nua_t *nua, int initial, tagi_t const *tags)
sip_from_t *f = NULL, f0[1];
int set;
char const *uicc_name = "default";
tl_gets(tags,
/* By nua_stack_set_from() */
SIPTAG_FROM_REF(from),
SIPTAG_FROM_STR_REF(str),
NUTAG_UICC_REF(uicc_name),
TAG_END());
#if HAVE_UICC_H
if (initial && uicc_name)
nua->nua_uicc = uicc_create(root, uicc_name);
#endif
if (!initial && from == NONE && str == NONE)
return 0;
......@@ -475,81 +475,62 @@ int nua_stack_set_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
tagi_t const *tags)
{
nua_handle_t *dnh = nua->nua_dhandle;
nua_handle_preferences_t tmp[1], *nhp = nh->nh_prefs;
nua_handle_preferences_t const *dnhp = dnh->nh_prefs;
su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };
tagi_t const *ptags;
int error, global;
int status = 900;
char const *phrase = "Error storing parameters";
sip_supported_t const *supported = NULL;
sip_allow_t const *allow = NULL;
sip_allow_events_t const *allow_events = NULL;
sip_allow_t const *appl_method = NULL;
int status;
char const *phrase;
enter;
ptags = !nh->nh_used_ptags ? nh->nh_ptags : NULL;
{
su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };
nua_handle_preferences_t *nhp = nh->nh_prefs;
nua_handle_preferences_t const *dnhp = dnh->nh_prefs;
nua_handle_preferences_t tmp[1];
nua_global_preferences_t gtmp[1], *ngp = NULL;
*tmp = *nhp; NHP_UNSET_ALL(tmp);
/* Supported features, allowed methods and events are merged
with previous ones */
/*
* Supported features, allowed methods and events are merged
* with previous or default settings.
*
* Here we just copy pointers from default settings. However when saving
* settings we have to be extra careful so that we
* 1) zero the pointers if the setting has not been modified
* 2) do not free pointer if the setting has been modified
* See NHP_ZAP_OVERRIDEN() below for gorier details.
*/
if (!NHP_ISSET(nhp, supported))
supported = tmp->nhp_supported = dnhp->nhp_supported;
tmp->nhp_supported = dnhp->nhp_supported;
if (!NHP_ISSET(nhp, allow))
allow = tmp->nhp_allow = dnhp->nhp_allow;
tmp->nhp_allow = dnhp->nhp_allow;
if (!NHP_ISSET(nhp, allow_events))
allow_events = tmp->nhp_allow_events = dnhp->nhp_allow_events;
tmp->nhp_allow_events = dnhp->nhp_allow_events;
if (!NHP_ISSET(nhp, appl_method))
appl_method = tmp->nhp_appl_method = dnhp->nhp_appl_method;
tmp->nhp_appl_method = dnhp->nhp_appl_method;
error = 0;
global = nh == dnh; /* save also stack-specific params */
if (nh == dnh) /* nua_set_params() call, save stack-wide params, too */
ngp = gtmp, *gtmp = *nua->nua_prefs;
/* Set and save parameters to tmp */
if (nhp_set_tags(tmphome, tmp, global, ptags) < 0)
error = 1, phrase = "Error storing default handle parameters";
else if (nhp_set_tags(tmphome, tmp, global, tags) < 0)
error = 1, phrase = "Error storing parameters";
else {
if (NHP_IS_ANY_SET(tmp)) {
if (tmp->nhp_supported == supported)
tmp->nhp_supported = NULL;
if (tmp->nhp_allow == allow)
tmp->nhp_allow = NULL;
if (tmp->nhp_allow_events == allow_events)
tmp->nhp_allow_events = NULL;
if (tmp->nhp_appl_method == appl_method)
tmp->nhp_appl_method = NULL;
/* Move parameters from tmp to nhp (or allocate new nhp) */
if (nh != dnh && nhp == dnh->nh_prefs)
nhp = NULL;
nhp = nhp_move_params(nh->nh_home, nhp, tmphome, tmp);
if (nhp)
nh->nh_prefs = nhp;
if (!nh->nh_used_ptags &&
nhp_set_tags(tmphome, tmp, NULL, nh->nh_ptags) < 0)
status = 900, phrase = "Error storing default handle parameters";
else if (nhp_set_tags(tmphome, tmp, ngp, tags) < 0)
status = 900, phrase = "Error storing parameters";
else if (nhp_save_params(nh, tmphome, ngp, tmp) < 0)
status = 900, phrase = su_strerror(ENOMEM);
else
/* Fail miserably with ENOMEM */
error = 1, status = 900, phrase = su_strerror(ENOMEM);
}
status = 200, phrase = "OK", nh->nh_used_ptags = 1;
if (!error)
nh->nh_used_ptags = 1;
su_home_deinit(tmphome);
}
su_home_deinit(tmphome);
if (status == 200) {
nua_handle_preferences_t const *nhp = nh->nh_prefs;
nua_handle_preferences_t const *dnhp = dnh->nh_prefs;
if (error)
;
else if (!nh->nh_soa && NHP_GET(nhp, dnhp, media_enable)) {
if (!nh->nh_soa && NHP_GET(nhp, dnhp, media_enable)) {
/* Create soa when needed */
char const *soa_name = NHP_GET(nhp, dnhp, soa_name);
......@@ -558,63 +539,60 @@ int nua_stack_set_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
else
nh->nh_soa = soa_create(soa_name, nua->nua_root, nh);
ptags = nh->nh_ptags;
if (!nh->nh_soa)
error = 1, status = 900, phrase = "Error Creating SOA Object";
status = 900, phrase = "Error Creating SOA Object";
else if (soa_set_params(nh->nh_soa, TAG_NEXT(nh->nh_ptags)) < 0)
status = 900, phrase = "Error Setting SOA Parameters";
}
else if (nh->nh_soa && !NHP_GET(nhp, dnhp, media_enable)) {
/* ... destroy soa when not needed */
soa_destroy(nh->nh_soa), nh->nh_soa = NULL;
}
if (!error && nh->nh_soa) {
if ((ptags && soa_set_params(nh->nh_soa, TAG_NEXT(ptags)) < 0) ||
(tags && soa_set_params(nh->nh_soa, TAG_NEXT(tags)) < 0))
error = 1, status = 900, phrase = "Error Setting SOA Parameters";
if (status == 200 && tags && nh->nh_soa &&
soa_set_params(nh->nh_soa, TAG_NEXT(tags)) < 0)
status = 900, phrase = "Error Setting SOA Parameters";
}
if (error || nh != dnh) {
;
}
else if (nua_stack_set_smime_params(nua, tags) < 0) {
error = 1, status = 900, phrase = "Error setting S/MIME parameters";
}
else if (!nua->nua_nta) {
}
if (status == 200 && nh == dnh) {
/* Set stack-specific things below */
else if (nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags)) < 0) {
if (nua_stack_set_smime_params(nua, tags) < 0) {
status = 900, phrase = "Error setting S/MIME parameters";
}
else if (nua->nua_nta && nta_agent_set_params(nua->nua_nta, TAG_NEXT(tags)) < 0) {
status = 900, phrase = "Error setting NTA parameters";
error = 1;
}
else {
nua_stack_set_from(nua, 0, tags);
if (NHP_ISSET(nhp, detect_network_updates))
if (nua->nua_prefs->ngp_detect_network_updates)
nua_stack_launch_network_change_detector(nua);
}
}
if (error) {
if (status != 200) {
if (e == nua_i_none)
SU_DEBUG_1(("nua_set_params(): failed: %s\n", phrase));
return UA_EVENT2(e, status, phrase), -1;
}
else {
if (e == nua_r_set_params)
UA_EVENT2(e, 200, "OK");
UA_EVENT2(e, status, phrase);
return 0;
}
}
/** Parse parameters from tags to @a nhp.
/** Parse parameters from tags to @a nhp or @a ngp.
*
* @param home allocate new values from @a home
* @param nhp structure to store handle preferences
* @param global if true, save also global parameters
* @param ngp structure to store global preferences
* @param tags list of tags to parse
*/
static int nhp_set_tags(su_home_t *home,
nua_handle_preferences_t *nhp,
int global,
nua_global_preferences_t *ngp,
tagi_t const *tags)
{
......@@ -997,7 +975,7 @@ static int nhp_set_tags(su_home_t *home,
NHP_SET_STR(nhp, outbound, value);
}
/* NUTAG_DETECT_NETWORK_UPDATES(detect_network_updates) */
else if (global && tag == nutag_detect_network_updates) {
else if (ngp && tag == nutag_detect_network_updates) {
int detector = (int)value;
if (detector < NUA_NW_DETECT_NOTHING)
......@@ -1005,7 +983,13 @@ static int nhp_set_tags(su_home_t *home,
else if (detector > NUA_NW_DETECT_TRY_FULL)
detector = NUA_NW_DETECT_TRY_FULL;
NHP_SET(nhp, detect_network_updates, detector);
ngp->ngp_detect_network_updates = detector;
ngp->ngp_set.ngp_detect_network_updates = 1;
}
/* NUTAG_SHUTDOWN_EVENTS() */
else if (ngp && tag == nutag_shutdown_events) {
ngp->ngp_shutdown_events = value != 0;
ngp->ngp_set.ngp_shutdown_events = 1;
}
}
......@@ -1078,54 +1062,70 @@ static int nhp_merge_lists(su_home_t *home,
return 1;
}
/** Save parameters in @a gtmp and @a tmp.
*/
static
nua_handle_preferences_t *nhp_move_params(su_home_t *home,
nua_handle_preferences_t *dst,
int nhp_save_params(nua_handle_t *nh,
su_home_t *tmphome,
nua_handle_preferences_t const *src)
nua_global_preferences_t *gsrc,
nua_handle_preferences_t *src)
{
/* Update prefs structure */
nua_handle_preferences_t tbf[1];
su_home_t *home = nh->nh_home;
nua_t *nua = nh->nh_nua;
nua_handle_t *dnh = nua->nua_dhandle;
nua_handle_preferences_t *dst = nh->nh_prefs, old[1];
if (gsrc) {
*nua->nua_prefs = *gsrc; /* No pointers this far */
}
if (dst == NULL)
dst = su_zalloc(home, sizeof *dst);
if (dst == NULL)
return NULL;
if (su_home_move(home, tmphome) < 0)
return NULL;
if (!NHP_IS_ANY_SET(src))
return 0;
*tbf = *dst;
if (nh == dnh || nh->nh_prefs != dnh->nh_prefs) {
dst = nh->nh_prefs, *old = *dst;
assert(dst);
}
else {
dst = su_zalloc(home, sizeof *dst), memset(old, 0, sizeof *old);
if (!dst)
return -1;
}
/* Move allocations from tmphome to home */
su_home_move(nh->nh_home, tmphome);
/* Copy parameters that are set from src to dst */
nhp_or_set(dst, src);
/* Handle pointer items. Free changed ones and zap unset ones. */
#define NHP_ZAP_OVERRIDEN(tbf, nhp, pref) \
(((tbf)->nhp_set.nhb_##pref \
&& (tbf)->nhp_##pref != (nhp)->nhp_##pref \
? su_free(home, (void *)(tbf)->nhp_##pref) : (void)0), \
(void)(!(nhp)->nhp_set.nhb_##pref ? (nhp)->nhp_##pref = NULL : NULL))
NHP_ZAP_OVERRIDEN(tbf, dst, soa_name);
NHP_ZAP_OVERRIDEN(tbf, dst, registrar);
NHP_ZAP_OVERRIDEN(tbf, dst, supported);
NHP_ZAP_OVERRIDEN(tbf, dst, allow);
NHP_ZAP_OVERRIDEN(tbf, dst, user_agent);
NHP_ZAP_OVERRIDEN(tbf, dst, organization);
NHP_ZAP_OVERRIDEN(tbf, dst, instance);
NHP_ZAP_OVERRIDEN(tbf, dst, m_display);
NHP_ZAP_OVERRIDEN(tbf, dst, m_username);
NHP_ZAP_OVERRIDEN(tbf, dst, m_params);
NHP_ZAP_OVERRIDEN(tbf, dst, m_features);
NHP_ZAP_OVERRIDEN(tbf, dst, outbound);
#define NHP_ZAP_OVERRIDEN_HDR(tbf, nhp, pref) \
(((tbf)->nhp_set.nhb_##pref \
&& (tbf)->nhp_##pref != (nhp)->nhp_##pref \
? msg_header_free(home, (void *)(tbf)->nhp_##pref) : (void)0), \
(void)(!(nhp)->nhp_set.nhb_##pref ? (nhp)->nhp_##pref = NULL : NULL))
NHP_ZAP_OVERRIDEN_HDR(tbf, dst, initial_route);
return dst;
/* Note that pointer items where !NHP_ISSET(old, pref) are not freed
(because they were just on loan from the default preference set) */
#define NHP_ZAP_OVERRIDEN(old, dst, free, pref) \
(((NHP_ISSET(old, pref) && \
(old)->nhp_##pref && (old)->nhp_##pref != (dst)->nhp_##pref) \
? (free)(home, (void *)(old)->nhp_##pref) : (void)0), \
(void)(!(dst)->nhp_set.nhb_##pref ? (dst)->nhp_##pref = NULL : NULL))
NHP_ZAP_OVERRIDEN(old, dst, su_free, soa_name);
NHP_ZAP_OVERRIDEN(old, dst, su_free, registrar);
NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, allow);
NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, supported);
NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, allow_events);
NHP_ZAP_OVERRIDEN(old, dst, su_free, user_agent);
NHP_ZAP_OVERRIDEN(old, dst, su_free, organization);
NHP_ZAP_OVERRIDEN(old, dst, su_free, m_display);
NHP_ZAP_OVERRIDEN(old, dst, su_free, m_username);
NHP_ZAP_OVERRIDEN(old, dst, su_free, m_params);
NHP_ZAP_OVERRIDEN(old, dst, su_free, m_features);
NHP_ZAP_OVERRIDEN(old, dst, su_free, instance);
NHP_ZAP_OVERRIDEN(old, dst, su_free, outbound);
NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, appl_method);
NHP_ZAP_OVERRIDEN(old, dst, msg_header_free, initial_route);
nh->nh_prefs = dst;
return 0;
}
static int nua_handle_tags_filter(tagi_t const *f, tagi_t const *t);
......@@ -1515,6 +1515,7 @@ int nua_stack_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
tagi_t const *tags)
{
nua_handle_t *dnh = nua->nua_dhandle;
nua_global_preferences_t const *ngp = nua->nua_prefs;
nua_handle_preferences_t const *nhp = nh->nh_prefs;
tagi_t *lst;
......@@ -1539,8 +1540,6 @@ int nua_stack_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
enter;
su_home_auto(tmphome, sizeof(tmphome));
nta_agent_get_params(nua->nua_nta,
NTATAG_UDP_MTU_REF(udp_mtu),
NTATAG_SIP_T1_REF(sip_t1),
......@@ -1588,6 +1587,8 @@ int nua_stack_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
? sip_##pref##_make(tmphome, (char *)nhp->nhp_##pref) \
: NULL))
su_home_auto(tmphome, sizeof(tmphome));
lst = tl_filtered_tlist
(tmphome, tags,
TAG_IF(has_from, SIPTAG_FROM(from)),
......@@ -1656,11 +1657,18 @@ int nua_stack_get_params(nua_t *nua, nua_handle_t *nh, nua_event_t e,
TIF(NUTAG_M_PARAMS, m_params),
TIF(NUTAG_M_FEATURES, m_features),
TIF(NUTAG_OUTBOUND, outbound),
TIF(NUTAG_DETECT_NETWORK_UPDATES, detect_network_updates),
/* Skip user-agent-level parameters if parameters are for handle only */
TAG_IF(nh != dnh, TAG_NEXT(media_params)),
/* Include tag in the list returned to user
* if it has been earlier set (by user) */
#define GIF(TAG, pref) \
TAG_IF(ngp->ngp_set.ngp_##pref, TAG(ngp->ngp_##pref))
GIF(NUTAG_DETECT_NETWORK_UPDATES, detect_network_updates),
GIF(NUTAG_SHUTDOWN_EVENTS, shutdown_events),
NTATAG_CONTACT(m),
#if HAVE_SOFIA_SMIME
......
......@@ -127,9 +127,6 @@ typedef struct nua_handle_preferences
/** Outbound OPTIONS */
char const *nhp_outbound;
/** Network detection: NONE, INFORMAL, TRY_FULL */
int nhp_detect_network_updates;
sip_allow_t *nhp_appl_method;
/** Initial route set */
......@@ -137,6 +134,12 @@ typedef struct nua_handle_preferences
union { struct {
/* A bit for each feature set by application */
/* NOTE:
Some compilers behave weird if there are bitfields
together with width > 32
So there should be a padding field (unsigned:0;)
every 32 bits.
*/
unsigned nhb_retry_count:1;
unsigned nhb_max_subscriptions:1;
......@@ -172,7 +175,8 @@ typedef struct nua_handle_preferences
unsigned nhb_allow:1;
unsigned nhb_supported:1;
unsigned :0; /* at most 32 bits ... */
unsigned :0; /* at most 32 bits before this point */
unsigned nhb_allow_events:1;
unsigned nhb_user_agent:1;
unsigned nhb_organization:1;
......@@ -183,7 +187,6 @@ typedef struct nua_handle_preferences
unsigned nhb_m_features:1;
unsigned nhb_instance:1;
unsigned nhb_outbound:1;
unsigned nhb_detect_network_updates:1;
unsigned nhb_appl_method:1;
unsigned nhb_initial_route:1;
unsigned :0;
......@@ -194,6 +197,26 @@ typedef struct nua_handle_preferences
#define nhp_set nhp_set_.set_bits
/** Global preferences for nua. */
typedef struct {
/** Network detection: NONE, INFORMAL, TRY_FULL */
signed int ngp_detect_network_updates:3;
/** Pass events during shutdown, too */
int ngp_shutdown_events:1;
unsigned :0; /* pad */
union { struct {
/* A bit for each feature set by application */
unsigned ngp_detect_network_updates:1;
unsigned ngp_shutdown_events:1;
unsigned :0;
} set_bits;
unsigned set_unsigned[2];
} ngp_set_;
} nua_global_preferences_t;
#define ngp_set ngp_set_.set_bits
#define DNHP_GET(dnhp, pref) ((dnhp)->nhp_##pref)
#define NHP_GET(nhp, dnhp, pref) \
......
......@@ -209,7 +209,7 @@ int nua_stack_init(su_root_t *root, nua_t *nua)
if (nua_stack_set_from(nua, 1, nua->nua_args) < 0)
return -1;
if (NHP_ISSET(dnh->nh_prefs, detect_network_updates))
if (nua->nua_prefs->ngp_detect_network_updates)
nua_stack_launch_network_change_detector(nua);
nua_stack_timer(nua, nua->nua_timer, NULL);
......
......@@ -217,28 +217,31 @@ struct nua_s {
su_clone_r nua_clone;
su_task_r nua_client;
su_network_changed_t *nua_nw_changed;
nua_callback_f nua_callback;
nua_magic_t *nua_magic;
nua_event_frame_t *nua_current;
nua_saved_event_t nua_signal[1];
/**< Used by stop-and-wait args calls */
tagi_t const *nua_args;
/* Engine state flags */
sip_time_t nua_shutdown;
unsigned nua_shutdown_started:1; /**< Shutdown initiated */
unsigned nua_shutdown_final:1; /**< Shutdown is complete */
unsigned nua_from_is_set;
unsigned :0;
/**< Used by stop-and-wait args calls */
tagi_t const *nua_args;
/**< Local SIP address. Contents are kept around for ever. */
sip_from_t nua_from[1];
/* ---------------------------------------------------------------------- */
/* Protocol (server) side */
su_network_changed_t *nua_nw_changed;
nua_registration_t *nua_registrations; /**< Active registrations */
......@@ -250,21 +253,8 @@ struct nua_s {
nta_agent_t *nua_nta;
su_timer_t *nua_timer;
void *nua_sip_parser;
sip_time_t nua_shutdown;
/* Route */
sip_service_route_t *nua_service_route;
/* User-agent parameters */
unsigned nua_media_enable:1;
unsigned :0;
#if HAVE_SMIME /* Start NRC Boston */
sm_object_t *sm;
#endif /* End NRC Boston */
nua_global_preferences_t nua_prefs[1];
nua_handle_t *nua_handles;
nua_handle_t **nua_handles_tail;
......
......@@ -267,6 +267,8 @@
* Settings:
* - NUTAG_ALLOW(x), NUTAG_APPL_METHOD(x)
*
* @par Server Shutdown
* - nua_shutdown(), NUTAG_SHUTDOWN_EVENTS(), nua_destroy().
*/
/* @par S/MIME
......@@ -1570,6 +1572,9 @@ extern msg_hclass_t sip_route_class[];
* NUTAG_INITIAL_ROUTE_STR() tags, the route set is constructed from them
* all.
*
* The initial route is inserted into request message before the route
* entries set with SIPTAG_ROUTE() or SIPTAG_ROUTE_STR().
*
* @par Used with
* nua_set_params() \n
* nua_set_hparams() \n
......@@ -2736,6 +2741,39 @@ tag_typedef_t nutag_dialog = UINTTAG_TYPEDEF(dialog);
* Reference tag for NUTAG_SIP_PARSER().
*/
/**@def NUTAG_SHUTDOWN_EVENTS(x)
*
* Allow passing of normal events when stack is being shut down.