]> sjero.net Git - linphone/blob - coreapi/help/registration.c
Aac-eld add missing header according to RFC3640 3.3.6
[linphone] / coreapi / help / registration.c
1
2 /*
3 linphone
4 Copyright (C) 2010  Belledonne Communications SARL 
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (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 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 /**
22  * @defgroup registration_tutorials Basic registration
23  * @ingroup tutorials
24  *This program is a _very_ simple usage example of liblinphone.
25  *Desmonstrating how to  initiate a SIP registration from a sip uri identity passed from the command line.
26  *first argument must be like sip:jehan@sip.linphone.org , second must be password .
27  *<br>
28  *ex registration sip:jehan@sip.linphone.org secret
29  *<br>Registration is cleared on SIGINT
30  *<br>
31  *@include registration.c
32
33  *
34  */
35
36 #ifdef IN_LINPHONE
37 #include "linphonecore.h"
38 #else
39 #include "linphone/linphonecore.h"
40 #endif
41
42 #include <signal.h>
43
44 static bool_t running=TRUE;
45
46 static void stop(int signum){
47         running=FALSE;
48 }
49
50 /**
51  * Registration state notification callback
52  */
53 static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
54                 printf("New registration state %s for user id [%s] at proxy [%s]\n"
55                                 ,linphone_registration_state_to_string(cstate)
56                                 ,linphone_proxy_config_get_identity(cfg)
57                                 ,linphone_proxy_config_get_addr(cfg));
58 }
59
60 LinphoneCore *lc;
61 int main(int argc, char *argv[]){
62         LinphoneCoreVTable vtable={0};
63
64         char* identity=NULL;
65         char* password=NULL;
66
67         /* takes   sip uri  identity from the command line arguments */
68         if (argc>1){
69                 identity=argv[1];
70         }
71
72         /* takes   password from the command line arguments */
73         if (argc>2){
74                 password=argv[2];
75         }
76
77         signal(SIGINT,stop);
78
79 #ifdef DEBUG
80         linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
81 #endif
82         /* 
83          Fill the LinphoneCoreVTable with application callbacks.
84          All are optional. Here we only use the registration_state_changed callbacks
85          in order to get notifications about the progress of the registration.
86          */
87         vtable.registration_state_changed=registration_state_changed;
88
89         /*
90          Instanciate a LinphoneCore object given the LinphoneCoreVTable
91         */
92         lc=linphone_core_new(&vtable,NULL,NULL,NULL);
93
94         LinphoneProxyConfig* proxy_cfg;
95         /*create proxy config*/
96         proxy_cfg = linphone_proxy_config_new();
97         /*parse identity*/
98         LinphoneAddress *from = linphone_address_new(identity);
99         if (from==NULL){
100                 printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
101                 goto end;
102         }
103                 LinphoneAuthInfo *info;
104                 if (password!=NULL){
105                         info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL); /*create authentication structure from identity*/
106                         linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
107                 }
108
109                 // configure proxy entries
110                 linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
111                 const char* server_addr = linphone_address_get_domain(from); /*extract domain address from identity*/
112                 linphone_proxy_config_set_server_addr(proxy_cfg,server_addr); /* we assume domain = proxy server address*/
113                 linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
114                 linphone_address_destroy(from); /*release resource*/
115
116                 linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
117                 linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
118
119
120         /* main loop for receiving notifications and doing background linphonecore work: */
121         while(running){
122                 linphone_core_iterate(lc); /* first iterate initiates registration */
123                 ms_usleep(50000);
124         }
125
126         linphone_core_get_default_proxy(lc,&proxy_cfg); /* get default proxy config*/
127         linphone_proxy_config_edit(proxy_cfg); /*start editing proxy configuration*/
128         linphone_proxy_config_enable_register(proxy_cfg,FALSE); /*de-activate registration for this proxy config*/
129         linphone_proxy_config_done(proxy_cfg); /*initiate REGISTER with expire = 0*/
130
131         while(linphone_proxy_config_get_state(proxy_cfg) !=  LinphoneRegistrationCleared){
132                 linphone_core_iterate(lc); /*to make sure we receive call backs before shutting down*/
133                 ms_usleep(50000);
134         }
135
136 end:
137         printf("Shutting down...\n");
138         linphone_core_destroy(lc);
139         printf("Exited\n");
140         return 0;
141 }
142