示例#1
0
static void print_for_expr(struct for_expr *e, int depth)
{
    struct for_clause *clause;
    int i;

    printf("%sfor\n", indent(depth));
    for (clause=e->clauses, i=0; clause != NULL; clause=clause->next, i++) {
	print_param_list(clause->vars, depth+1);
	switch (clause->kind) {
	  case for_EQUAL_THEN:
	    {
		struct equal_then_for_clause *c
		    = (struct equal_then_for_clause *)clause;
		printf("%sequal\n", indent(depth+2));
		print_expr(c->equal, depth+3);
		printf("%sthen\n", indent(depth+2));
		print_expr(c->then, depth+3);
	    }
	    break;
	  case for_IN:
	    {
		struct in_for_clause *c = (struct in_for_clause *)clause;
		printf("%sin\n", indent(depth+2));
		print_expr(c->collection, depth+3);
	    }
	    break;
	  case for_FROM:
	    {
		struct from_for_clause *c = (struct from_for_clause *)clause;
		printf("%sfrom\n", indent(depth+2));
		print_expr(c->from, depth+3);
		if (c->to) {
		    static char *to_kinds[] = {"to", "above", "below"};
		    printf("%s%s\n", indent(depth+2),
			   to_kinds[(int)c->to_kind]);
		    print_expr(c->to, depth+3);
		}
		if (c->by) {
		    printf("%sby\n", indent(depth+2));
		    print_expr(c->by, depth+3);
		}
	    }
	    break;
	  default:
	    lose("Bogus for clause kind.");
	}
    }
    if (e->until) {
	printf("%suntil\n", indent(depth));
	print_expr(e->until, depth+1);
    }
    printf("%sdo\n", indent(depth));
    print_body(e->body, depth+1);
    if (e->finally) {
	printf("%sfinally\n", indent(depth));
	print_body(e->finally, depth+1);
    }
    printf("%send for\n", indent(depth));
}
示例#2
0
static void print_if_expr(struct if_expr *e, int depth)
{
    printf("%sif\n", indent(depth));
    print_expr(e->cond, depth+1);
    printf("%sconsequent\n", indent(depth));
    print_body(e->consequent, depth+1);
    printf("%salternate\n", indent(depth));
    print_body(e->alternate, depth+1);
    printf("%send if\n", indent(depth));
}
示例#3
0
 std::ostream& print_branch(const ast::Branch& branch) {
   print_indent();
   stream << "if(";
   node(*branch.condition);
   stream << ") {\n";
   this->indent += 2;
   print_body(branch.then_);
   this->indent -= 2;
   stream << " }\n else {\n";
   this->indent += 2;
   print_body(branch.else_);
   this->indent -= 2;
   stream << "}\n";
   return stream;
 }
示例#4
0
static void print_method(struct method *method, int depth)
{
    fputs(indent(depth), stdout);
    if (method->top_level)
	fputs("top level ", stdout);
    if (method->debug_name)
	printf("method %s\n", debug_name_string(method->debug_name));
    else if (method->name)
	printf("method %s\n", method->name->symbol->name);
    else
	fputs("anonymous method\n", stdout);

    print_param_list(method->params, depth+1);
    if (method->specializers) {
	printf("%sspecializers\n", indent(depth));
	print_expr(method->specializers, depth+1);
    }
    print_return_type_list(method->rettypes, depth);
    printf("%sbody\n", indent(depth));
    print_body(method->body, depth+1);

    printf("%send ", indent(depth));
    if (method->top_level)
	fputs("top level ", stdout);
    if (method->debug_name)
	printf("method %s\n", debug_name_string(method->debug_name));
    else if (method->name)
	printf("method %s\n", method->name->symbol->name);
    else
	fputs("anonymous method\n", stdout);
}
示例#5
0
static void print_let_constituent(struct let_constituent *c, int depth)
{
    print_bindings(c->bindings, depth);
    printf("%sbody\n", indent(depth));
    print_body(c->body, depth+1);
    printf("%send bind\n", indent(depth));
}
示例#6
0
void rec_print_leaf_2(int height, int size){
	if (height == size){
		return;
	}else {
		print_left(height,size,1);
		print_body(height,size);
		printf("\\\n");
		rec_print_leaf_2(height+1, size);
	}
}
示例#7
0
/*!
 * \internal
 * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
 *
 * \details Attempts to fill in as much information as possible into the given
 * msg structure copied from the given request data.
 *
 * \param rdata The SIP request
 * \param msg The asterisk message structure to fill in.
 */
static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
{

#define CHECK_RES(z_) do { if (z_) { ast_msg_destroy(msg); \
		return PJSIP_SC_INTERNAL_SERVER_ERROR; } } while (0)

	int size;
	char buf[MAX_BODY_SIZE];
	pjsip_name_addr *name_addr;
	const char *field;
	pjsip_status_code code;
	struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
	const char *context = S_OR(endpt->message_context, endpt->context);

	/* make sure there is an appropriate context and extension*/
	if ((code = get_destination(rdata, context, buf)) != PJSIP_SC_OK) {
		return code;
	}

	CHECK_RES(ast_msg_set_context(msg, "%s", context));
	CHECK_RES(ast_msg_set_exten(msg, "%s", buf));

	/* to header */
	name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
	if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
		buf[size] = '\0';
		/* prepend the tech */
		CHECK_RES(ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf)-1)));
	}

	/* from header */
	name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
	if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
		buf[size] = '\0';
		CHECK_RES(ast_msg_set_from(msg, "%s", buf));
	}

	/* receive address */
	field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
	CHECK_RES(ast_msg_set_var(msg, "PJSIP_RECVADDR", field));

	/* body */
	if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
		CHECK_RES(ast_msg_set_body(msg, "%s", buf));
	}

	/* endpoint name */
	if (endpt->id.self.name.valid) {
		CHECK_RES(ast_msg_set_var(msg, "PJSIP_PEERNAME", endpt->id.self.name.str));
	}

	CHECK_RES(headers_to_vars(rdata, msg));

	return PJSIP_SC_OK;
}
示例#8
0
static void print_exception_clause(struct exception_clause *clause, int depth)
{
    printf("%sexception\n%stype\n", indent(depth), indent(depth+1));
    print_expr(clause->type, depth+2);
    if (clause->condition)
	printf("%scondition %s\n", indent(depth+1),
	       clause->condition->symbol->name);
    print_plist(clause->plist, depth+1);
    printf("%shandler\n", indent(depth+1));
    print_body(clause->body, depth+2);
}
示例#9
0
static void print_local_constituent(struct local_constituent *c, int depth)
{
    struct method *method;

    printf("%slocal\n", indent(depth));
    for (method = c->methods; method != NULL; method = method->next_local)
	print_method(method, depth+1);
    printf("%sbody\n", indent(depth));
    print_body(c->body, depth+1);
    printf("%send local\n", indent(depth));
}
示例#10
0
static void print_block_expr(struct block_expr *e, int depth)
{
    struct exception_clause *clause;

    if (e->exit_fun)
	printf("%sblock (%s)\n", indent(depth), e->exit_fun->symbol->name);
    else
	printf("%sblock <no exit function>\n", indent(depth));
    print_body(e->body, depth+1);
    if (e->inner)
	for (clause = e->inner; clause != NULL; clause = clause->next)
	    print_exception_clause(clause, depth);
    if (e->cleanup) {
	printf("%scleanup\n", indent(depth));
	print_body(e->cleanup, depth+1);
    }
    if (e->outer)
	for (clause = e->outer; clause != NULL; clause = clause->next)
	    print_exception_clause(clause, depth);
    printf("%send block\n", indent(depth));
}
示例#11
0
 std::ostream& print_while(const ast::While& whil) {
   print_indent();
   stream << "while(";
   node(*whil.condition);
   stream << ") {\n";
   this->indent += 2;
   print_body(whil.body);
   this->indent -= 2;
   print_indent();
   stream << "}\n";
   return stream;
 }
示例#12
0
static void print_handler_constituent(struct handler_constituent *c, int depth)
{
    printf("%shandler\n", indent(depth));
    if (c->type) {
	printf("%stype:\n", indent(depth+1));
	print_expr(c->type, depth+2);
	printf("%sfunction:\n", indent(depth+1));
	print_expr(c->func, depth+2);
	print_plist(c->plist, depth+1);
    }
    printf("%sbody\n", indent(depth));
    print_body(c->body, depth+1);
    printf("%send handler\n", indent(depth));
}
示例#13
0
文件: gmime-test.c 项目: Chris00/mu
static gboolean
test_message (GMimeMessage *msg)
{
	gchar		*val;
	const gchar	*str;

	g_print ("From   : %s\n", g_mime_message_get_sender (msg));

	val = get_recip (msg, GMIME_RECIPIENT_TYPE_TO);
	g_print ("To     : %s\n", val ? val : "<none>" );
	g_free (val);

	val = get_recip (msg, GMIME_RECIPIENT_TYPE_CC);
	g_print ("Cc     : %s\n", val ? val : "<none>" );
	g_free (val);

	val = get_recip (msg, GMIME_RECIPIENT_TYPE_BCC);
	g_print ("Bcc    : %s\n", val ? val : "<none>" );
	g_free (val);

	str = g_mime_message_get_subject (msg);
	g_print ("Subject: %s\n", str ? str : "<none>");

	print_date (msg);

	str = g_mime_message_get_message_id (msg);
	g_print ("Msg-id : %s\n", str ? str : "<none>");

	{
		gchar	*refsstr;
		refsstr = get_refs_str (msg);
		g_print ("Refs   : %s\n", refsstr ? refsstr : "<none>");
		g_free (refsstr);
	}

	print_body (msg);

	return TRUE;
}
示例#14
0
 std::ostream& print_func(const ast::Func& func) {
   stream << "fun ";
   print_func_header(func);
   stream << " {\n";
   this->indent = 2;
   print_indent();
   stream <<  "vars {\n";
   this->indent += 2;
   for (auto it = func.local_variables.begin();
           it != func.local_variables.end(); ++it) {
     print_indent();
     type(it->type);
     stream << " " << it->identifier << ";\n";
   }
   this->indent -= 2;
   print_indent();
   stream << "}\n";
   print_body(func.body);
   stream << "}\n";
   this->indent = 0;
   return stream;
 }
示例#15
0
int main (int argc, char *argv[]) 
{
	printf("Analyzing...\n");
	ofp=fopen(o_file_0,"w");
	if(ofp==NULL){
		fprintf(stderr, "Can't open output file %s!\n", o_file_0);
		return(1);
	}
	
	
	/* sections of our gv file */
	print_header();
	print_body(argc, argv);
	print_footer();
	
	
	
	
	fclose(ofp);
	printf("Generated graph.\n");
	
	return 0;
}
示例#16
0
static void print_condition_body(struct condition_body *body, int depth)
{
    struct condition *c;

    while (body != NULL) {
	c = body->clause->conditions;
	if (c) {
	    printf("%swhen\n", indent(depth));
	    while (1) {
		print_expr(c->cond, depth+1);
		c = c->next;
		if (c == NULL)
		    break;
		printf("%sor\n", indent(depth));
	    }
	    printf("%s=>\n", indent(depth));
	}
	else
	    printf("%sotherwise\n", indent(depth));
	print_body(body->clause->body, depth+1);
	body = body->next;
    }
}
示例#17
0
int main(int argc, char *argv[])
{
    boolean print_parse = FALSE;
    boolean print_expanded = FALSE;
    char *arg;
    char *source_name = NULL;
    char *output_name = NULL;
    FILE *file;

    add_header_handler("module", set_module);
    add_header_handler("library", set_library);
    add_header_handler(NULL, end_of_headers);

    init_sym_table();
    init_feature();
    init_info();
    init_expand();
    init_compile();
    
#ifdef MACOS
	add_feature(symbol("macos"));
#	ifndef SHLB
	argc = ccommand( &argv );
#	endif
#endif


    while ((arg = *++argv) != NULL) {
	if (arg[0] == '-') {
	    switch (arg[1]) {
	      case 'd':
		if (arg[2] == '\0') {
		    print_parse = TRUE;
		    print_expanded = TRUE;
		}
		else {
		    char *ptr;
		    for (ptr = arg+2; *ptr != '\0'; ptr++) {
			switch (*ptr) {
			  case 'p':
			    print_parse = TRUE;
			    break;
			  case 'e':
			    print_expanded = TRUE;
			    break;
			  default:
			    fprintf(stderr, "Invalid thing to print: ``%c''\n",
				    *ptr);
			    usage();
			}
		    }
		}
		break;

	      case 'o':
		if (output_name != NULL) {
		    fprintf(stderr, "-o can only be used once.\n");
		    usage();
		}
		else if (arg[2] != '\0')
		    output_name = arg+2;
		else if (*++argv == NULL) {
		    fprintf(stderr, "-o must be followed by the output "
			    "file name.\n");
		    usage();
		}
		else
		    output_name = *argv;
		break;

	      case 'l':
		if (LibraryName != NULL) {
		    fprintf(stderr, "-l can only be used once.\n");
		    usage();
		}
		if (arg[2] != '\0')
		    LibraryName = symbol(arg+2);
		else if (*++argv == NULL) {
		    fprintf(stderr, "-l must be followed by the library "
			    "name.\n");
		    usage();
		}
		else
		    LibraryName = symbol(*argv);
		break;

	      case 'q':
		GiveWarnings = FALSE;
		break;

	      case 'D':
		if (arg[2] != '\0')
		    add_feature(symbol(arg + 2));
		else if (*++argv == NULL) {
		    fprintf(stderr,
			    "-D must be followed by the feature to define.\n");
		    usage();
		}
		else
		    add_feature(symbol(*argv));
		break;

	      case 'U':
		if (arg[2] != '\0')
		    remove_feature(symbol(arg + 2));
		else if (*++argv == NULL) {
		    fprintf(stderr, "-U must be followed by the feature "
			    "to undefine.\n");
		    usage();
		}
		else
		    remove_feature(symbol(*argv));
		break;

	      default:
		fprintf(stderr, "Invalid flag: ``%s''\n", arg);
		usage();
	    }
	}
	else if (source_name != NULL) {
	    fprintf(stderr, "Too many files\n");
	    usage();
	}
	else
	    source_name = arg;
    }

    if (source_name == NULL)
	usage();


    yyin = fopen(source_name, "rb");
    if (yyin == NULL) {
        /* Try the same filename but in the current directory */
        /* Start ptr at the null termination, and work backwards to a 
	   path separator. */
        char *ptr = source_name + strlen(source_name);
        for ( ; ptr != source_name; ptr--) {
	    if (*ptr == '/' || *ptr == '\\') {
	        /* We're pointing at the path separator, which is too far */
	        ptr++;
	        break;
	    }
	}
	/* If ptr is a different string than source_name, and it isn't
	   the empty string... */
	if (ptr != source_name && *ptr != 0) {
	    yyin = fopen(ptr, "rb");
	}
	if (yyin == NULL) {
	    perror(source_name);
	    exit(1);
	}
    }

    current_file = source_name;

    yyparse();

    if (print_parse) {
	printf("================ Original Parse Tree ================\n");
	print_body(Program, 0);
    }

    if (nerrors != 0)
	exit(1);

    /* Do the various source-to-source expansions. */
    expand(Program);

    if (print_expanded) {
	printf("================ Expanded Parse Tree ================\n");
	print_body(Program, 0);
    }

    if (nerrors != 0)
	exit(1);

    /* Run environment analysis */
    environment_analysis(Program);

    if (nerrors != 0)
	exit(1);

    if (output_name == NULL)
	output_name = make_output_name(source_name, ".dbc");

    if (strcmp(output_name, "-") == 0)
        file = stdout;
    else
        file = fopen(output_name, "wb");

    if (file == NULL) {
	perror(output_name);
	exit(1);
    }

    dump_setup_output(source_name, file);

    /* Generate code. */
    compile(Program);

    dump_finalize_output();

    fclose(file);

    exit(0);
    return 0;
}
示例#18
0
static int msg_send(void *data)
{
	RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);

	const struct ast_sip_body body = {
		.type = "text",
		.subtype = "plain",
		.body_text = ast_msg_get_body(mdata->msg)
	};

	pjsip_tx_data *tdata;
	RAII_VAR(char *, uri, NULL, ast_free);
	RAII_VAR(struct ast_sip_endpoint *, endpoint, get_outbound_endpoint(
			 mdata->to, &uri), ao2_cleanup);

	if (!endpoint) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not find endpoint '%s' and "
			"no default outbound endpoint configured\n", mdata->to);
		return -1;
	}

	if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not create request\n");
		return -1;
	}

	update_to(tdata, mdata->to);
	update_from(tdata, mdata->from);

	if (ast_sip_add_body(tdata, &body)) {
		pjsip_tx_data_dec_ref(tdata);
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
		return -1;
	}

	vars_to_headers(mdata->msg, tdata);

	ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
		mdata->to, ast_sorcery_object_get_id(endpoint), mdata->from);

	if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");
		return -1;
	}

	return PJ_SUCCESS;
}

static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
{
	struct msg_data *mdata;

	if (ast_strlen_zero(to)) {
		ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI  must be specified\n");
		return -1;
	}

	if (!(mdata = msg_data_create(msg, to, from)) ||
	    ast_sip_push_task(message_serializer, msg_send, mdata)) {
		ao2_ref(mdata, -1);
		return -1;
	}
	return 0;
}

static const struct ast_msg_tech msg_tech = {
	.name = "pjsip",
	.msg_send = sip_msg_send,
};

static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
				 pjsip_dialog *dlg, pjsip_transaction *tsx)
{
	pjsip_tx_data *tdata;
	pj_status_t status;

	status = ast_sip_create_response(rdata, code, NULL, &tdata);
	if (status != PJ_SUCCESS) {
		ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
		return status;
	}

	if (dlg && tsx) {
		status = pjsip_dlg_send_response(dlg, tsx, tdata);
	} else {
		struct ast_sip_endpoint *endpoint;

		endpoint = ast_pjsip_rdata_get_endpoint(rdata);
		status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
		ao2_cleanup(endpoint);
	}

	if (status != PJ_SUCCESS) {
		ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
	}

	return status;
}

static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
{
	enum pjsip_status_code code;
	struct ast_msg *msg;

	/* if not a MESSAGE, don't handle */
	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
		return PJ_FALSE;
	}

	code = check_content_type(rdata);
	if (code != PJSIP_SC_OK) {
		send_response(rdata, code, NULL, NULL);
		return PJ_TRUE;
	}

	msg = ast_msg_alloc();
	if (!msg) {
		send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
		return PJ_TRUE;
	}

	code = rx_data_to_ast_msg(rdata, msg);
	if (code != PJSIP_SC_OK) {
		send_response(rdata, code, NULL, NULL);
		ast_msg_destroy(msg);
		return PJ_TRUE;
	}

	if (!ast_msg_has_destination(msg)) {
		ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
		send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
		ast_msg_destroy(msg);
		return PJ_TRUE;
	}

	/* Send it to the messaging core.
	 *
	 * If we are unable to send a response, the most likely reason is that we
	 * are handling a retransmission of an incoming MESSAGE and were unable to
	 * create a transaction due to a duplicate key. If we are unable to send
	 * a response, we should not queue the message to the dialplan
	 */
	if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
		ast_msg_queue(msg);
	}

	return PJ_TRUE;
}

static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
{
	char buf[MAX_BODY_SIZE];
	enum pjsip_status_code code;
	struct ast_frame f;
	pjsip_dialog *dlg = session->inv_session->dlg;
	pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);

	if (!session->channel) {
		send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
		return 0;
	}

	if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
		send_response(rdata, code, dlg, tsx);
		return 0;
	}

	if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
		/* invalid body size */
		send_response(rdata, PJSIP_SC_REQUEST_ENTITY_TOO_LARGE, dlg, tsx);
		return 0;
	}

	ast_debug(3, "Received in dialog SIP message\n");

	memset(&f, 0, sizeof(f));
	f.frametype = AST_FRAME_TEXT;
	f.subclass.integer = 0;
	f.offset = 0;
	f.data.ptr = buf;
	f.datalen = strlen(buf) + 1;
	ast_queue_frame(session->channel, &f);

	send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
	return 0;
}
示例#19
0
/*!
 * \internal
 * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
 *
 * \details Attempts to fill in as much information as possible into the given
 * msg structure copied from the given request data.
 *
 * \param rdata The SIP request
 * \param msg The asterisk message structure to fill in.
 */
static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
{
	RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup);
	pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
	pjsip_sip_uri *sip_ruri;
	pjsip_name_addr *name_addr;
	char buf[MAX_BODY_SIZE];
	const char *field;
	const char *context;
	char exten[AST_MAX_EXTENSION];
	int res = 0;
	int size;

	if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
		return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
	}

	sip_ruri = pjsip_uri_get_uri(ruri);
	ast_copy_pj_str(exten, &sip_ruri->user, AST_MAX_EXTENSION);

	endpt = ast_pjsip_rdata_get_endpoint(rdata);
	ast_assert(endpt != NULL);

	context = S_OR(endpt->message_context, endpt->context);
	res |= ast_msg_set_context(msg, "%s", context);
	res |= ast_msg_set_exten(msg, "%s", exten);

	/* to header */
	name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
	size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
	if (size <= 0) {
		return PJSIP_SC_INTERNAL_SERVER_ERROR;
	}
	buf[size] = '\0';
	res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1));

	/* from header */
	name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
	size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
	if (size <= 0) {
		return PJSIP_SC_INTERNAL_SERVER_ERROR;
	}
	buf[size] = '\0';
	res |= ast_msg_set_from(msg, "%s", buf);

	field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 1);
	res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field);

	if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
		res |= ast_msg_set_body(msg, "%s", buf);
	}

	/* endpoint name */
	res |= ast_msg_set_tech(msg, "%s", "PJSIP");
	res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt));
	if (endpt->id.self.name.valid) {
		res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str);
	}

	res |= headers_to_vars(rdata, msg);

	return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR;
}
示例#20
0
static int run_binrpc_cmd(int s, struct binrpc_cmd * cmd, char* fmt)
{
	int cookie;
	unsigned char reply_buf[MAX_REPLY_SIZE];
	unsigned char* msg_body;
	struct binrpc_parse_ctx in_pkt;
	int ret;

	cookie=gen_cookie();
	if ((ret=send_binrpc_cmd(s, cmd, cookie))<0){
		if (ret==-1) goto error_send;
		else goto binrpc_err;
	}
	/* read reply */
	memset(&in_pkt, 0, sizeof(in_pkt));
	if ((ret=get_reply(s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt,
					&msg_body))<0){
		switch(ret){
			case -1:
				goto error_read;
			case -2:
				goto error_parse;
			case -3:
				goto error_cookie;
			case -4:
				goto error_toobig;
		}
		goto error;
	}
	switch(in_pkt.type){
		case BINRPC_FAULT:
			if (print_fault(&in_pkt, msg_body, in_pkt.tlen)<0){
				goto error;
			}
			break;
		case BINRPC_REPL:
			if (print_body(&in_pkt, msg_body, in_pkt.tlen, fmt)<0){
				goto error;
			}
			break;
		default:
			fprintf(stderr, "ERROR: not a reply\n");
			goto error;
	}
	if (verbose) printf(".\n");
	/* normal exit */
	return 0;
binrpc_err:
	fprintf(stderr, "ERROR while building the packet: %s\n",
				binrpc_error(ret));
	goto error;
error_parse:
	fprintf(stderr, "ERROR while parsing the reply: %s\n",
				binrpc_error(binrpc_errno));
	goto error;
error_cookie:
	fprintf(stderr, "ERROR: cookie does not match\n");
	goto error;
error_toobig:
	fprintf(stderr, "ERROR: reply too big\n");
	goto error;
error_send:
	fprintf(stderr, "ERROR: send packet failed: %s (%d)\n",
			strerror(errno), errno);
	goto error;
error_read:
	fprintf(stderr, "ERROR: read reply failed: %s (%d)\n",
			strerror(errno), errno);
	goto error;
error:
	return -1;
}
示例#21
0
static int print_fault(struct binrpc_parse_ctx* in_pkt,
						unsigned char* body, int size)
{
	printf("error: ");
	return print_body(in_pkt, body, size, "%v - %v\n");
}
示例#22
0
static int msg_send(void *data)
{
	RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);

	const struct ast_sip_body body = {
		.type = "text",
		.subtype = "plain",
		.body_text = ast_msg_get_body(mdata->msg)
	};

	pjsip_tx_data *tdata;
	RAII_VAR(char *, uri, NULL, ast_free);
	RAII_VAR(struct ast_sip_endpoint *, endpoint, get_outbound_endpoint(
			 mdata->to, &uri), ao2_cleanup);

	if (!endpoint) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not find endpoint and "
			"no default outbound endpoint configured\n");
		return -1;
	}

	if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not create request\n");
		return -1;
	}

	update_to(tdata, mdata->to);
	update_from(tdata, mdata->from);

	if (ast_sip_add_body(tdata, &body)) {
		pjsip_tx_data_dec_ref(tdata);
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
		return -1;
	}

	vars_to_headers(mdata->msg, tdata);

	if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
		ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");
		return -1;
	}

	return PJ_SUCCESS;
}

static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
{
	struct msg_data *mdata;

	if (ast_strlen_zero(to)) {
		ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI  must be specified\n");
		return -1;
	}

	if (!(mdata = msg_data_create(msg, to, from)) ||
	    ast_sip_push_task(NULL, msg_send, mdata)) {
		ao2_ref(mdata, -1);
		return -1;
	}
	return 0;
}

static const struct ast_msg_tech msg_tech = {
	.name = "pjsip",
	.msg_send = sip_msg_send,
};

static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
				 pjsip_dialog *dlg, pjsip_transaction *tsx)
{
	pjsip_tx_data *tdata;
	pj_status_t status;
	pjsip_response_addr res_addr;

	status = ast_sip_create_response(rdata, code, NULL, &tdata);
	if (status != PJ_SUCCESS) {
		ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
		return status;
	}

	if (dlg && tsx) {
		status = pjsip_dlg_send_response(dlg, tsx, tdata);
	} else {
		/* Get where to send request. */
		status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
		if (status != PJ_SUCCESS) {
			ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
			return status;
		}
		status = ast_sip_send_response(&res_addr, tdata, ast_pjsip_rdata_get_endpoint(rdata));
	}

	if (status != PJ_SUCCESS) {
		ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
	}

	return status;
}

static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
{
	enum pjsip_status_code code;
	struct ast_msg *msg;

	/* if not a MESSAGE, don't handle */
	if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
		return PJ_FALSE;
	}

	msg = ast_msg_alloc();
	if (!msg) {
		send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
		return PJ_TRUE;
	}

	if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
		send_response(rdata, code, NULL, NULL);
		return PJ_TRUE;
	}

	if ((code = rx_data_to_ast_msg(rdata, msg)) == PJSIP_SC_OK) {
		/* send it to the dialplan */
		ast_msg_queue(msg);
		code = PJSIP_SC_ACCEPTED;
	}

	send_response(rdata, code, NULL, NULL);
	return PJ_TRUE;
}

static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
{
	char buf[MAX_BODY_SIZE];
	enum pjsip_status_code code;
	struct ast_frame f;

	pjsip_dialog *dlg = session->inv_session->dlg;
	pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);

	if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
		send_response(rdata, code, dlg, tsx);
		return 0;
	}

	if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
		/* invalid body size */
		return 0;
	}

	ast_debug(3, "Received in dialog SIP message\n");

	memset(&f, 0, sizeof(f));
	f.frametype = AST_FRAME_TEXT;
	f.subclass.integer = 0;
	f.offset = 0;
	f.data.ptr = buf;
	f.datalen = strlen(buf) + 1;
	ast_queue_frame(session->channel, &f);

	send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
	return 0;
}
示例#23
0
static void print_loop_expr(struct loop_expr *e, int depth)
{
    printf("%sloop\n", indent(depth));
    print_body(e->body, depth+1);
    printf("%send loop\n", indent(depth));
}
示例#24
0
static void print_body_expr(struct body_expr *e, int depth)
{
    printf("%sbegin\n", indent(depth));
    print_body(e->body, depth+1);
    printf("%send\n", indent(depth));
}