Jan Janak 20 gadi atpakaļ
vecāks
revīzija
48d2c4c874

+ 0 - 158
modules/db_mysql/my_id.c

@@ -1,158 +0,0 @@
-/* 
- * $Id$
- *
- *
- * Copyright (C) 2001-2004 iptel.org
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser 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 <stdlib.h>
-#include <string.h>
-#include "../../dprint.h"
-#include "../../mem/mem.h"
-#include "my_id.h"
-#include "utils.h"
-
-
-/*
- * Create a new connection identifier
- */
-struct my_id* new_my_id(const char* url)
-{
-	char* buf, *username, *password, *host, *port, *database;
-	int l;
-	struct my_id* ptr;
-
-	if (!url) {
-		LOG(L_ERR, "new_my_id: Invalid parameter\n");
-		return 0;
-	}
-
-	     /* Make a scratch-pad copy of the url */
-	l = strlen(url);
-	buf = (char*)pkg_malloc(l + 1);
-	if (!buf) {
-		LOG(L_ERR, "new_my_id: Not enough memory\n");
-		return 0;
-	}
-	memcpy(buf, url, l + 1);
-
-	ptr = (struct my_id*)pkg_malloc(sizeof(struct my_id));
-	if (!ptr) {
-		LOG(L_ERR, "new_my_id: No memory left\n");
-		goto err;
-	}
-	memset(ptr, 0, sizeof(struct my_id));
-
-	if (parse_mysql_url(buf, &username, &password, &host, &port, &database) < 0) {
-		LOG(L_ERR, "new_my_id: Error while parsing mysql URL: %s\n", url);
-		goto err;
-	}
-
-	ptr->username.len = strlen(username);
-	ptr->username.s = (char*)pkg_malloc(ptr->username.len + 1);
-	if (!ptr->username.s) {
-		LOG(L_ERR, "new_my_id: No memory left\n");
-		goto err;
-	}
-	memcpy(ptr->username.s, username, ptr->username.len + 1);
-
-	if (password) {
-		ptr->password.len = strlen(password);
-		ptr->password.s = (char*)pkg_malloc(ptr->password.len + 1);
-		if (!ptr->password.s) {
-			LOG(L_ERR, "new_my_id: No memory left\n");
-			goto err;
-		}
-		memcpy(ptr->password.s, password, ptr->password.len + 1);
-	}
-
-	ptr->host.len = strlen(host);
-	ptr->host.s = (char*)pkg_malloc(ptr->host.len + 1);
-	if (!ptr->host.s) {
-		LOG(L_ERR, "new_my_id: No memory left\n");
-		goto err;
-	}
-	memcpy(ptr->host.s, host, ptr->host.len + 1);
-
-	if (port && *port) {
-		ptr->port = atoi(port);
-	} else {
-		ptr->port = 0;
-	}
-
-	ptr->database.len = strlen(database);
-	ptr->database.s = (char*)pkg_malloc(ptr->database.len + 1);
-	if (!ptr->database.s) {
-		LOG(L_ERR, "new_my_id: No memory left\n");
-		goto err;
-	}
-	memcpy(ptr->database.s, database, ptr->database.len + 1);
-
-	pkg_free(buf);
-	return ptr;
-
- err:
-	if (buf) pkg_free(buf);
-	if (ptr && ptr->username.s) pkg_free(ptr->username.s);
-	if (ptr && ptr->password.s) pkg_free(ptr->password.s);
-	if (ptr && ptr->host.s) pkg_free(ptr->host.s);
-	if (ptr && ptr->database.s) pkg_free(ptr->database.s);
-	if (ptr) pkg_free(ptr);
-	return 0;
-}
-
-
-/*
- * Compare two connection identifiers
- */
-unsigned char cmp_my_id(struct my_id* id1, struct my_id* id2)
-{
-	if (!id1 || !id2) return 0;
-	if (id1->port != id2->port) return 0;
-	if (id1->username.len != id2->username.len) return 0;
-	if (id1->password.len != id2->password.len) return 0;
-	if (id1->host.len != id2->host.len) return 0;
-	if (id1->database.len != id2->database.len) return 0;
-
-	if (memcmp(id1->username.s, id2->username.s, id1->username.len)) return 0;
-	if (memcmp(id1->password.s, id2->password.s, id1->password.len)) return 0;
-	if (strncasecmp(id1->host.s, id2->host.s, id1->host.len)) return 0;
-	if (memcmp(id1->database.s, id2->database.s, id1->database.len)) return 0;
-	return 1;
-}
-
-
-/*
- * Free a connection identifier
- */
-void free_my_id(struct my_id* id)
-{
-	if (!id) return;
-
-	if (id->username.s) pkg_free(id->username.s);
-	if (id->password.s) pkg_free(id->password.s);
-	if (id->host.s) pkg_free(id->host.s);
-	if (id->database.s) pkg_free(id->database.s);
-	pkg_free(id);
-}

+ 0 - 65
modules/db_mysql/my_id.h

@@ -1,65 +0,0 @@
-/* 
- * $Id$
- *
- *
- * Copyright (C) 2001-2004 iptel.org
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser 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
- */
-
-#ifndef MY_ID_H
-#define MY_ID_H
-
-#include "../../str.h"
-
-/*
- * All str fields must be zero terminated because we
- * will pass them to mysql_real_connect
- */
-struct my_id {
-	str username;  /* Username, case sensitive */
-	str password;  /* Password, case sensitive */
-	str host;      /* Host or IP, case insensitive */
-	unsigned short port; /* Port number */
-	str database;  /* Database, case sensitive */
-};
-
-
-/*
- * Create a new connection identifier
- */
-struct my_id* new_my_id(const char* url);
-
-
-/*
- * Compare two connection identifiers
- */
-unsigned char cmp_my_id(struct my_id* id1, struct my_id* id2);
-
-
-/*
- * Free a connection identifier
- */
-void free_my_id(struct my_id* id);
-
-
-#endif /* MY_ID_H */

+ 0 - 135
modules/db_mysql/my_pool.c

@@ -1,135 +0,0 @@
-/* 
- * $Id$
- *
- *
- * Copyright (C) 2001-2004 iptel.org
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser 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 <unistd.h>
-#include "../../dprint.h"
-#include "my_pool.h"
-#include "my_id.h"
-
-
-/* The head of the pool */
-static struct my_con* pool = 0;
-
-/*
- * Pid of the process that added the last
- * connection to the pool. This is used to
- * check for inherited database connections.
- */
-static int pool_pid;
-
-
-/*
- * Get a connection from the pool, reuse existing
- * if possible, otherwise create a new one
- */
-struct my_con* get_connection(const char* url)
-{
-	struct my_id* id;
-	struct my_con* ptr;
-	int pid;
-
-	if (!url) {
-		LOG(L_ERR, "get_connection: Invalid parameter value\n");
-		return 0;
-	}
-
-	pid = getpid();
-	if (pool && (pool_pid != pid)) {
-		LOG(L_ERR, "get_connection: Inherited open database connections, this is not a good idea\n");
-		return 0;
-	}
-
-	pool_pid = pid;
-
-	id = new_my_id(url);
-	if (!id) return 0;
-
-	ptr = pool;
-	while (ptr) {
-		if (cmp_my_id(id, ptr->id)) {
-			DBG("get_connection: Connection found in the pool\n");
-			ptr->ref++;
-			free_my_id(id);
-			return ptr;
-		}
-		ptr = ptr->next;
-	}
-
-	DBG("get_connection: Connection not found in the pool\n");
-	ptr = new_connection(id);
-	if (!ptr) {
-		free_my_id(id);
-		return 0;
-	}
-
-	ptr->next = pool;
-	pool = ptr;
-	return ptr;
-}
-
-
-/*
- * Release a connection, the connection will be left
- * in the pool if ref count != 0, otherwise it
- * will be delete completely
- */
-void release_connection(struct my_con* con)
-{
-	struct my_con* ptr;
-
-	if (!con) return;
-
-	if (con->ref > 1) {
-		     /* There are still other users, just
-		      * decrease the reference count and return
-		      */
-		DBG("release_connection: Connection still kept in the pool\n");
-		con->ref--;
-		return;
-	}
-
-	DBG("release_connection: Removing connection from the pool\n");
-
-	if (pool == con) {
-		pool = pool->next;
-	} else {
-		ptr = pool;
-		while(ptr) {
-			if (ptr->next == con) break;
-			ptr = ptr->next;
-		}
-		if (!ptr) {
-			LOG(L_ERR, "release_connection: Weird, connection not found in the pool\n");
-		} else {
-			     /* Remove the connection from the pool */
-			ptr->next = con->next;
-		}
-	}
-
-	free_connection(con);
-}

+ 0 - 49
modules/db_mysql/my_pool.h

@@ -1,49 +0,0 @@
-/* 
- * $Id$
- *
- *
- * Copyright (C) 2001-2004 iptel.org
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- *    [email protected]
- *
- * ser 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
- */
-
-#ifndef MY_POOL_H
-#define MY_POOL_H
-
-#include "my_con.h"
-
-/*
- * Get a connection from the pool, reuse existing
- * if possible, otherwise create a new one
- */
-struct my_con* get_connection(const char* url);
-
-
-/*
- * Release a connection, the connection will be left
- * in the pool if ref count != 0, otherwise it
- * will be delete completely
- */
-void release_connection(struct my_con* con);
-
-
-#endif /* MY_POOL_H */