⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ul_fifo.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * $Id: ul_fifo.c,v 1.33.2.1 2005/03/29 11:54:35 janakj Exp $ * * Copyright (C) 2001-2003 FhG Fokus * * 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: *    info@iptel.org * * 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 * * History: * --------- * 2003-03-12 added support for replication mark */#include <string.h>#include <stdio.h>#include "../../fifo_server.h"#include "../../dprint.h"#include "../../ut.h"#include "../../qvalue.h"#include "ul_fifo.h"#include "dlist.h"#include "udomain.h"#include "utime.h"#include "ul_mod.h"#define MAX_CONTACT_LEN 128#define MAX_EXPIRES_LEN 20#define MAX_Q_LEN 20#define MAX_REPLICATE_LEN 12#define MAX_FLAGS_LEN 12/* * Dedicated to Douglas Adams, don't panic ! */#define FIFO_CALLID "The-Answer-To-The-Ultimate-Question-Of-Life-Universe-And-Everything"#define FIFO_CALLID_LEN (sizeof(FIFO_CALLID)-1)#define FIFO_CSEQ 42#define FIFO_UA "SIP Express Router FIFO"#define FIFO_UA_LEN 23static int print_ul_stats(FILE *reply_file){	dlist_t* ptr;		fprintf(reply_file, "Domain Registered Expired\n");		ptr = root;	while(ptr) {		fprintf(reply_file, "'%.*s' %d %d\n",			ptr->d->name->len, ZSW(ptr->d->name->s),			ptr->d->users,			ptr->d->expired			);		ptr = ptr->next;	}	return 1;}int static ul_stats_cmd( FILE *pipe, char *response_file ){	FILE *reply_file;		reply_file=open_reply_pipe(response_file);	if (reply_file==0) {		LOG(L_ERR, "ERROR: ul_stats: file not opened\n");		return -1;	}	fputs( "200 ok\n", reply_file );	print_ul_stats( reply_file );	fclose(reply_file);	return 1;}int static ul_dump(FILE* pipe, char* response_file){	FILE* reply_file;	reply_file=open_reply_pipe(response_file);	if (reply_file==0) {		LOG(L_ERR, "ERROR: ul_dump: file not opened\n");		return -1;	}	fputs( "200 ok\n", reply_file);	print_all_udomains(reply_file);	fclose(reply_file);	return 1;}int static ul_flush(FILE* pipe, char* response_file){	synchronize_all_udomains();	fifo_reply(response_file, "200 ul_flush completed" );	return 1;}static inline void fifo_find_domain(str* _name, udomain_t** _d){	dlist_t* ptr;	ptr = root;	while(ptr) {		if ((ptr->name.len == _name->len) &&		    !memcmp(ptr->name.s, _name->s, _name->len)) {			break;		}		ptr = ptr->next;	}		if (ptr) {		*_d = ptr->d;	} else {		*_d = 0;	}}static inline int add_contact(udomain_t* _d, str* _u, str* _c, time_t _e, qvalue_t _q, int _f){	urecord_t* r;	ucontact_t* c = 0;	int res;	str cid;	str ua;		if (_e == 0 && !(_f & FL_PERMANENT)) {		LOG(L_ERR, "fifo_add_contact(): expires == 0 and not persistent contact, giving up\n");		return -1;	}	get_act_time();	res = get_urecord(_d, _u, &r);	if (res < 0) {		LOG(L_ERR, "fifo_add_contact(): Error while getting record\n");		return -2;	}	if (res >  0) { /* Record not found */		if (insert_urecord(_d, _u, &r) < 0) {			LOG(L_ERR, "fifo_add_contact(): Error while creating new urecord\n");			return -3;		}	} else {		if (get_ucontact(r, _c, &c) < 0) {			LOG(L_ERR, "fifo_add_contact(): Error while obtaining ucontact\n");			return -4;		}	}			cid.s = FIFO_CALLID;	cid.len = FIFO_CALLID_LEN;	ua.s = FIFO_UA;	ua.len = FIFO_UA_LEN;	if (c) {		if (update_ucontact(c, _e + act_time, _q, &cid, FIFO_CSEQ, _f, FL_NONE, &ua, 0) < 0) {			LOG(L_ERR, "fifo_add_contact(): Error while updating contact\n");			release_urecord(r);			return -5;		}	} else {		if (insert_ucontact(r, _c, _e + act_time, _q, &cid, FIFO_CSEQ, _f, &c, &ua, 0) < 0) {			LOG(L_ERR, "fifo_add_contact(): Error while inserting contact\n");			release_urecord(r);			return -6;		}	}		release_urecord(r);	return 0;}static int ul_add(FILE* pipe, char* response_file){	char table_s[MAX_TABLE];	char user_s[MAX_USER];	char contact_s[MAX_CONTACT_LEN];	char expires_s[MAX_EXPIRES_LEN];	char q_s[MAX_Q_LEN];	char rep_s[MAX_REPLICATE_LEN];	char flags_s[MAX_FLAGS_LEN];	udomain_t* d;	int exp_i, flags_i;	char* at;	qvalue_t qval;	str table, user, contact, expires, q, rep, flags;	if (!read_line(table_s, MAX_TABLE, pipe, &table.len) || table.len == 0) {		fifo_reply(response_file,			   "400 ul_add: table name expected\n");		LOG(L_ERR, "ERROR: ul_add: table name expected\n");		return 1;	}		if (!read_line(user_s, MAX_USER, pipe, &user.len) || user.len  == 0) {		fifo_reply(response_file,			   "400 ul_add: aor name expected\n");		LOG(L_ERR, "ERROR: ul_add: aor expected\n");		return 1;	}	at = memchr(user_s, '@', user.len);	if (use_domain) {		if (!at) {			fifo_reply(response_file,				   "400 ul_add: username@domain expected\n");			LOG(L_ERR, "ERROR: ul_add: Domain missing\n");			return 1;		}	} else {		if (at) {			user.len = at - user_s;		}	}	if (!read_line(contact_s, MAX_CONTACT_LEN, pipe, &contact.len) || contact.len == 0) {		fifo_reply(response_file,			   "400 ul_add: contact expected\n");		LOG(L_ERR, "ERROR: ul_add: contact expected\n");		return 1;	}		if (!read_line(expires_s, MAX_EXPIRES_LEN, pipe, &expires.len) || expires.len == 0) {		fifo_reply(response_file,			   "400 ul_add: expires expected\n");		LOG(L_ERR, "ERROR: ul_add: expires expected\n");		return 1;	}		if (!read_line(q_s, MAX_Q, pipe, &q.len) || q.len == 0) {		fifo_reply(response_file,			   "400 ul_add: q expected\n");		LOG(L_ERR, "ERROR: ul_add: q expected\n");		return 1;	}	     /* Kept for backwards compatibility */	if (!read_line(rep_s, MAX_REPLICATE_LEN, pipe, &rep.len) || rep.len == 0) {		fifo_reply(response_file,			   "400 ul_add: replicate expected\n");		LOG(L_ERR, "ERROR: ul_add: replicate expected\n");		return 1;	}	if (!read_line(flags_s, MAX_FLAGS_LEN, pipe, &flags.len) || flags.len == 0) {		fifo_reply(response_file,			   "400 ul_add: flags expected\n");		LOG(L_ERR, "ERROR: ul_add: flags expected\n");		return 1;	}		table.s = table_s;	user.s = user_s;	strlower(&user);	contact.s = contact_s;	expires.s = expires_s;	q.s = q_s;	flags.s = flags_s;		fifo_find_domain(&table, &d);		if (d) {		if (str2int(&expires, (unsigned int*)&exp_i) < 0) {			fifo_reply(response_file, "400 Invalid expires format\n");			return 1;		}		if (str2q(&qval, q.s, q.len) < 0) {			fifo_reply(response_file, "400 Invalid q value\n");			return 1;		}		if (str2int(&flags, (unsigned int*)&flags_i) < 0) {			fifo_reply(response_file, "400 Invalid flags format\n");			return 1;		}				lock_udomain(d);				if (add_contact(d, &user, &contact, exp_i, qval, flags_i) < 0) {			unlock_udomain(d);			LOG(L_ERR, "ul_add(): Error while adding contact ('%.*s','%.*s') in table '%.*s'\n",			    user.len, ZSW(user.s), contact.len, ZSW(contact.s), table.len, ZSW(table.s));			fifo_reply(response_file, "500 Error while adding contact\n"				   " ('%.*s','%.*s') in table '%.*s'\n",				   user.len, ZSW(user.s), contact.len, ZSW(contact.s), table.len, ZSW(table.s));			return 1;		}		unlock_udomain(d);				fifo_reply(response_file, "200 Added to table\n"				"('%.*s','%.*s') to '%.*s'\n",			   user.len, ZSW(user.s), contact.len, ZSW(contact.s), table.len, ZSW(table.s));		return 1;	} else {		fifo_reply(response_file, "400 Table '%.*s' not found in memory, use save(\"%.*s\") or lookup(\"%.*s\") in the configuration script first\n", 			table.len, ZSW(table.s), table.len, ZSW(table.s), table.len, ZSW(table.s));		return 1;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -