bdb_uri.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * BDB Database Driver for SIP-router
00005  *
00006  * Copyright (C) 2008 iptelorg GmbH
00007  *
00008  * This file is part of SIP-router, a free SIP server.
00009  *
00010  * SIP-router is free software; you can redistribute it and/or modify it under the
00011  * terms of the GNU General Public License as published by the Free Software
00012  * Foundation; either version 2 of the License, or (at your option) any later
00013  * version.
00014  *
00015  * SIP-router is distributed in the hope that it will be useful, but WITHOUT ANY
00016  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00018  * details.
00019  *
00020  * You should have received a copy of the GNU General Public License along
00021  * with this program; if not, write to the Free Software Foundation, Inc.,
00022  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00023  */
00024 
00036 #include <string.h>
00037 
00038 #include "../../mem/mem.h"
00039 #include "../../ut.h"
00040 
00041 #include "bdb_uri.h"
00042 
00043 #ifndef CFG_DIR
00044 #define CFG_DIR "/tmp"
00045 #endif
00046 
00047 #define BDB_ID          "bdb://"
00048 #define BDB_ID_LEN      (sizeof(BDB_ID)-1)
00049 
00054 #define cmpstr(s1, s2, f) \
00055         ((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
00056 
00057 
00066 static unsigned char bdb_uri_cmp(db_uri_t* uri1, db_uri_t* uri2)
00067 {
00068         bdb_uri_t * buri1, *buri2;
00069 
00070         if (!uri1 || !uri2) return 0;
00071 
00072         buri1 = DB_GET_PAYLOAD(uri1);
00073         buri2 = DB_GET_PAYLOAD(uri2);
00074 
00075         if (cmpstr(buri1->uri, buri2->uri, strcmp))
00076                 return 0;
00077         return 1;
00078 }
00079 
00080 /*
00081  * Parse BDB URI of form
00082  * //path/to/dir
00083  *
00084  * Returns 0 if parsing was successful and -1 otherwise
00085  */
00086 int parse_bdb_uri(bdb_uri_t* res, str* uri)
00087 {
00088         str s;
00089 
00090         if(uri==NULL || uri->s==NULL)
00091                 return -1;
00092 
00093         s = *uri;
00094 
00095         res->uri = (char*)pkg_malloc((s.len+1)*sizeof(char));
00096 
00097         if(res->uri == NULL)
00098         {
00099                 ERR("bdb: no more pkg\n");
00100                 return -1;
00101         }
00102 
00103         memcpy(res->uri, s.s, s.len);
00104         res->uri[s.len] = '\0';
00105 
00106         if(s.s[0]!='/')
00107         {
00108                 res->path.s = (char*)pkg_malloc((sizeof(CFG_DIR)+s.len+2)*sizeof(char));
00109                 memset(res->path.s, 0, (sizeof(CFG_DIR)+s.len+2)*sizeof(char));
00110                 if(res->path.s==NULL)
00111                 {
00112                         ERR("bdb: no more pkg.\n");
00113                         pkg_free(res->uri);
00114                         res->uri = NULL;
00115                         return -1;
00116                 }
00117                 strcpy(res->path.s, CFG_DIR);
00118                 res->path.s[sizeof(CFG_DIR)] = '/';
00119                 strncpy(&res->path.s[sizeof(CFG_DIR)+1], s.s, s.len);
00120                 res->path.len = sizeof(CFG_DIR)+s.len;
00121         } else {
00122                 res->path.s = res->uri;
00123                 res->path.len = strlen(res->path.s);
00124         }
00125         
00126         return 0;
00127 }
00128 
00129 static void bdb_uri_free(db_uri_t* uri, bdb_uri_t* payload)
00130 {
00131         if (payload == NULL) return;
00132         if(payload->path.s && payload->path.s!=payload->uri)
00133                 pkg_free(payload->path.s);
00134         if (payload->uri) pkg_free(payload->uri);
00135         db_drv_free(&payload->drv);
00136         pkg_free(payload);
00137 }
00138 
00139 
00140 int bdb_uri(db_uri_t* uri)
00141 {
00142         bdb_uri_t *buri;
00143 
00144         buri = (bdb_uri_t*)pkg_malloc(sizeof(bdb_uri_t));
00145         if (buri == NULL) {
00146                 ERR("bdb: No memory left\n");
00147                 goto error;
00148         }
00149         memset(buri, '\0', sizeof(bdb_uri_t));
00150         if (db_drv_init(&buri->drv, bdb_uri_free) < 0) goto error;
00151     if (parse_bdb_uri(buri,  &uri->body) < 0) goto error;
00152 
00153         DB_SET_PAYLOAD(uri, buri);
00154         uri->cmp = bdb_uri_cmp;
00155         return 0;
00156 
00157  error:
00158         if (buri) {
00159                 if (buri->uri) pkg_free(buri->uri);
00160                 db_drv_free(&buri->drv);
00161                 pkg_free(buri);
00162         }
00163         return -1;
00164 }
00165 
00166