]> sjero.net Git - linphone/blob - p2pproxy/plugin-src/fonis.c
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / plugin-src / fonis.c
1 /*
2 linphone
3 Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20
21 #include "linphonecore.h"
22
23 #include "p2pproxy.h"
24
25 typedef struct _FonisContext{
26         int toto;
27 }FonisContext;
28
29 static ms_thread_t fonis_thread;
30
31
32 static void *fonis_thread_func(void *arg){
33         char *argv[]={"-edge-only","-sip", "5058",NULL};
34         if (p2pproxy_application_start(3,argv)!=0){
35                 ms_error("Fail to start fonis thread !");
36         }
37         return NULL;
38 }
39
40 static bool_t fonis_init(void){
41         static bool_t initd=FALSE;
42         if (!initd){
43                 ms_thread_create(&fonis_thread,NULL,fonis_thread_func,NULL);
44                 initd=TRUE;
45                 while( p2pproxy_application_get_state()==P2PPROXY_ERROR_APPLICATION_NOT_STARTED){
46                         usleep(200000);
47                         ms_message("Waiting for p2pproxy to start");
48                 }
49         }
50         return TRUE;
51 }
52
53 static bool_t fonis_check_connected(SipSetupContext *ctx){
54         int retries;
55         LinphoneCore *lc=linphone_proxy_config_get_core(sip_setup_context_get_proxy_config(ctx));
56         for(retries=0;retries<1200;retries++){
57                 if (p2pproxy_application_get_state()==P2PPROXY_CONNECTED){
58                         if (retries>0) linphone_core_stop_waiting(lc);
59                         return TRUE;
60                 }
61                 if (retries==0){
62                         ms_message("Waiting for p2pproxy to connect to the network...");
63                         linphone_core_start_waiting(lc,"Trying to connect to the fonis network...");
64                 }else linphone_core_update_progress(lc,NULL,-1);
65         }
66         linphone_core_stop_waiting(lc);
67         return FALSE;
68 }
69
70 static int fonis_test_account(SipSetupContext *ctx, const char *uri){
71         int ret;
72         LinphoneCore *lc=linphone_proxy_config_get_core(sip_setup_context_get_proxy_config(ctx));
73         if (!fonis_check_connected(ctx)) return -1;
74         linphone_core_start_waiting(lc,"Checking...");
75         ret=(p2pproxy_accountmgt_isValidAccount(uri)==P2PPROXY_ACCOUNTMGT_USER_EXIST) ? 1 : 0;
76         linphone_core_update_progress(lc,NULL,1);
77         linphone_core_stop_waiting(lc);
78         return ret;
79 }
80
81 static int fonis_create_account(SipSetupContext *ctx, const char *uri, const char *passwd){
82         int err;
83         LinphoneCore *lc=linphone_proxy_config_get_core(sip_setup_context_get_proxy_config(ctx));
84         if (!fonis_check_connected(ctx)) return -1;
85         linphone_core_start_waiting(lc,"Creating account...");
86         err=p2pproxy_accountmgt_createAccount(uri);
87         ms_message("Account creation result for %s: %i",uri,err);
88         linphone_core_update_progress(lc,NULL,1);
89         linphone_core_stop_waiting(lc);
90         if (err<0) return -1;
91         return 0;
92 }
93
94 static int fonis_login_account(SipSetupContext * ctx,const char *uri, const char *passwd){
95         if (!fonis_check_connected(ctx)) return -1;
96         if (p2pproxy_accountmgt_isValidAccount(uri)==P2PPROXY_ACCOUNTMGT_USER_EXIST) {
97                 return 0;
98         }
99         else return -1;
100 }
101
102 static int fonis_get_proxy(SipSetupContext *ctx, const char *domain, char *proxy, size_t sz){
103         int err;
104         if (!fonis_check_connected(ctx)) return -1;
105         err=p2pproxy_resourcemgt_lookup_sip_proxy(proxy,sz,(char*)domain);
106         if (err==0) return 0;
107         else return -1;
108 }
109
110 static int fonis_get_stun_servers(SipSetupContext *ctx, char *stun1, char *stun2, size_t size){
111         FonisContext *fc=(FonisContext*)ctx->data;
112         int ret=-1;
113         p2pproxy_resourcemgt_resource_list_t* l=p2pproxy_resourcemgt_new_resource_list();
114         if (p2pproxy_resourcemgt_lookup_media_resource(l,ctx->domain)==0){
115                 if (l->size>0) strncpy(stun1,l->resource_uri[0],size);
116                 if (l->size>1) strncpy(stun2,l->resource_uri[1],size);
117                 ret=0;
118         }
119         p2pproxy_resourcemgt_delete_resource_list(l);
120         return ret;
121 }
122
123 static int fonis_get_relay(SipSetupContext *ctx, char *relay, size_t size){
124         FonisContext *fc=(FonisContext*)ctx->data;
125         int ret=-1;
126         p2pproxy_resourcemgt_resource_list_t* l=p2pproxy_resourcemgt_new_resource_list();
127         if (p2pproxy_resourcemgt_lookup_media_resource(l,ctx->domain)==0){
128                 if (l->size>0) strncpy(relay,l->resource_uri[0],size);
129                 ret=0;
130         }
131         p2pproxy_resourcemgt_delete_resource_list(l);
132         return ret;
133 }
134
135 static void fonis_exit(){
136         p2pproxy_application_stop();
137 }
138
139 static const char *fonis_get_notice(SipSetupContext *ssctx){
140         return "<b>WARNING: experimental feature !</b>"
141 "FONIS stands for Free Overlay Network for Instant SIP.\n"
142 "Based on SIP and Peer to Peer technologies, it allows people to find each other through the help of a virtual network."
143 "Once you and your friends are registered, you'll be able to call each other simply by entering your friend's username in "
144 "linphone's sip uri box."
145 "Read more information about FONIS at http://www.fonis.org";
146 }
147
148 static const char *fonis_domains[]={
149         "p2p.linphone.org",
150         NULL
151 };
152
153 static const char ** fonis_get_domains(SipSetupContext *ssctx){
154         return fonis_domains;
155 }
156
157
158
159 static SipSetup fonis_sip_setup={
160         .capabilities=SIP_SETUP_CAP_PROXY_PROVIDER|SIP_SETUP_CAP_STUN_PROVIDER|
161                                 SIP_SETUP_CAP_RELAY_PROVIDER|SIP_SETUP_CAP_ACCOUNT_MANAGER,
162         .name="fonis",
163         .init=fonis_init,
164         .account_exists=fonis_test_account,
165         .create_account=fonis_create_account,
166         .login_account=fonis_login_account,
167         .get_proxy=fonis_get_proxy,
168         .get_stun_servers=fonis_get_stun_servers,
169         .get_relay=fonis_get_relay,
170         .exit=fonis_exit,
171         .get_notice=fonis_get_notice,
172         .get_domains=fonis_get_domains
173 };
174
175 void libfonisprovider_init(void){
176         sip_setup_register(&fonis_sip_setup);
177 }
178