1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* $Id: flat_con.c,v 1.1.1.1 2005/06/13 16:47:38 bogdan_iancu Exp $
*
* Flastore module connection structure
*
* Copyright (C) 2004 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <errno.h>
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "flatstore_mod.h"
#include "flat_con.h"
#define FILE_SUFFIX ".log"
#define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
/* returns a pkg_malloc'ed file name */
static char* get_name(struct flat_id* id)
{
char* buf;
int buf_len;
char* num, *ptr;
int num_len;
int total_len;
buf_len=pathmax();
if (!id) {
LOG(L_ERR, "get_name: Invalid parameter value\n");
return 0;
}
total_len=id->dir.len+1 /* / */+id->table.len+1 /* _ */+
FILE_SUFFIX_LEN+1 /* \0 */; /* without pid*/
if (buf_len<total_len){
LOG(L_ERR, "get_name: the path is too long (%d and PATHMAX is %d)\n",
total_len, buf_len);
return 0;
}
buf=pkg_malloc(buf_len);
if (buf==0){
LOG(L_ERR, "ERROR: get_name: memory allocation failure\n");
return 0;
}
ptr = buf;
memcpy(ptr, id->dir.s, id->dir.len);
ptr += id->dir.len;
*ptr++ = '/';
memcpy(ptr, id->table.s, id->table.len);
ptr += id->table.len;
*ptr++ = '_';
num = int2str(flat_pid, &num_len);
if (buf_len<(total_len+num_len)){
LOG(L_ERR, "ERROR: get_name: the path is too long (%d and PATHMAX is"
" %d)\n", total_len+num_len, buf_len);
pkg_free(buf);
return 0;
}
memcpy(ptr, num, num_len);
ptr += num_len;
memcpy(ptr, FILE_SUFFIX, FILE_SUFFIX_LEN);
ptr += FILE_SUFFIX_LEN;
*ptr = '\0';
return buf;
}
struct flat_con* flat_new_connection(struct flat_id* id)
{
char* fn;
struct flat_con* res;
if (!id) {
LOG(L_ERR, "flat_new_connection: Invalid parameter value\n");
return 0;
}
res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
if (!res) {
LOG(L_ERR, "flat_new_connection: No memory left\n");
return 0;
}
memset(res, 0, sizeof(struct flat_con));
res->ref = 1;
res->id = id;
fn = get_name(id);
if (fn==0){
LOG(L_ERR, "flat_new_connection: get_name() failed\n");
return 0;
}
res->file = fopen(fn, "a");
pkg_free(fn); /* we don't need fn anymore */
if (!res->file) {
LOG(L_ERR, "flat_new_connection: %s\n", strerror(errno));
pkg_free(res);
return 0;
}
return res;
}
/*
* Close the connection and release memory
*/
void flat_free_connection(struct flat_con* con)
{
if (!con) return;
if (con->id) free_flat_id(con->id);
if (con->file) {
fclose(con->file);
}
pkg_free(con);
}
/*
* Reopen a connection
*/
int flat_reopen_connection(struct flat_con* con)
{
char* fn;
if (!con) {
LOG(L_ERR, "flat_reopen_connection: Invalid parameter value\n");
return -1;
}
if (con->file) {
fclose(con->file);
con->file = 0;
fn = get_name(con->id);
if (fn == 0) {
LOG(L_ERR, "flat_reopen_connection: Error in get_name\n");
return -1;
}
con->file = fopen(fn, "a");
pkg_free(fn);
if (!con->file) {
LOG(L_ERR, "flat_reopen_connection: Invalid parameter value\n");
return -1;
}
}
return 0;
}
|