source: mediastreamer2/linphone/coreapi/proxy.c @ 797:204f2e97bc62

Last change on this file since 797:204f2e97bc62 was 797:204f2e97bc62, checked in by smorlat <smorlat@…>, 3 years ago

Add API docs for liblinphone.

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

File size: 24.4 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
28void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
29        MSList *elem;
30        int i;
31        for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
32                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
33                linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
34        }
35}
36
37void linphone_proxy_config_init(LinphoneProxyConfig *obj){
38        memset(obj,0,sizeof(LinphoneProxyConfig));
39        obj->rid=-1;
40        obj->expires=3600;
41}
42
43/**
44 * @addtogroup proxies
45 * @{
46**/
47
48/**
49 * Creates an empty proxy config.
50**/
51LinphoneProxyConfig *linphone_proxy_config_new(){
52        LinphoneProxyConfig *obj=NULL;
53        obj=ms_new(LinphoneProxyConfig,1);
54        linphone_proxy_config_init(obj);
55        return obj;
56}
57
58/**
59 * Destroys a proxy config.
60 *
61 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
62 * linphone_core_remove_proxy_config() must not be freed.
63**/
64void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
65        if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
66        if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
67        if (obj->reg_route!=NULL) ms_free(obj->reg_route);
68        if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
69        if (obj->realm!=NULL) ms_free(obj->realm);
70        if (obj->type!=NULL) ms_free(obj->type);
71        if (obj->contact_addr!=NULL) ms_free(obj->contact_addr);
72}
73
74/**
75 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
76**/
77bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
78        return obj->registered;
79}
80
81void linphone_proxy_config_get_contact(LinphoneProxyConfig *cfg, const char **ip, int *port){
82        if (cfg->registered){
83                *ip=cfg->contact_addr;
84                *port=cfg->contact_port;
85        }else{
86                *ip=NULL;
87                *port=0;
88        }
89}
90
91static void update_contact(LinphoneProxyConfig *cfg, const char *ip, const char *port){
92        if (cfg->contact_addr){
93                ms_free(cfg->contact_addr);
94        }
95        cfg->contact_addr=ms_strdup(ip);
96        if (port!=NULL)
97                cfg->contact_port=atoi(port);
98        else cfg->contact_port=5060;
99}
100
101bool_t linphone_proxy_config_register_again_with_updated_contact(LinphoneProxyConfig *obj, osip_message_t *orig_request, osip_message_t *last_answer){
102        osip_message_t *msg;
103        const char *rport,*received;
104        osip_via_t *via=NULL;
105        osip_generic_param_t *param=NULL;
106        osip_contact_t *ctt=NULL;
107        osip_message_get_via(last_answer,0,&via);
108        if (!via) return FALSE;
109        osip_via_param_get_byname(via,"rport",&param);
110        if (param) rport=param->gvalue;
111        else return FALSE;
112        param=NULL;
113        osip_via_param_get_byname(via,"received",&param);
114        if (param) received=param->gvalue;
115        else return FALSE;
116        osip_message_get_contact(orig_request,0,&ctt);
117        if (strcmp(ctt->url->host,received)==0){
118                /*ip address matches, check ports*/
119                const char *contact_port=ctt->url->port;
120                const char *via_rport=rport;
121                if (via_rport==NULL || strlen(via_rport)>0)
122                        via_rport="5060";
123                if (contact_port==NULL || strlen(contact_port)>0)
124                        contact_port="5060";
125                if (strcmp(contact_port,via_rport)==0){
126                        ms_message("Register has up to date contact, doing nothing.");
127                        return FALSE;
128                }else ms_message("ports do not match, need to update the register (%s <> %s)", contact_port,via_rport);
129        }
130        eXosip_lock();
131        msg=NULL;
132        eXosip_register_build_register(obj->rid,obj->expires,&msg);
133        if (msg==NULL){
134                eXosip_unlock();
135                ms_warning("Fail to create a contact updated register.");
136                return FALSE;
137        }
138        osip_message_get_contact(msg,0,&ctt);
139        if (ctt->url->host!=NULL){
140                osip_free(ctt->url->host);
141        }
142        ctt->url->host=osip_strdup(received);
143        if (ctt->url->port!=NULL){
144                osip_free(ctt->url->port);
145        }
146        ctt->url->port=osip_strdup(rport);
147        eXosip_register_send_register(obj->rid,msg);
148        eXosip_unlock();
149        update_contact(obj,received,rport);
150        ms_message("Resending new register with updated contact %s:%s",received,rport);
151        return TRUE;
152}
153
154/**
155 * Sets the proxy address
156 *
157 * Examples of valid sip proxy address are:
158 * - IP address: sip:87.98.157.38
159 * - IP address with port: sip:87.98.157.38:5062
160 * - hostnames : sip:sip.example.net
161**/
162int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
163        int err;
164        osip_from_t *url;
165        if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
166        obj->reg_proxy=NULL;
167        if (server_addr!=NULL && strlen(server_addr)>0){
168                osip_from_init(&url);
169                err=osip_from_parse(url,server_addr);
170                if (err==0 && url->url->host!=NULL){
171                        obj->reg_proxy=ms_strdup(server_addr);
172                }else{
173                        ms_warning("Could not parse %s",server_addr);
174                }
175                osip_from_free(url);
176        }
177        return 0;
178}
179
180/**
181 * Sets the user identity as a SIP address.
182 *
183 * This identity is normally formed with display name, username and domain, such
184 * as:
185 * Alice <sip:alice@example.net>
186 * The REGISTER messages will have from and to set to this identity.
187 *
188**/
189void linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
190        int err=0;
191        osip_from_t *url=NULL;
192        if (identity!=NULL && strlen(identity)>0){
193                osip_from_init(&url);
194                err=osip_from_parse(url,identity);
195                if (err<0 || url->url->host==NULL || url->url->username==NULL){
196                        ms_warning("Could not parse %s",identity);
197                        osip_from_free(url);
198                        return;
199                }
200        } else err=-2;
201        if (obj->reg_identity!=NULL) {
202                ms_free(obj->reg_identity);
203                obj->reg_identity=NULL;
204        }
205        if (err==-2) obj->reg_identity=NULL;
206        else {
207                obj->reg_identity=ms_strdup(identity);
208                if (obj->realm)
209                        ms_free(obj->realm);
210                obj->realm=ms_strdup(url->url->host);
211        }
212        if (url) osip_from_free(url);
213}
214
215const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
216        return cfg->realm;
217}
218
219/**
220 * Sets a SIP route.
221 * When a route is set, all outgoing calls will go to the route's destination if this proxy
222 * is the default one (see linphone_core_set_default_proxy() ).
223**/
224void linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
225{
226        int err;
227        osip_uri_param_t *lr_param=NULL;
228        osip_route_t *rt=NULL;
229        char *tmproute=NULL;
230        if (route!=NULL && strlen(route)>0){
231                osip_route_init(&rt);
232                err=osip_route_parse(rt,route);
233                if (err<0){
234                        ms_warning("Could not parse %s",route);
235                        osip_route_free(rt);
236                        return ;
237                }
238                if (obj->reg_route!=NULL) {
239                        ms_free(obj->reg_route);
240                        obj->reg_route=NULL;
241                }
242                       
243                /* check if the lr parameter is set , if not add it */
244                osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
245                if (lr_param==NULL){
246                        osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
247                        osip_route_to_str(rt,&tmproute);
248                        obj->reg_route=ms_strdup(tmproute);
249                        osip_free(tmproute);
250                }else obj->reg_route=ms_strdup(route);
251        }else{
252                if (obj->reg_route!=NULL) ms_free(obj->reg_route);
253                obj->reg_route=NULL;
254        }
255}
256
257bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
258        if (obj->reg_proxy==NULL){
259                if (lc->vtable.display_warning)
260                        lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
261                                                " followed by a hostname."));
262                return FALSE;
263        }
264        if (obj->reg_identity==NULL){
265                if (lc->vtable.display_warning)
266                        lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
267                                        "sip:username@proxydomain, such as sip:alice@example.net"));
268                return FALSE;
269        }
270        return TRUE;
271}
272
273/**
274 * Indicates whether a REGISTER request must be sent to the proxy.
275**/
276void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
277        obj->reg_sendregister=val;
278}
279
280/**
281 * Sets the registration expiration time in seconds.
282**/
283void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
284        if (val<=0) val=600;
285        obj->expires=val;
286}
287
288void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
289        obj->publish=val;
290}
291
292/**
293 * Starts editing a proxy configuration.
294 *
295 * Because proxy configuration must be consistent, applications MUST
296 * call linphone_proxy_config_edit() before doing any attempts to modify
297 * proxy configuration (such as identity, proxy address and so on).
298 * Once the modifications are done, then the application must call
299 * linphone_proxy_config_done() to commit the changes.
300**/
301void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
302        obj->auth_failures=0;
303        if (obj->reg_sendregister){
304                /* unregister */
305                if (obj->registered) {
306                        osip_message_t *msg;
307                        eXosip_lock();
308                        eXosip_register_build_register(obj->rid,0,&msg);
309                        eXosip_register_send_register(obj->rid,msg);
310                        eXosip_unlock();
311                        obj->registered=FALSE;
312                }
313        }
314}
315
316void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
317{
318        obj->lc=lc;
319        linphone_proxy_config_done(obj);
320}
321
322static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
323        const char *id_str;
324        if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
325        else id_str=linphone_core_get_primary_contact(obj->lc);
326        if (obj->reg_sendregister){
327                char *ct=NULL;
328                osip_message_t *msg=NULL;
329                eXosip_lock();
330                obj->rid=eXosip_register_build_initial_register(id_str,obj->reg_proxy,NULL,obj->expires,&msg);
331                eXosip_register_send_register(obj->rid,msg);
332                eXosip_unlock();
333                if (ct!=NULL) osip_free(ct);
334        }
335}
336
337/**
338 * Commits modification made to the proxy configuration.
339**/
340int linphone_proxy_config_done(LinphoneProxyConfig *obj)
341{
342        if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
343        obj->commit=TRUE;
344        linphone_proxy_config_write_all_to_config_file(obj->lc);
345        return 0;
346}
347
348void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
349{
350        if (cfg->realm!=NULL) {
351                ms_free(cfg->realm);
352                cfg->realm=NULL;
353        }
354        if (realm!=NULL) cfg->realm=ms_strdup(realm);
355}
356
357int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
358                               LinphoneOnlineStatus presence_mode)
359{
360  osip_message_t *pub;
361  int i;
362  const char *from=NULL;
363  char buf[5000];
364
365  if (proxy->publish==FALSE) return 0;
366       
367  if (proxy!=NULL) {
368    from=linphone_proxy_config_get_identity(proxy);
369  }
370  if (from==NULL) from=linphone_core_get_primary_contact(proxy->lc);
371
372  if (presence_mode==LINPHONE_STATUS_ONLINE)
373    {
374      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
375<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
376          entity=\"%s\">\n\
377<tuple id=\"sg89ae\">\n\
378<status>\n\
379<basic>open</basic>\n\
380</status>\n\
381<contact priority=\"0.8\">%s</contact>\n\
382<note>online</note>\n\
383</tuple>\n\
384</presence>",
385               from, from);
386    }
387  else if (presence_mode==LINPHONE_STATUS_BUSY
388           ||presence_mode==LINPHONE_STATUS_NOT_DISTURB)
389    {
390      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
391<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
392          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
393          entity=\"%s\">\n\
394<tuple id=\"sg89ae\">\n\
395<status>\n\
396<basic>open</basic>\n\
397<es:activities>\n\
398  <es:activity>busy</es:activity>\n\
399</es:activities>\n\
400</status>\n\
401<contact priority=\"0.8\">%s</contact>\n\
402<note>busy</note>\n\
403</tuple>\n\
404</presence>",
405              from, from);
406    }
407  else if (presence_mode==LINPHONE_STATUS_BERIGHTBACK)
408    {
409      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
410<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
411          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
412          entity=\"%s\">\n\
413<tuple id=\"sg89ae\">\n\
414<status>\n\
415<basic>open</basic>\n\
416<es:activities>\n\
417  <es:activity>in-transit</es:activity>\n\
418</es:activities>\n\
419</status>\n\
420<contact priority=\"0.8\">%s</contact>\n\
421<note>be right back</note>\n\
422</tuple>\n\
423</presence>",
424              from,from);
425    }
426  else if (presence_mode==LINPHONE_STATUS_AWAY
427           ||presence_mode==LINPHONE_STATUS_MOVED
428           ||presence_mode==LINPHONE_STATUS_ALT_SERVICE)
429    {
430      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
431<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
432          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
433          entity=\"%s\">\n\
434<tuple id=\"sg89ae\">\n\
435<status>\n\
436<basic>open</basic>\n\
437<es:activities>\n\
438  <es:activity>away</es:activity>\n\
439</es:activities>\n\
440</status>\n\
441<contact priority=\"0.8\">%s</contact>\n\
442<note>away</note>\n\
443</tuple>\n\
444</presence>",
445              from, from);
446    }
447  else if (presence_mode==LINPHONE_STATUS_ONTHEPHONE)
448    {
449      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
450<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
451          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
452          entity=\"%s\">\n\
453<tuple id=\"sg89ae\">\n\
454<status>\n\
455<basic>open</basic>\n\
456<es:activities>\n\
457  <es:activity>on-the-phone</es:activity>\n\
458</es:activities>\n\
459</status>\n\
460<contact priority=\"0.8\">%s</contact>\n\
461<note>on the phone</note>\n\
462</tuple>\n\
463</presence>",
464              from, from);
465    }
466  else if (presence_mode==LINPHONE_STATUS_OUTTOLUNCH)
467    {
468      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
469<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
470          xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
471          entity=\"%s\">\n\
472<tuple id=\"sg89ae\">\n\
473<status>\n\
474<basic>open</basic>\n\
475<es:activities>\n\
476  <es:activity>meal</es:activity>\n\
477</es:activities>\n\
478</status>\n\
479<contact priority=\"0.8\">%s</contact>\n\
480<note>out to lunch</note>\n\
481</tuple>\n\
482</presence>",
483              from, from);
484    }
485  else if (presence_mode==LINPHONE_STATUS_OFFLINE)
486    {
487      /* */
488      snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
489<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
490xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
491entity=\"%s\">\n%s",
492              from,
493"<tuple id=\"sg89ae\">\n\
494<status>\n\
495<basic>closed</basic>\n\
496<es:activities>\n\
497  <es:activity>permanent-absence</e:activity>\n\
498</es:activities>\n\
499</status>\n\
500</tuple>\n\
501\n</presence>\n");
502    }
503
504  i = eXosip_build_publish(&pub, (char *)from, (char *)from, NULL, "presence", "1800", "application/pidf+xml", buf);
505
506  if (i<0)
507    {
508      ms_message("Failed to build publish request.");
509      return -1;
510    }
511
512  eXosip_lock();
513  i = eXosip_publish(pub, from); /* should update the sip-if-match parameter
514                                    from sip-etag  from last 200ok of PUBLISH */
515  eXosip_unlock();
516  if (i<0)
517    {
518      ms_message("Failed to send publish request.");
519      return -1;
520    }
521  return 0;
522}
523
524
525/**
526 * Add a proxy configuration.
527 * This will start registration on the proxy, if registration is enabled.
528**/
529int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
530        if (!linphone_proxy_config_check(lc,cfg)) return -1;
531        lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
532        linphone_proxy_config_apply(cfg,lc);
533        return 0;
534}
535
536extern void linphone_friend_check_for_removed_proxy(LinphoneFriend *lf, LinphoneProxyConfig *cfg);
537
538/**
539 * Removes a proxy configuration.
540 *
541 * LinphoneCore will then automatically unregister and place the proxy configuration
542 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
543**/
544void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
545        MSList *elem;
546        lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
547        /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
548        lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
549        /* this will unREGISTER */
550        linphone_proxy_config_edit(cfg);
551        if (lc->default_proxy==cfg){
552                lc->default_proxy=NULL;
553        }
554        /* invalidate all references to this proxy in our friend list */
555        for (elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
556                linphone_friend_check_for_removed_proxy((LinphoneFriend*)elem->data,cfg);
557        }
558       
559}
560
561/**
562 * Sets the default proxy.
563 *
564 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
565 * Toggling it as default will make LinphoneCore use the identity associated with
566 * the proxy configuration in all incoming and outgoing calls.
567**/
568void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
569        /* check if this proxy is in our list */
570        if (config!=NULL){
571                if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
572                        ms_warning("Bad proxy address: it is not in the list !");
573                        lc->default_proxy=NULL;
574                        return ;
575                }
576        }
577        lc->default_proxy=config;
578       
579}       
580
581void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
582        if (index<0) linphone_core_set_default_proxy(lc,NULL);
583        else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
584}
585
586/**
587 * Returns the default proxy configuration, that is the one used to determine the current identity.
588**/
589int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
590        int pos=-1;
591        if (config!=NULL) *config=lc->default_proxy;
592        if (lc->default_proxy!=NULL){
593                pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
594        }
595        return pos;
596}
597
598static int rid_compare(const void *pcfg,const void *prid){
599        const LinphoneProxyConfig *cfg=(const LinphoneProxyConfig*)pcfg;
600        const int *rid=(const int*)prid;
601        ms_message("cfg= %s, cfg->rid=%i, rid=%i",cfg->reg_proxy, cfg->rid, *rid);
602        return cfg->rid-(*rid);
603}
604
605LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid){
606        MSList *elem=ms_list_find_custom(lc->sip_conf.proxies,rid_compare, &rid);
607        if (elem==NULL){
608                ms_message("linphone_core_get_proxy_config_from_rid: searching in deleted proxies...");
609                elem=ms_list_find_custom(lc->sip_conf.deleted_proxies,rid_compare, &rid);
610        }
611        if (elem==NULL) return NULL;
612        else return (LinphoneProxyConfig*)elem->data;
613}
614
615/**
616 * Returns an unmodifiable list of entered proxy configurations.
617**/
618const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
619        return lc->sip_conf.proxies;
620}
621
622
623void linphone_proxy_config_process_authentication_failure(LinphoneCore *lc, eXosip_event_t *ev){
624        LinphoneProxyConfig *cfg=linphone_core_get_proxy_config_from_rid(lc, ev->rid);
625        if (cfg){
626                cfg->auth_failures++;
627                /*restart a new register so that the user gets a chance to be prompted for a password*/
628                if (cfg->auth_failures==1){
629                        linphone_proxy_config_register(cfg);
630                }
631        }
632}
633
634void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
635{
636        char key[50];
637
638        sprintf(key,"proxy_%i",index);
639        lp_config_clean_section(config,key);
640        if (obj==NULL){
641                return;
642        }
643        if (obj->type!=NULL){
644                lp_config_set_string(config,key,"type",obj->type);
645        }
646        if (obj->reg_proxy!=NULL){
647                lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
648        }
649        if (obj->reg_route!=NULL){
650                lp_config_set_string(config,key,"reg_route",obj->reg_route);
651        }
652        if (obj->reg_identity!=NULL){
653                lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
654        }
655        lp_config_set_int(config,key,"reg_expires",obj->expires);
656        lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
657        lp_config_set_int(config,key,"publish",obj->publish);
658}
659
660
661
662LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
663{
664        const char *tmp;
665        const char *identity;
666        const char *proxy;
667        LinphoneProxyConfig *cfg;
668        char key[50];
669       
670        sprintf(key,"proxy_%i",index);
671
672        if (!lp_config_has_section(config,key)){
673                return NULL;
674        }
675
676        cfg=linphone_proxy_config_new();
677
678        identity=lp_config_get_string(config,key,"reg_identity",NULL); 
679        proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
680       
681        linphone_proxy_config_set_identity(cfg,identity);
682        linphone_proxy_config_set_server_addr(cfg,proxy);
683       
684        tmp=lp_config_get_string(config,key,"reg_route",NULL);
685        if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
686
687        linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
688        linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
689       
690        linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
691       
692        tmp=lp_config_get_string(config,key,"type",NULL);
693        if (tmp!=NULL && strlen(tmp)>0) 
694                linphone_proxy_config_set_sip_setup(cfg,tmp);
695
696        return cfg;
697}
698
699static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
700        SipSetupContext *ssc;
701        SipSetup *ss=sip_setup_lookup(cfg->type);
702        LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
703        unsigned int caps;
704        if (!ss) return ;
705        ssc=sip_setup_context_new(ss,cfg);
706        cfg->ssctx=ssc;
707        if (cfg->reg_identity==NULL){
708                ms_error("Invalid identity for this proxy configuration.");
709                return;
710        }
711        caps=sip_setup_context_get_capabilities(ssc);
712        if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
713                if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
714                        if (lc->vtable.display_warning){
715                                char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
716                                lc->vtable.display_warning(lc,tmp);
717                                ms_free(tmp);
718                        }
719                        return;
720                }
721        }
722        if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
723                char proxy[256];
724                if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
725                        linphone_proxy_config_set_server_addr(cfg,proxy);
726                }else{
727                        ms_error("Could not retrieve proxy uri !");
728                }
729        }
730       
731}
732
733SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
734        if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
735        if (cfg->type!=NULL){
736                return sip_setup_lookup(cfg->type);
737        }
738        return NULL;
739}
740
741void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
742        if (cfg->commit){
743                if (cfg->type && cfg->ssctx==NULL){
744                        linphone_proxy_config_activate_sip_setup(cfg);
745                }
746                linphone_proxy_config_register(cfg);
747                cfg->commit=FALSE;
748        }
749}
750
751void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
752        if (cfg->type)
753                ms_free(cfg->type);
754        cfg->type=ms_strdup(type);
755        if (linphone_proxy_config_get_addr(cfg)==NULL){
756                /*put a placeholder so that the sip setup gets saved into the config */
757                linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
758        }
759}
760
761SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
762        return cfg->ssctx;
763}
764
765/**
766 * @}
767**/
768
769LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
770        LinphoneAccountCreator *obj;
771        LinphoneProxyConfig *cfg;
772        SipSetup *ss=sip_setup_lookup(type);
773        SipSetupContext *ssctx;
774        if (!ss){
775                return NULL;
776        }
777        if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
778                ms_error("%s cannot manage accounts.");
779                return NULL;
780        }
781        obj=ms_new0(LinphoneAccountCreator,1);
782        cfg=linphone_proxy_config_new();
783        ssctx=sip_setup_context_new(ss,cfg);
784        obj->lc=core;
785        obj->ssctx=ssctx;
786        set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
787        cfg->lc=core;
788        return obj;
789}
790
791void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
792        set_string(&obj->username,username);
793}
794
795void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
796        set_string(&obj->password,password);
797}
798
799void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
800        set_string(&obj->domain,domain);
801}
802
803const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
804        return obj->username;
805}
806
807const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
808        return obj->domain;
809}
810
811int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
812        SipSetupContext *ssctx=obj->ssctx;
813        char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
814        int err=sip_setup_context_account_exists(ssctx,uri);
815        ms_free(uri);
816        return err;
817}
818
819LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
820        SipSetupContext *ssctx=obj->ssctx;
821        char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
822        int err=sip_setup_context_create_account(ssctx,uri,obj->password);
823        ms_free(uri);
824        if (err==0) {
825                obj->succeeded=TRUE;
826                return sip_setup_context_get_proxy_config(ssctx);
827        }
828        return NULL;
829}
830
831void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
832        if (obj->username)
833                ms_free(obj->username);
834        if (obj->password)
835                ms_free(obj->password);
836        if (obj->domain)
837                ms_free(obj->domain);
838        if (!obj->succeeded){
839                linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
840        }
841}
842
843
844
Note: See TracBrowser for help on using the repository browser.