00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034
00035 #include "xmpp.h"
00036 #include "../../parser/parse_uri.h"
00037 #include "../../parser/parse_param.h"
00038
00039 extern param_t *_xmpp_gwmap_list;
00040
00046 char *decode_uri_sip_xmpp(char *uri)
00047 {
00048 sip_uri_t puri;
00049 static char buf[512];
00050 char *p;
00051 param_t *it = NULL;
00052
00053 if (!uri)
00054 return NULL;
00055 if (parse_uri(uri, strlen(uri), &puri) < 0) {
00056 LM_ERR("failed to parse URI\n");
00057 return NULL;
00058 }
00059 if(_xmpp_gwmap_list==0)
00060 {
00061 strncpy(buf, puri.user.s, sizeof(buf));
00062 buf[puri.user.len] = 0;
00063
00064
00065 if ((p = strchr(buf, domain_separator)))
00066 *p = '@';
00067 } else {
00068 for(it=_xmpp_gwmap_list; it; it=it->next)
00069 {
00070 if(it->name.len==puri.host.len
00071 && strncasecmp(it->name.s, puri.host.s, it->name.len)==0)
00072 {
00073 break;
00074 }
00075 }
00076 if(it && it->body.len>0)
00077 {
00078 snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
00079 it->body.len, it->body.s);
00080 } else {
00081 snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
00082 puri.host.len, puri.host.s);
00083 }
00084 }
00085 return buf;
00086 }
00087
00091 char *encode_uri_sip_xmpp(char *uri)
00092 {
00093 sip_uri_t puri;
00094 static char buf[512];
00095 param_t *it = NULL;
00096
00097 if (!uri)
00098 return NULL;
00099 if (parse_uri(uri, strlen(uri), &puri) < 0) {
00100 LM_ERR("failed to parse URI\n");
00101 return NULL;
00102 }
00103 if(_xmpp_gwmap_list==0)
00104 {
00105 snprintf(buf, sizeof(buf), "%.*s%c%.*s@%s",
00106 puri.user.len, puri.user.s,
00107 domain_separator,
00108 puri.host.len, puri.host.s,
00109 xmpp_domain);
00110 } else {
00111 for(it=_xmpp_gwmap_list; it; it=it->next)
00112 {
00113 if(it->name.len==puri.host.len
00114 && strncasecmp(it->name.s, puri.host.s, it->name.len)==0)
00115 {
00116 break;
00117 }
00118 }
00119 if(it && it->body.len>0)
00120 {
00121 snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
00122 it->body.len, it->body.s);
00123 } else {
00124 snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
00125 puri.host.len, puri.host.s);
00126 }
00127 }
00128 return buf;
00129 }
00130
00134 char *decode_uri_xmpp_sip(char *jid)
00135 {
00136 static char buf[512];
00137 char *p;
00138 char tbuf[512];
00139 sip_uri_t puri;
00140 str sd;
00141 param_t *it = NULL;
00142
00143 if (!jid)
00144 return NULL;
00145
00146 if(_xmpp_gwmap_list==0)
00147 {
00148 snprintf(buf, sizeof(buf), "sip:%s", jid);
00149
00150
00151 if ((p = strchr(buf, '/')))
00152 *p = 0;
00153
00154 if ((p = strchr(buf, '@')))
00155 *p = 0;
00156
00157 if ((p = strchr(buf, domain_separator)))
00158 *p = '@';
00159 } else {
00160 snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
00161
00162
00163 if ((p = strchr(tbuf, '/')))
00164 *p = 0;
00165 if (parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
00166 LM_ERR("failed to parse URI\n");
00167 return NULL;
00168 }
00169 for(it=_xmpp_gwmap_list; it; it=it->next)
00170 {
00171 if(it->body.len>0)
00172 {
00173 sd = it->body;
00174 } else {
00175 sd = it->name;
00176 }
00177 if(sd.len==puri.host.len
00178 && strncasecmp(sd.s, puri.host.s, sd.len)==0)
00179 {
00180 break;
00181 }
00182 }
00183 if(it)
00184 {
00185 snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
00186 it->name.len, it->name.s);
00187 } else {
00188 snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
00189 puri.host.len, puri.host.s);
00190 }
00191
00192 }
00193 return buf;
00194 }
00195
00199 char *encode_uri_xmpp_sip(char *jid)
00200 {
00201 static char buf[512];
00202 char *p;
00203 char tbuf[512];
00204 sip_uri_t puri;
00205 str sd;
00206 param_t *it = NULL;
00207
00208 if (!jid)
00209 return NULL;
00210
00211 if(_xmpp_gwmap_list==0)
00212 {
00213
00214 if ((p = strchr(jid, '/')))
00215 *p = 0;
00216 if ((p = strchr(jid, '@')))
00217 *p = domain_separator;
00218 snprintf(buf, sizeof(buf), "sip:%s@%s", jid, gateway_domain);
00219 } else {
00220 snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
00221
00222
00223 if ((p = strchr(tbuf, '/')))
00224 *p = 0;
00225 if (parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
00226 LM_ERR("failed to parse URI\n");
00227 return NULL;
00228 }
00229 for(it=_xmpp_gwmap_list; it; it=it->next)
00230 {
00231 if(it->body.len>0)
00232 {
00233 sd = it->body;
00234 } else {
00235 sd = it->name;
00236 }
00237 if(sd.len==puri.host.len
00238 && strncasecmp(sd.s, puri.host.s, sd.len)==0)
00239 {
00240 break;
00241 }
00242 }
00243 if(it)
00244 {
00245 snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
00246 it->name.len, it->name.s);
00247 } else {
00248 snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
00249 puri.host.len, puri.host.s);
00250 }
00251
00252 }
00253
00254 return buf;
00255 }
00256
00257 char *extract_domain(char *jid)
00258 {
00259 char *p;
00260
00261 if ((p = strchr(jid, '/')))
00262 *p = 0;
00263 if ((p = strchr(jid, '@'))) {
00264 *p++ = 0;
00265 return p;
00266 }
00267 return p;
00268 }
00269
00270 char *random_secret(void)
00271 {
00272 static char secret[41];
00273 int i, r;
00274
00275 for (i = 0; i < 40; i++) {
00276 r = (int) (36.0 * rand() / RAND_MAX);
00277 secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
00278 }
00279 secret[40] = '\0';
00280
00281 return secret;
00282 }
00283
00284 char *db_key(char *secret, char *domain, char *id)
00285 {
00286 char buf[1024];
00287 char *hash;
00288
00289 snprintf(buf, sizeof(buf), "%s", secret);
00290 hash = shahash(buf);
00291
00292 snprintf(buf, sizeof(buf), "%s%s", hash, domain);
00293 hash = shahash(buf);
00294
00295 snprintf(buf, sizeof(buf), "%s%s", hash, id);
00296 hash = shahash(buf);
00297 return hash;
00298 }
00299