jabberd2  2.3.1
util.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
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 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, MA02111-1307USA
19  */
20 
21 #define _GNU_SOURCE
22 #include <string.h>
23 
24 #include "s2s.h"
25 
27 char *s2s_route_key(pool_t p, const char *local, const char *remote) {
28  char *key;
29 
30  if(local == NULL) local = "";
31  if(remote == NULL) remote = "";
32 
33  if(p == NULL)
34  key = (char *) malloc(strlen(local) + strlen(remote) + 2);
35  else
36  key = (char *) pmalloc(p, strlen(local) + strlen(remote) + 2);
37 
38  sprintf(key, "%s/%s", local, remote);
39 
40  return key;
41 }
42 
44 int s2s_route_key_match(char *local, const char *remote, const char *rkey, int rkeylen) {
45  char *klocal, *kremote;
46  int ret;
47 
48  klocal = strndup(rkey, rkeylen);
49  kremote = strchr(klocal, '/');
50  if(kremote != NULL) *kremote++ = '\0';
51 
52  ret = (local == NULL || (klocal != NULL && !strcmp(local, klocal)))
53  && (remote == NULL || (kremote != NULL && !strcmp(remote, kremote)));
54 
55  free(klocal);
56 
57  return ret;
58 }
59 
61 char *s2s_db_key(pool_t p, const char *secret, const char *remote, const char *id) {
62  char hash[41], buf[1024];
63 
64  _sx_debug(ZONE, "generating dialback key, secret %s, remote %s, id %s", secret, remote, id);
65 
66  shahash_r(secret, hash);
67 
68  snprintf(buf, 1024, "%s%s", hash, remote);
69  shahash_r(buf, hash);
70 
71  snprintf(buf, 1024, "%s%s", hash, id);
72  shahash_r(buf, hash);
73 
74  _sx_debug(ZONE, "dialback key generated: %s", hash);
75 
76  if(p == NULL)
77  return strdup(hash);
78  else
79  return pstrdup(p, hash);
80 }
void * pmalloc(pool_t p, int size)
Definition: pool.c:141
void shahash_r(const char *str, char hashbuf[41])
convenience (originally by Thomas Muldowney)
Definition: str.c:358
char * s2s_route_key(pool_t p, const char *local, const char *remote)
generate a local/remote route key
Definition: util.c:27
char * s2s_db_key(pool_t p, const char *secret, const char *remote, const char *id)
generate a dialback key
Definition: util.c:61
#define _sx_debug
Definition: sx.h:405
int s2s_route_key_match(char *local, const char *remote, const char *rkey, int rkeylen)
match route key - used for searching route hash
Definition: util.c:44
char * pstrdup(pool_t p, const char *src)
XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is wit...
Definition: pool.c:191
#define ZONE
Definition: mio_impl.h:76
pool - base node for a pool.
Definition: pool.h:80