flat_con.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: flat_con.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Flastore module connection structure
00005  *
00006  * Copyright (C) 2004 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <string.h>
00026 #include <errno.h>
00027 #include "../../mem/mem.h"
00028 #include "../../dprint.h"
00029 #include "../../ut.h"
00030 #include "flatstore_mod.h"
00031 #include "flat_con.h"
00032 
00033 #define FILE_SUFFIX ".log"
00034 #define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
00035 
00036 /* returns a pkg_malloc'ed file name */
00037 static char* get_name(struct flat_id* id)
00038 {
00039    char* buf;
00040    int buf_len;
00041    char* num, *ptr;
00042    int num_len;
00043    int total_len;
00044 
00045    buf_len=pathmax();
00046    if (!id) {
00047       LM_ERR("invalid parameter value\n");
00048       return 0;
00049    }
00050    total_len=id->dir.len+1 /* / */+id->table.len+1 /* _ */+
00051             FILE_SUFFIX_LEN+1 /* \0 */; /* without pid*/
00052    if (buf_len<total_len){
00053       LM_ERR("the path is too long (%d and PATHMAX is %d)\n",
00054                total_len, buf_len);
00055       return 0;
00056    }
00057    
00058    buf=pkg_malloc(buf_len);
00059    if (buf==0){
00060       LM_ERR("pkg memory allocation failure\n");
00061       return 0;
00062    }
00063 
00064    ptr = buf;
00065 
00066    memcpy(ptr, id->dir.s, id->dir.len);
00067    ptr += id->dir.len;
00068    *ptr++ = '/';
00069 
00070    memcpy(ptr, id->table.s, id->table.len);
00071    ptr += id->table.len;
00072 
00073    *ptr++ = '_';
00074    
00075    num = int2str(flat_pid, &num_len);
00076    if (buf_len<(total_len+num_len)){
00077       LM_ERR("the path is too long (%d and PATHMAX is"
00078             " %d)\n", total_len+num_len, buf_len);
00079       pkg_free(buf);
00080       return 0;
00081    }
00082    memcpy(ptr, num, num_len);
00083    ptr += num_len;
00084 
00085    memcpy(ptr, FILE_SUFFIX, FILE_SUFFIX_LEN);
00086    ptr += FILE_SUFFIX_LEN;
00087 
00088    *ptr = '\0';
00089    return buf;
00090 }
00091 
00092 
00093 struct flat_con* flat_new_connection(struct flat_id* id)
00094 {
00095    char* fn;
00096 
00097    struct flat_con* res;
00098 
00099    if (!id) {
00100       LM_ERR("invalid parameter value\n");
00101       return 0;
00102    }
00103 
00104    res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
00105    if (!res) {
00106       LM_ERR("no pkg memory left\n");
00107       return 0;
00108    }
00109 
00110    memset(res, 0, sizeof(struct flat_con));
00111    res->ref = 1;
00112    
00113    res->id = id;
00114 
00115    fn = get_name(id);
00116    if (fn==0){
00117       LM_ERR("get_name() failed\n");
00118       return 0;
00119    }
00120 
00121    res->file = fopen(fn, "a");
00122    pkg_free(fn); /* we don't need fn anymore */
00123    if (!res->file) {
00124       LM_ERR(" %s\n", strerror(errno));
00125       pkg_free(res);
00126       return 0;
00127    }
00128    
00129    return res;
00130 }
00131 
00132 
00133 /*
00134  * Close the connection and release memory
00135  */
00136 void flat_free_connection(struct flat_con* con)
00137 {
00138    if (!con) return;
00139    if (con->id) free_flat_id(con->id);
00140    if (con->file) {
00141       fclose(con->file);
00142    }
00143    pkg_free(con);
00144 }
00145 
00146 
00147 /*
00148  * Reopen a connection
00149  */
00150 int flat_reopen_connection(struct flat_con* con)
00151 {
00152    char* fn;
00153 
00154    if (!con) {
00155       LM_ERR("invalid parameter value\n");
00156       return -1;
00157    }
00158 
00159    if (con->file) {
00160       fclose(con->file);
00161       con->file = 0;
00162 
00163       fn = get_name(con->id);
00164       if (fn == 0) {
00165          LM_ERR("failed to get_name\n");
00166          return -1;
00167       }
00168 
00169       con->file = fopen(fn, "a");
00170       pkg_free(fn);
00171 
00172       if (!con->file) {
00173          LM_ERR("invalid parameter value\n");
00174          return -1;
00175       }
00176    }
00177 
00178    return 0;
00179 }

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6