ul_fifo.c

来自「性能优秀的SIP Proxy」· C语言 代码 · 共 650 行 · 第 1/2 页

C
650
字号
	if (use_domain) {		if (!at) {			fifo_reply(response_file,				   "400 ul_rm: username@domain expected\n");			LOG(L_ERR, "ERROR: ul_rm: Domain missing\n");			return 1;		}	} else {		if (at) {			aor.len = at - user;		}	}	aor.s = user;	strlower(&aor);	t.s = table;	fifo_find_domain(&t, &d);	LOG(L_INFO, "INFO: deleting user-loc (%s,%s)\n",	    table, user );		if (d) {		lock_udomain(d);		if (delete_urecord(d, &aor, 0) < 0) {			LOG(L_ERR, "ul_rm(): Error while deleting user %s\n", user);			unlock_udomain(d);			fifo_reply(response_file, "500 Error while deleting user %s\n", user);			return 1;		}		unlock_udomain(d);		fifo_reply(response_file, "200 user (%s, %s) deleted\n", 			table, user);		return 1;	} else {		fifo_reply(response_file, "400 table (%s) not found\n", table);		return 1;	}}static int ul_rm_contact(FILE* pipe, char* response_file){	char table[MAX_TABLE];	char user[MAX_USER];	char contact[MAX_CONTACT_LEN];	udomain_t* d;	urecord_t* r;	ucontact_t* con;	str aor, t, c;	int res;	char* at;	if (!read_line(table, MAX_TABLE, pipe, &t.len) || t.len ==0) {		fifo_reply(response_file, 			   "400 ul_rm_contact: table name expected\n");		LOG(L_ERR, "ERROR: ul_rm_contact: table name expected\n");		return 1;	}	if (!read_line(user, MAX_USER, pipe, &aor.len) || aor.len==0) {		fifo_reply(response_file, 			   "400 ul_rm_contact: user name expected\n");		LOG(L_ERR, "ERROR: ul_rm_contact: user name expected\n");		return 1;	}	at = memchr(user, '@', aor.len);	if (use_domain) {		if (!at) {			fifo_reply(response_file,				   "400 ul_rm_contact: user@domain expected\n");			LOG(L_ERR, "ERROR: ul_rm_contact: Domain missing\n");			return 1;		}	} else {		if (at) {			aor.len = at - user;		}	}	if (!read_line(contact, MAX_CONTACT_LEN, pipe, &c.len) || c.len == 0) {		fifo_reply(response_file,			"400 ul_rm_contact: contact expected\n");		LOG(L_ERR, "ERROR: ul_rm_contact: contact expected\n");		return 1;	}	aor.s = user;	strlower(&aor);	t.s = table;	c.s = contact;	fifo_find_domain(&t, &d);	LOG(L_INFO, "INFO: deleting user-loc contact (%s,%s,%s)\n",	    table, user, contact );	if (d) {		lock_udomain(d);		res = get_urecord(d, &aor, &r);		if (res < 0) {			fifo_reply(response_file, "500 Error while looking for "				"username %s in table %s\n", user, table);			LOG(L_ERR, "ERROR: ul_rm_contact: Error while looking for "				"username %s in table %s\n", user, table);			unlock_udomain(d);			return 1;		}				if (res > 0) {			fifo_reply(response_file, "404 Username %s in table %s "				"not found\n", user, table);			unlock_udomain(d);			return 1;		}		res = get_ucontact(r, &c, &fifo_cid, FIFO_CSEQ+1, &con);		if (res < 0) {			fifo_reply(response_file, "500 Error while looking for "				"contact %s\n", contact);			LOG(L_ERR, "ERROR: ul_rm_contact: Error while looking for "				"contact %s\n", contact);			unlock_udomain(d);			return 1;		}					if (res > 0) {			fifo_reply(response_file, "404 Contact %s in table %s "				"not found\n", contact, table);			unlock_udomain(d);			return 1;		}		if (delete_ucontact(r, con) < 0) {			fifo_reply(response_file, "500 ul_rm_contact: Error while "				"deleting contact %s\n", contact);			unlock_udomain(d);			return 1;		}		release_urecord(r);		unlock_udomain(d);		fifo_reply(response_file, "200 Contact (%s, %s) deleted from "			"table %s\n", user, contact, table);		return 1;	} else {		fifo_reply(response_file, "400 table (%s) not found\n", table);		return 1;	}}/* * Build Contact HF for reply */static inline int print_contacts(FILE* _o, ucontact_t* _c){	int cnt = 0;	while(_c) {		if (VALID_CONTACT(_c, act_time)) {			cnt++;			if (cnt==1) {				fputs( "200 OK\n", _o);			}			fprintf(_o, "<%.*s>;q=%s;expires=%d;flags=0x%X;socket=<%.*s>"				";methods=0x%X"				"%s%.*s%s" /*received*/				"%s%.*s%s" /*user-agent*/				"%s%.*s%s\n", /*path*/				_c->c.len, ZSW(_c->c.s),				q2str(_c->q, 0), (int)(_c->expires - act_time), _c->flags,				_c->sock?_c->sock->sock_str.len:3,					_c->sock?_c->sock->sock_str.s:"NULL",				_c->methods,				_c->received.len?";received=<":"",_c->received.len,					ZSW(_c->received.s), _c->received.len?">":"",				_c->user_agent.len?";user_agent=<":"",_c->user_agent.len,					ZSW(_c->user_agent.s), _c->user_agent.len?">":"",				_c->path.len?";path=<":"",_c->path.len,					ZSW(_c->path.s), _c->path.len?">":""				);		}		_c = _c->next;	}	return cnt;}static inline int ul_show_contact(FILE* pipe, char* response_file){	char table[MAX_TABLE];	char user[MAX_USER];	FILE* reply_file;	udomain_t* d;	urecord_t* r;	int res;	str t, aor;	char* at;	if (!read_line(table, MAX_TABLE, pipe, &t.len) || t.len ==0) {		fifo_reply(response_file, 			   "400 ul_show_contact: table name expected\n");		LOG(L_ERR, "ERROR: ul_show_contact: table name expected\n");		return 1;	}	if (!read_line(user, MAX_USER, pipe, &aor.len) || aor.len==0) {		fifo_reply(response_file, 			   "400 ul_show_contact: user name expected\n");		LOG(L_ERR, "ERROR: ul_show_contact: user name expected\n");		return 1;	}		at = memchr(user, '@', aor.len);	if (use_domain) {		if (!at) {			fifo_reply(response_file,				   "400 ul_show_contact: user@domain expected\n");			LOG(L_ERR, "ERROR: ul_show_contact: Domain missing\n");			return 1;		}	} else {		if (at) {			aor.len = at - user;		}	}	aor.s = user;	strlower(&aor);	t.s = table;		fifo_find_domain(&t, &d);	if (d) {		lock_udomain(d);		res = get_urecord(d, &aor, &r);		if (res < 0) {			fifo_reply(response_file, "500 Error while looking for username %s in table %s\n", user, table);			LOG(L_ERR, "ERROR: ul_show_contact: Error while looking for username %s in table %s\n", user, table);			unlock_udomain(d);			return 1;		}				if (res > 0) {			fifo_reply(response_file, "404 Username %s in table %s not found\n", user, table);			unlock_udomain(d);			return 1;		}				get_act_time();		reply_file=open_reply_pipe(response_file);		if (reply_file==0) {			LOG(L_ERR, "ERROR: ul_show_contact: file not opened\n");			unlock_udomain(d);			return 1;		}		if (!print_contacts(reply_file, r->contacts)) {			unlock_udomain(d);			fprintf(reply_file, "404 No registered contacts found\n");			fclose(reply_file);			return 1;		}		fclose(reply_file);		unlock_udomain(d);		return 1;	} else {		fifo_reply(response_file, "400 table (%s) not found\n", table);		return 1;	}}int init_ul_fifo( void ) {	if (register_fifo_cmd(ul_rm, UL_RM, 0) < 0) {		LOG(L_CRIT, "cannot register ul_rm\n");		return -1;	}	if (register_fifo_cmd(ul_rm_contact, UL_RM_CONTACT, 0) < 0) {		LOG(L_CRIT, "cannot register ul_rm_contact\n");		return -1;	}		if (register_fifo_cmd(ul_dump, UL_DUMP, 0) < 0) {		LOG(L_CRIT, "cannot register ul_dump\n");		return -1;	}	if (register_fifo_cmd(ul_flush, UL_FLUSH, 0) < 0) {		LOG(L_CRIT, "cannot register ul_flush\n");		return -1;	}	if (register_fifo_cmd(ul_add, UL_ADD, 0) < 0) {		LOG(L_CRIT, "cannot register ul_add\n");		return -1;	}	if (register_fifo_cmd(ul_show_contact, UL_SHOW_CONTACT, 0) < 0) {		LOG(L_CRIT, "cannot register ul_show_contact\n");		return -1;	}	return 1;}

⌨️ 快捷键说明

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