tls_util.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * TLS module - common functions
00005  *
00006  * Copyright (C) 2001-2003 FhG FOKUS
00007  * Copyright (C) 2004,2005 Free Software Foundation, Inc.
00008  * Copyright (C) 2005 iptelorg GmbH
00009  *
00010  * This file is part of SIP-router, a free SIP server.
00011  *
00012  * SIP-router is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version
00016  *
00017  * SIP-router is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  */
00026 
00027 #define _GNU_SOURCE 1 /* Needed for strndup */
00028 
00029 #include <string.h>
00030 #include <libgen.h>
00031 #include "../../mem/shm_mem.h"
00032 #include "../../globals.h"
00033 #include "tls_mod.h"
00034 #include "tls_util.h"
00044 /*
00045  * Make a shared memory copy of ASCII zero terminated string
00046  * Return value: -1 on error
00047  *                0 on success
00048  */
00049 int shm_asciiz_dup(char** dest, char* val)
00050 {
00051         char* ret;
00052         int len;
00053 
00054         if (!val) {
00055                 *dest = NULL;
00056                 return 0;
00057         }
00058 
00059         len = strlen(val);
00060         ret = shm_malloc(len + 1);
00061         if (!ret) {
00062                 ERR("No memory left\n");
00063                 return -1;
00064         }
00065         memcpy(ret, val, len + 1);
00066         *dest = ret;
00067         return 0;
00068 }
00069 
00070 
00071 /*
00072  * Delete old TLS configuration that is not needed anymore
00073  */
00074 void collect_garbage(void)
00075 {
00076         tls_domains_cfg_t* prev, *cur;
00077 
00078              /* Make sure we do not run two garbage collectors
00079               * at the same time
00080               */
00081         lock_get(tls_domains_cfg_lock);
00082 
00083              /* Skip the current configuration, garbage starts
00084               * with the 2nd element on the list
00085               */
00086         prev = *tls_domains_cfg;
00087         cur = (*tls_domains_cfg)->next;
00088 
00089         while(cur) {
00090                 if (cur->ref_count == 0) {
00091                              /* Not referenced by any existing connection */
00092                         prev->next = cur->next;
00093                         tls_free_cfg(cur);
00094                 }
00095 
00096                 prev = cur;
00097                 cur = cur->next;
00098         }
00099 
00100         lock_release(tls_domains_cfg_lock);
00101 }
00102