util.c
Go to the documentation of this file.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
00026
00027
00028
00029
00030
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034
00035 #include "xmpp.h"
00036 #include "../../parser/parse_uri.h"
00037
00038
00039
00040
00041
00042 char *decode_uri_sip_xmpp(char *uri)
00043 {
00044 struct sip_uri puri;
00045 static char buf[512];
00046 char *p;
00047
00048 if (!uri)
00049 return NULL;
00050 if (parse_uri(uri, strlen(uri), &puri) < 0) {
00051 LM_ERR("failed to parse URI\n");
00052 return NULL;
00053 }
00054 strncpy(buf, puri.user.s, sizeof(buf));
00055 buf[puri.user.len] = 0;
00056
00057
00058 if ((p = strchr(buf, domain_separator)))
00059 *p = '@';
00060
00061 return buf;
00062 }
00063
00064
00065 char *encode_uri_sip_xmpp(char *uri)
00066 {
00067 struct sip_uri puri;
00068 static char buf[512];
00069
00070 if (!uri)
00071 return NULL;
00072 if (parse_uri(uri, strlen(uri), &puri) < 0) {
00073 LM_ERR("failed to parse URI\n");
00074 return NULL;
00075 }
00076 snprintf(buf, sizeof(buf), "%.*s%c%.*s@%s",
00077 puri.user.len, puri.user.s,
00078 domain_separator,
00079 puri.host.len, puri.host.s,
00080 xmpp_domain);
00081 return buf;
00082 }
00083
00084
00085 char *decode_uri_xmpp_sip(char *jid)
00086 {
00087 static char buf[512];
00088 char *p;
00089
00090 if (!jid)
00091 return NULL;
00092 snprintf(buf, sizeof(buf), "sip:%s", jid);
00093
00094
00095 if ((p = strchr(buf, '/')))
00096 *p = 0;
00097
00098 if ((p = strchr(buf, '@')))
00099 *p = 0;
00100
00101 if ((p = strchr(buf, domain_separator)))
00102 *p = '@';
00103
00104 return buf;
00105 }
00106
00107
00108 char *encode_uri_xmpp_sip(char *jid)
00109 {
00110 static char buf[512];
00111 char *p;
00112
00113 if (!jid)
00114 return NULL;
00115
00116 if ((p = strchr(jid, '/')))
00117 *p = 0;
00118 if ((p = strchr(jid, '@')))
00119 *p = domain_separator;
00120 snprintf(buf, sizeof(buf), "sip:%s@%s", jid, gateway_domain);
00121 return buf;
00122 }
00123
00124 char *extract_domain(char *jid)
00125 {
00126 char *p;
00127
00128 if ((p = strchr(jid, '/')))
00129 *p = 0;
00130 if ((p = strchr(jid, '@'))) {
00131 *p++ = 0;
00132 return p;
00133 }
00134 return p;
00135 }
00136
00137 char *random_secret(void)
00138 {
00139 static char secret[41];
00140 int i, r;
00141
00142 for (i = 0; i < 40; i++) {
00143 r = (int) (36.0 * rand() / RAND_MAX);
00144 secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
00145 }
00146 secret[40] = '\0';
00147
00148 return secret;
00149 }
00150
00151 char *db_key(char *secret, char *domain, char *id)
00152 {
00153 char buf[1024];
00154 char *hash;
00155
00156 snprintf(buf, sizeof(buf), "%s", secret);
00157 hash = shahash(buf);
00158
00159 snprintf(buf, sizeof(buf), "%s%s", hash, domain);
00160 hash = shahash(buf);
00161
00162 snprintf(buf, sizeof(buf), "%s%s", hash, id);
00163 hash = shahash(buf);
00164 return hash;
00165 }
00166