source: mediastreamer2/linphone/coreapi/proxy.c @ 295:7c2d63cfbe15

Last change on this file since 295:7c2d63cfbe15 was 295:7c2d63cfbe15, checked in by smorlat <smorlat@…>, 4 years ago

fix bug when adding contacts
sipsetup plugins work in progress.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@298 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 17.2 KB
Line 
1/*
2linphone
3Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4*/
5/*
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU Library General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 
21#include "linphonecore.h"
22#include "sipsetup.h"
23#include <eXosip2/eXosip.h>
24#include <osipparser2/osip_message.h>
25#include "lpconfig.h"
26#include "private.h"
27
28
29void linphone_proxy_config_init(LinphoneProxyConfig *obj){
30        memset(obj,0,sizeof(LinphoneProxyConfig));
31        obj->rid=-1;
32        obj->expires=3600;
33}
34
35LinphoneProxyConfig *linphone_proxy_config_new(){
36        LinphoneProxyConfig *obj=NULL;
37        obj=ms_new(LinphoneProxyConfig,1);
38        linphone_proxy_config_init(obj);
39        return obj;
40}
41
42void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
43        if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
44        if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
45        if (obj->reg_route!=NULL) ms_free(obj->reg_route);
46        if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
47}
48
49bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
50        return obj->registered;
51}
52
53static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
54        osip_message_t *msg;
55        eXosip_lock();
56        eXosip_register_build_register(obj->rid,obj->expires,&msg);
57        eXosip_register_send_register(obj->rid,msg);
58        eXosip_unlock();
59}
60
61void linphone_proxy_config_register_again_with_updated_contact(LinphoneProxyConfig *obj, osip_message_t *orig_request, osip_message_t *last_answer){
62        osip_message_t *msg;
63        const char *rport,*received;
64        osip_via_t *via=NULL;
65        osip_generic_param_t *param=NULL;
66        osip_contact_t *ctt=NULL;
67        osip_message_get_via(last_answer,0,&via);
68        if (!via) return;
69        osip_via_param_get_byname(via,"rport",&param);
70        if (param) rport=param->gvalue;
71        else return;
72        param=NULL;
73        osip_via_param_get_byname(via,"received",&param);
74        if (param) received=param->gvalue;
75        else return;
76        osip_message_get_contact(orig_request,0,&ctt);
77        if (strcmp(ctt->url->host,received)==0 && (ctt->url->port!=0 && strcmp(ctt->url->port,rport)==0)){
78                ms_message("Register has up to date contact, doing nothing.");
79                return;
80        }
81        eXosip_lock();
82        msg=NULL;
83        eXosip_register_build_register(obj->rid,obj->expires,&msg);
84        osip_message_get_contact(msg,0,&ctt);
85        if (ctt->url->host!=NULL){
86                osip_free(ctt->url->host);
87        }
88        ctt->url->host=osip_strdup(received);
89        if (ctt->url->port!=NULL){
90                osip_free(ctt->url->port);
91        }
92        ctt->url->port=osip_strdup(rport);
93        eXosip_register_send_register(obj->rid,msg);
94        eXosip_unlock();
95        ms_message("Resending new register with updated contact %s:%s",received,rport);
96}
97
98int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
99        int err;
100        osip_from_t *url;
101        if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
102        obj->reg_proxy=NULL;
103        if (server_addr!=NULL && strlen(server_addr)>0){
104                osip_from_init(&url);
105                err=osip_from_parse(url,server_addr);
106                if (err==0 && url->url->host!=NULL){
107                        obj->reg_proxy=ms_strdup(server_addr);
108                }else{
109                        ms_warning("Could not parse %s",server_addr);
110                }
111                osip_from_free(url);
112        }
113        return 0;
114}
115
116void linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
117        int err=0;
118        osip_from_t *url;
119        if (identity!=NULL && strlen(identity)>0){
120                osip_from_init(&url);
121                err=osip_from_parse(url,identity);
122                if (err<0 || url->url->host==NULL || url->url->username==NULL){
123                        ms_warning("Could not parse %s",identity);
124                        osip_from_free(url);
125                        return;
126                }
127                osip_from_free(url);
128        } else err=-2;
129        if (obj->reg_identity!=NULL) {
130                ms_free(obj->reg_identity);
131                obj->reg_identity=NULL;
132        }
133        if (err==-2) obj->reg_identity=NULL;
134        else obj->reg_identity=ms_strdup(identity);
135}
136
137void linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
138{
139        int err;
140        osip_uri_param_t *lr_param=NULL;
141        osip_route_t *rt=NULL;
142        char *tmproute=NULL;
143        if (route!=NULL && strlen(route)>0){
144                osip_route_init(&rt);
145                err=osip_route_parse(rt,route);
146                if (err<0){
147                        ms_warning("Could not parse %s",route);
148                        osip_route_free(rt);
149                        return ;
150                }
151                if (obj->reg_route!=NULL) {
152                        ms_free(obj->reg_route);
153                        obj->reg_route=NULL;
154                }
155                       
156                /* check if the lr parameter is set , if not add it */
157                osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
158                if (lr_param==NULL){
159                        osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
160                        osip_route_to_str(rt,&tmproute);
161                        obj->reg_route=ms_strdup(tmproute);
162                        osip_free(tmproute);
163                }else obj->reg_route=ms_strdup(route);
164        }else{
165                if (obj->reg_route!=NULL) ms_free(obj->reg_route);
166                obj->reg_route=NULL;
167        }
168}
169
170bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
171        if (obj->reg_proxy==NULL){
172                if (lc->vtable.display_warning)
173                        lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
174                                                " followed by a hostname."));
175                return FALSE;
176        }
177        if (obj->reg_identity==NULL){
178                if (lc->vtable.display_warning)
179                        lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
180                                        "sip:username@proxydomain, such as sip:alice@example.net"));
181                return FALSE;
182        }
183        return TRUE;
184}
185
186void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
187        obj->reg_sendregister=val;
188}
189
190void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
191        if (val<=0) val=600;
192        obj->expires=val;
193}
194
195void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
196        obj->publish=val;
197}
198
199void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
200        obj->frozen=TRUE;
201        if (obj->reg_sendregister){
202                /* unregister */
203                if (obj->registered) {
204                        osip_message_t *msg;
205                        eXosip_lock();
206                        eXosip_register_build_register(obj->rid,0,&msg);
207                        eXosip_register_send_register(obj->rid,msg);
208                        eXosip_unlock();
209                        obj->registered=FALSE;
210                }
211        }
212}
213
214void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
215{
216        obj->lc=lc;
217        linphone_proxy_config_done(obj);
218}
219
220int linphone_proxy_config_done(LinphoneProxyConfig *obj)
221{
222        const char *id_str;
223        if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
224        if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
225        else id_str=linphone_core_get_primary_contact(obj->lc);
226        obj->frozen=FALSE;
227        if (obj->reg_sendregister){
228                char *ct=NULL;
229                osip_message_t *msg=NULL;
230                obj->rid=eXosip_register_build_initial_register(id_str,obj->reg_proxy,NULL,obj->expires,&msg);
231                eXosip_register_send_register(obj->rid,msg);
232                if (ct!=NULL) osip_free(ct);
233        }
234        return 0;
235}
236
237void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
238{
239        if (cfg->realm!=NULL) {
240                ms_free(cfg->realm);
241                cfg->realm=NULL;
242        }
243        if (realm!=NULL) cfg->realm=ms_strdup(realm);
244}
245
246int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
247                               LinphoneOnlineStatus presence_mode)
248{
249  osip_message_t *pub;
250  int i;
251  const char *from=NULL;
252  char buf[5000];
253
254  if (proxy->publish==FALSE) return 0;
255       
256  if (proxy!=NULL) {
257    from=linphone_proxy_config_get_identity(proxy);
258  }
259  if (from==NULL) from=linphone_core_get_primary_contact(proxy->lc);
260
261  if (presence_mode==LINPHONE_STATUS_ONLINE)
262    {
263      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
264<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
265          entity=\"%s\">\n\
266<tuple id=\"sg89ae\">\n\
267<status>\n\
268<basic>open</basic>\n\
269</status>\n\
270<contact priority=\"0.8\">%s</contact>\n\
271<note>online</note>\n\
272</tuple>\n\
273</presence>",
274               from, from);
275    }
276  else if (presence_mode==LINPHONE_STATUS_BUSY
277           ||presence_mode==LINPHONE_STATUS_NOT_DISTURB)
278    {
279      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
280<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
281          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
282          entity=\"%s\">\n\
283<tuple id=\"sg89ae\">\n\
284<status>\n\
285<basic>open</basic>\n\
286<es:activities>\n\
287  <es:activity>busy</es:activity>\n\
288</es:activities>\n\
289</status>\n\
290<contact priority=\"0.8\">%s</contact>\n\
291<note>busy</note>\n\
292</tuple>\n\
293</presence>",
294              from, from);
295    }
296  else if (presence_mode==LINPHONE_STATUS_BERIGHTBACK)
297    {
298      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
299<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
300          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
301          entity=\"%s\">\n\
302<tuple id=\"sg89ae\">\n\
303<status>\n\
304<basic>open</basic>\n\
305<es:activities>\n\
306  <es:activity>in-transit</es:activity>\n\
307</es:activities>\n\
308</status>\n\
309<contact priority=\"0.8\">%s</contact>\n\
310<note>be right back</note>\n\
311</tuple>\n\
312</presence>",
313              from,from);
314    }
315  else if (presence_mode==LINPHONE_STATUS_AWAY
316           ||presence_mode==LINPHONE_STATUS_MOVED
317           ||presence_mode==LINPHONE_STATUS_ALT_SERVICE)
318    {
319      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
320<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
321          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
322          entity=\"%s\">\n\
323<tuple id=\"sg89ae\">\n\
324<status>\n\
325<basic>open</basic>\n\
326<es:activities>\n\
327  <es:activity>away</es:activity>\n\
328</es:activities>\n\
329</status>\n\
330<contact priority=\"0.8\">%s</contact>\n\
331<note>away</note>\n\
332</tuple>\n\
333</presence>",
334              from, from);
335    }
336  else if (presence_mode==LINPHONE_STATUS_ONTHEPHONE)
337    {
338      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
339<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
340          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
341          entity=\"%s\">\n\
342<tuple id=\"sg89ae\">\n\
343<status>\n\
344<basic>open</basic>\n\
345<es:activities>\n\
346  <es:activity>on-the-phone</es:activity>\n\
347</es:activities>\n\
348</status>\n\
349<contact priority=\"0.8\">%s</contact>\n\
350<note>on the phone</note>\n\
351</tuple>\n\
352</presence>",
353              from, from);
354    }
355  else if (presence_mode==LINPHONE_STATUS_OUTTOLUNCH)
356    {
357      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
358<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
359          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
360          entity=\"%s\">\n\
361<tuple id=\"sg89ae\">\n\
362<status>\n\
363<basic>open</basic>\n\
364<es:activities>\n\
365  <es:activity>meal</es:activity>\n\
366</es:activities>\n\
367</status>\n\
368<contact priority=\"0.8\">%s</contact>\n\
369<note>out to lunch</note>\n\
370</tuple>\n\
371</presence>",
372              from, from);
373    }
374  else if (presence_mode==LINPHONE_STATUS_OFFLINE)
375    {
376      /* */
377      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
378<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
379xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
380entity=\"%s\">\n%s",
381              from,
382"<tuple id=\"sg89ae\">\n\
383<status>\n\
384<basic>closed</basic>\n\
385<es:activities>\n\
386  <es:activity>permanent-absence</e:activity>\n\
387</es:activities>\n\
388</status>\n\
389</tuple>\n\
390\n</presence>\n");
391    }
392
393  i = eXosip_build_publish(&pub, (char *)from, (char *)from, NULL, "presence", "1800", "application/pidf+xml", buf);
394
395  if (i<0)
396    {
397      ms_message("Failed to build publish request.");
398      return -1;
399    }
400
401  eXosip_lock();
402  i = eXosip_publish(pub, from); /* should update the sip-if-match parameter
403                                    from sip-etag  from last 200ok of PUBLISH */
404  eXosip_unlock();
405  if (i<0)
406    {
407      ms_message("Failed to send publish request.");
408      return -1;
409    }
410  return 0;
411}
412
413int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
414        if (!linphone_proxy_config_check(lc,cfg)) return -1;
415        lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
416        linphone_proxy_config_apply(cfg,lc);
417        return 0;
418}
419
420extern void linphone_friend_check_for_removed_proxy(LinphoneFriend *lf, LinphoneProxyConfig *cfg);
421
422void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
423        MSList *elem;
424        lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
425        /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
426        lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
427        /* this will unREGISTER */
428        linphone_proxy_config_edit(cfg);
429        if (lc->default_proxy==cfg){
430                lc->default_proxy=NULL;
431        }
432        /* invalidate all references to this proxy in our friend list */
433        for (elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
434                linphone_friend_check_for_removed_proxy((LinphoneFriend*)elem->data,cfg);
435        }
436       
437}
438
439void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
440        /* check if this proxy is in our list */
441        if (config!=NULL){
442                if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
443                        ms_warning("Bad proxy address: it is not in the list !");
444                        lc->default_proxy=NULL;
445                        return ;
446                }
447        }
448        lc->default_proxy=config;
449       
450}
451       
452
453void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
454        if (index<0) linphone_core_set_default_proxy(lc,NULL);
455        else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
456}
457
458int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
459        int pos=-1;
460        if (config!=NULL) *config=lc->default_proxy;
461        if (lc->default_proxy!=NULL){
462                pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
463        }
464        return pos;
465}
466
467static int rid_compare(const void *pcfg,const void *prid){
468        const LinphoneProxyConfig *cfg=(const LinphoneProxyConfig*)pcfg;
469        const int *rid=(const int*)prid;
470        ms_message("cfg= %s, cfg->rid=%i, rid=%i",cfg->reg_proxy, cfg->rid, *rid);
471        return cfg->rid-(*rid);
472}
473
474LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid){
475        MSList *elem=ms_list_find_custom(lc->sip_conf.proxies,rid_compare, &rid);
476        if (elem==NULL){
477                ms_message("linphone_core_get_proxy_config_from_rid: searching in deleted proxies...");
478                elem=ms_list_find_custom(lc->sip_conf.deleted_proxies,rid_compare, &rid);
479        }
480        if (elem==NULL) return NULL;
481        else return (LinphoneProxyConfig*)elem->data;
482}
483
484void linphone_core_retry_proxy_register(LinphoneCore *lc, const char *realm)
485{
486        MSList *elem;
487        for (elem=lc->sip_conf.proxies;elem!=NULL;elem=ms_list_next(elem)){
488                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
489                /*ms_message("linphone_core_retry_proxy_register: cfg->auth_pending=%i, cfg->realm=%s, realm=%s",
490                                        cfg->auth_pending,cfg->realm,realm);*/
491                if (cfg->auth_pending && cfg->realm!=NULL && strcmp(cfg->realm,realm)==0){
492                        ms_message("Restarting REGISTER request for %s.",cfg->reg_proxy);
493                        linphone_proxy_config_register(cfg);
494                }
495        }
496}
497
498const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
499        return lc->sip_conf.proxies;
500}
501
502
503void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
504{
505        char key[50];
506
507        sprintf(key,"proxy_%i",index);
508        lp_config_clean_section(config,key);
509        if (obj==NULL){
510                return;
511        }
512        if (obj->type!=NULL){
513                lp_config_set_string(config,key,"type",obj->type);
514        }
515        if (obj->reg_proxy!=NULL){
516                lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
517        }
518        if (obj->reg_route!=NULL){
519                lp_config_set_string(config,key,"reg_route",obj->reg_route);
520        }
521        if (obj->reg_identity!=NULL){
522                lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
523        }
524        lp_config_set_int(config,key,"reg_expires",obj->expires);
525        lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
526        lp_config_set_int(config,key,"publish",obj->publish);
527}
528
529LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
530{
531        const char *tmp;
532        const char *identity;
533        const char *proxy;
534        LinphoneProxyConfig *cfg;
535        char key[50];
536       
537        sprintf(key,"proxy_%i",index);
538
539        if (!lp_config_has_section(config,key)){
540                return NULL;
541        }
542
543        cfg=linphone_proxy_config_new();
544
545        identity=lp_config_get_string(config,key,"reg_identity",NULL); 
546        proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
547       
548        linphone_proxy_config_set_identity(cfg,identity);
549        linphone_proxy_config_set_server_addr(cfg,proxy);
550       
551        tmp=lp_config_get_string(config,key,"reg_route",NULL);
552        if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
553
554        linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
555        linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
556       
557        linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
558       
559        tmp=lp_config_get_string(config,key,"type",NULL);
560        if (tmp!=NULL && strlen(tmp)>0) 
561                linphone_proxy_config_set_sip_setup(cfg,tmp);
562
563        return cfg;
564}
565
566void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
567        SipSetup *ss=sip_setup_lookup(type);
568        SipSetupContext *ssc;
569        if (!ss) return ;
570        ssc=sip_setup_context_new(ss,cfg);
571        if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)==0){
572                char proxy[256];
573                if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
574                        linphone_proxy_config_set_server_addr(cfg,proxy);
575                }
576        }
577        cfg->ssctx=ssc;
578}
579
580SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
581        return cfg->ssctx;
582}
583
584
Note: See TracBrowser for help on using the repository browser.