📄 uac_fifo.c
字号:
/* * $Id: uac_fifo.c,v 1.12 2004/11/09 15:15:12 andrei 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-12-03 : fifo_callback() updated for changes in tm callbacks (bogdan) * 2004-02-11: fix: TM callback writes to fifo changed to non-blocking (jiri) */#include <string.h>#include "../../mem/shm_mem.h"#include "../../mem/mem.h"#include "../../dprint.h"#include "../../fifo_server.h"#include "../../str.h"#include "../../parser/msg_parser.h"#include "../../parser/parse_from.h"#include "../../parser/parse_uri.h"#include "../../ip_addr.h"#include "config.h"#include "ut.h"#include "uac.h"#include "dlg.h"#include "callid.h"#include "h_table.h"#include "uac_fifo.h"/* * Callback data structure */struct cb_data { dlg_t* dialog; char filename[1];};struct str_list { str s; struct str_list *next;};#define skip_hf(_hf) ( \ ((_hf)->type == HDR_FROM) || \ ((_hf)->type == HDR_TO) || \ ((_hf)->type == HDR_CALLID) || \ ((_hf)->type == HDR_CSEQ) \)/* * Report an error to syslog and FIFO output file */static inline void fifo_uac_error(char *reply_fifo, int code, char *msg){ LOG(L_ERR, "ERROR: fifo_uac_error: %s\n", msg ); fifo_reply(reply_fifo, "%d fifo_uac_error: %s", code, msg);}/* * Get the Request URI from the FIFO stream and parse it */static inline int fifo_get_ruri(FILE* stream, char* response_file, str* ruri, struct sip_uri* puri){ static char ruri_buf[MAX_URI_SIZE]; if (!read_line(ruri_buf, MAX_URI_SIZE, stream, &ruri->len) || !ruri->len) { fifo_uac_error(response_file, 400, "ruri expected"); return -1; } if (parse_uri(ruri_buf, ruri->len, puri) < 0 ) { fifo_uac_error(response_file, 400, "ruri invalid\n"); return -2; } ruri->s = ruri_buf; DBG("DEBUG: fifo_get_ruri: '%.*s'\n", ruri->len, ruri->s); return 0;}/* * Get and parse next hop URI */static inline int fifo_get_nexthop(FILE* stream, char* response_file, str* nexthop, struct sip_uri* pnexthop){ static char nexthop_buf[MAX_URI_SIZE]; if (!read_line(nexthop_buf, MAX_URI_SIZE, stream, &nexthop->len) || !nexthop->len) { fifo_uac_error(response_file, 400, "next hop address expected\n"); return -1; } if (nexthop->len == 1 && nexthop_buf[0] == '.' ) { DBG("DEBUG: fifo_get_nexthop: next hop empty\n"); nexthop->s = 0; nexthop->len = 0; } else if (parse_uri(nexthop_buf, nexthop->len, pnexthop) < 0 ) { fifo_uac_error(response_file, 400, "next hop uri invalid\n"); return -2; } else { nexthop->s = nexthop_buf; DBG("DEBUG: fifo_get_nexthop: hop: '%.*s'\n", nexthop->len, nexthop->s); } return 0;}/* * Get method name from FIFO stream */static inline int fifo_get_method(FILE* stream, char* response_file, str* method){ static char method_buf[MAX_METHOD]; if (!read_line(method_buf, MAX_METHOD, stream, &method->len) || !method->len) { /* line breaking must have failed -- consume the rest and proceed to a new request */ fifo_uac_error(response_file, 400, "method expected"); return -1; } method->s = method_buf; DBG("fifo_get_method: method: '%.*s'\n", method->len, method->s); return 0;}/* * Get message body from FIFO stream */static inline int fifo_get_body(FILE* stream, char* response_file, str* body){ static char body_buf[MAX_BODY]; if (!read_body(body_buf, MAX_BODY, stream, &body->len)) { fifo_uac_error(response_file, 400, "body expected"); return -1; } body->s = body_buf; DBG("fifo_get_body: body: %.*s\n", body->len, body->s); return 0;}/* * Get message headers from FIFO stream */static inline int fifo_get_headers(FILE* stream, char* response_file, str* headers){ static char headers_buf[MAX_HEADER]; /* now read and parse header fields */ if (!read_line_set(headers_buf, MAX_HEADER, stream, &headers->len) || !headers->len) { fifo_uac_error(response_file, 400, "HFs expected"); return -1; } headers->s = headers_buf; DBG("fifo_get_headers: headers: %.*s\n", headers->len, headers->s); return 0;}/* * Create shm_copy of filename */static inline int fifo_cbp(char** shm_file, char* response_file){ int fn_len; if (response_file) { fn_len = strlen(response_file) + 1; *shm_file = shm_malloc(fn_len); if (!*shm_file) { fifo_uac_error(response_file, 500, "no shmem"); return -1; } memcpy(*shm_file, response_file, fn_len); } else { *shm_file = 0; } return 0;}static inline struct str_list *new_str(char *s, int len, struct str_list **last, int *total){ struct str_list *new; new=pkg_malloc(sizeof(struct str_list)); if (!new) { LOG(L_ERR, "ERROR: new_str: not enough mem\n"); return 0; } new->s.s=s; new->s.len=len; new->next=0; (*last)->next=new; *last=new; *total+=len; return new;}static char *get_hfblock(str *uri, struct hdr_field *hf, int *l, int proto) { struct str_list sl, *last, *new, *i, *foo; int hf_avail, frag_len, total_len; char *begin, *needle, *dst, *ret, *d; str *sock_name, *portname; union sockaddr_union to_su; struct socket_info* send_sock; ret=0; /* pessimist: assume failure */ total_len=0; last=&sl; last->next=0; portname=sock_name=0; for (; hf; hf=hf->next) { if (skip_hf(hf)) continue; begin=needle=hf->name.s; hf_avail=hf->len; /* substitution loop */ while(hf_avail) { d=memchr(needle, SUBST_CHAR, hf_avail); if (!d || d+1>=needle+hf_avail) { /* nothing to substitute */ new=new_str(begin, hf_avail, &last, &total_len); if (!new) goto error; break; } else { frag_len=d-begin; d++; /* d not at the second substitution char */ switch(*d) { case SUBST_CHAR: /* double SUBST_CHAR: IP */ /* string before substitute */ new=new_str(begin, frag_len, &last, &total_len); if (!new) goto error; /* substitute */ if (!sock_name) { send_sock=uri2sock(0, uri, &to_su, proto ); if (!send_sock) { LOG(L_ERR, "ERROR: get_hfblock: send_sock failed\n"); goto error; } sock_name=&send_sock->address_str; portname=&send_sock->port_no_str; } new=new_str(sock_name->s, sock_name->len, &last, &total_len ); if (!new) goto error; /* inefficient - FIXME --andrei*/ new=new_str(":", 1, &last, &total_len); if (!new) goto error; new=new_str(portname->s, portname->len, &last, &total_len ); if (!new) goto error; /* keep going ... */ begin=needle=d+1;hf_avail-=frag_len+2; continue; default: /* no valid substitution char -- keep going */ hf_avail-=frag_len+1; needle=d; } } /* possible substitute */ } /* substitution loop */ /* proceed to next header */ /* new=new_str(CRLF, CRLF_LEN, &last, &total_len ); if (!new) goto error; */ DBG("DEBUG: get_hfblock: one more hf processed\n"); } /* header loop */ /* construct a single header block now */ ret=pkg_malloc(total_len); if (!ret) { LOG(L_ERR, "ERROR: get_hfblock no pkg mem for hf block\n"); goto error; } i=sl.next; dst=ret; while(i) { foo=i; i=i->next; memcpy(dst, foo->s.s, foo->s.len); dst+=foo->s.len; pkg_free(foo); } *l=total_len; return ret;error: i=sl.next; while(i) { foo=i; i=i->next; pkg_free(foo); } *l=0; return 0;}/* syntax: :t_uac:[file] EOL method EOL r-uri EOL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -