Пример #1
0
/* copy the via lines from the message to the message 
   reply for correct routing of our reply.
*/
void cpy_vias(char *reply, char *dest){
	char *first_via, *middle_via, *last_via, *backup;

	/* lets see if we find any via */
	if ((first_via=STRSTR(reply, "VIA_STR"))==NULL &&
		(first_via=STRSTR(reply, "\nVIA_SHORT_STR"))==NULL ){
		printf("error: the received message doesn't contain a Via header\n");
		exit_code(3);
	}
	last_via=first_via+4;
	middle_via=last_via;
	/* proceed additional via lines */
	while ((middle_via=STRSTR(last_via, "VIA_STR"))!=NULL ||
		   (middle_via=STRSTR(last_via, "\nVIA_SHORT_STR"))!=NULL )
		last_via=middle_via+4;
	last_via=strchr(last_via, '\n');
	middle_via=strchr(dest, '\n')+1;
	/* make a backup, insert the vias after the first line and append 
	   backup
	*/
	backup=malloc(strlen(middle_via)+1);
	if (!backup) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	strcpy(backup, middle_via);
	strncpy(middle_via, first_via, (size_t)(last_via-first_via+1));
	strcpy(middle_via+(last_via-first_via+1), backup);
	free(backup);
	if (verbose > 2)
		printf("message reply with vias included:\n%s\n", reply);
}
Пример #2
0
void cpy_to(char *reply, char *dest) {
	char *src_to, *dst_to, *backup, *tmp;

	/* find the position where we want to insert the To */
	if ((dst_to=STRSTR(dest, "TO_STR"))==NULL &&
		(dst_to=STRSTR(dest, "\nTO_SHORT_STR"))==NULL) {
		printf("error: could not find To in the reply\n");
		exit_code(2);
	}
	/* find the To we want to copy */
	if ((src_to=STRSTR(reply, "TO_STR"))==NULL && 
		(src_to=STRSTR(reply, "\nTO_SHORT_STR"))==NULL) {
		if (verbose > 0)
			printf("warning: could not find To in reply. "
				"trying with original To\n");
	}
	else {
		/* both To found, so copy it */
		tmp=strchr(dst_to, '\n');
		tmp++;
		backup=malloc(strlen(tmp)+1);
		if (!backup) {
			printf("failed to allocate memory\n");
			exit_code(255);
		}
		strcpy(backup, tmp);
		tmp=strchr(src_to, '\n');
		strncpy(dst_to, src_to, (size_t)(tmp-src_to+1));
		strcpy(dst_to+(tmp-src_to+1), backup);
		free(backup);
		if (verbose >2)
			printf("reply with copyed To:\n%s\n", dest);
	}
}
Пример #3
0
static inline unsigned long srv_ruli(char *host, int *port, char *srv) {
	int srv_code;
	int ruli_opts = RULI_RES_OPT_SEARCH | RULI_RES_OPT_SRV_NOINET6 | RULI_RES_OPT_SRV_NOSORT6 | RULI_RES_OPT_SRV_NOFALL;
#ifdef RULI_RES_OPT_SRV_CNAME
	ruli_opts |= RULI_RES_OPT_SRV_CNAME;
#endif

	ruli_sync_t *sync_query = ruli_sync_query(srv, host, *port, ruli_opts);

	/* sync query failure? */
	if (!sync_query) {
		printf("DNS SRV lookup failed for: %s\n", host);
		exit_code(2, __PRETTY_FUNCTION__, "DNS SRV lookup failed");
	}

	srv_code = ruli_sync_srv_code(sync_query);
	/* timeout? */
	if (srv_code == RULI_SRV_CODE_ALARM) {
		printf("Timeout during DNS SRV lookup for: %s\n", host);
		ruli_sync_delete(sync_query);
		exit_code(2, __PRETTY_FUNCTION__, "timeout during DNS SRV lookup");
	}
	/* service provided? */
	else if (srv_code == RULI_SRV_CODE_UNAVAILABLE) {
		printf("SRV service not provided for: %s\n", host);
		ruli_sync_delete(sync_query);
		exit_code(2, __PRETTY_FUNCTION__, "missing service in DNS SRV reply");
	}
	else if (srv_code) {
		int rcode = ruli_sync_rcode(sync_query);
		if (verbose > 1)
			printf("SRV query failed for: %s, srv_code=%d, rcode=%d\n", host, srv_code, rcode);
		ruli_sync_delete(sync_query);
		return 0;
	}

	ruli_list_t *srv_list = ruli_sync_srv_list(sync_query);

	int srv_list_size = ruli_list_size(srv_list);

	if (srv_list_size < 1) {
		if (verbose > 1)
			printf("No SRV record: %s.%s\n", srv, host);
		return 0;
	}

	ruli_srv_entry_t *entry = (ruli_srv_entry_t *) ruli_list_get(srv_list, 0);
	ruli_list_t *addr_list = &entry->addr_list;
	int addr_list_size = ruli_list_size(addr_list);

	if (addr_list_size < 1) {
		printf("missing addresses in SRV lookup for: %s\n", host);
		ruli_sync_delete(sync_query);
		exit_code(2, __PRETTY_FUNCTION__, "missing address in DNS SRV reply");
	}

	*port = entry->port;
	ruli_addr_t *addr = (ruli_addr_t *) ruli_list_get(addr_list, 0);
	return addr->addr.ipv4.s_addr;
}
Пример #4
0
/* Finds the SRV records for the given host. It returns the target IP
 * address and fills the port and transport if a suitable SRV record
 * exists. Otherwise it returns 0. The function follows 3263: first
 * TLS, then TCP and finally UDP. */
unsigned long getsrvadr(char *host, int *port, unsigned int *transport) {
	unsigned long adr = 0;

#ifdef HAVE_SRV
	int srvport = 5060;

#ifdef HAVE_CARES_H
	int status;
	int optmask = ARES_OPT_FLAGS;
	struct ares_options options;

	options.flags = ARES_FLAG_NOCHECKRESP;
	options.servers = NULL;
	options.nservers = 0;

	status = ares_init_options(&channel, &options, optmask);
	if (status != ARES_SUCCESS) {
		printf("error: failed to initialize ares\n");
		exit_code(2);
	}
#endif

#ifdef WITH_TLS_TRANSP
	adr = getsrvaddress(host, &srvport, SRV_SIP_TLS);
	if (adr != 0) {
		*transport = SIP_TLS_TRANSPORT;
		if (verbose > 1)
			printf("using SRV record: %s.%s:%i\n", SRV_SIP_TLS, host, srvport);
		printf("TLS transport not yet supported\n");
		exit_code(2);
	}
	else {
#endif
		adr = getsrvaddress(host, &srvport, SRV_SIP_TCP);
		if (adr != 0) {
			*transport = SIP_TCP_TRANSPORT;
			if (verbose > 1)
				printf("using SRV record: %s.%s:%i\n", SRV_SIP_TCP, host, srvport);
		}
		else {
			adr = getsrvaddress(host, &srvport, SRV_SIP_UDP);
			if (adr != 0) {
				*transport = SIP_UDP_TRANSPORT;
				if (verbose > 1)
					printf("using SRV record: %s.%s:%i\n", SRV_SIP_UDP, host, srvport);
			}
		}
#ifdef WITH_TLS_TRANSP
	}
#endif

#ifdef HAVE_CARES_H
	ares_destroy(channel);
#endif

	*port = srvport;
#endif // HAVE_SRV
	return adr;
}
Пример #5
0
inline unsigned long srv_ares(char *host, int *port, char *srv) {
	int nfds, count, srvh_len;
	char *srvh;
	fd_set read_fds, write_fds;
	struct timeval *tvp, tv;

	caport = 0;
	caadr = 0;
	ca_tmpname = NULL;
#ifdef DEBUG
	printf("!!! ARES query !!!\n");
#endif

	srvh_len = strlen(host) + strlen(srv) + 2;
	srvh = malloc(srvh_len);
	if (srvh == NULL) {
		printf("error: failed to allocate memory (%i) for ares query\n", srvh_len);
		exit_code(2);
	}
	memset(srvh, 0, srvh_len);
	strncpy(srvh, srv, strlen(srv));
	memcpy(srvh + strlen(srv), ".", 1);
	strcpy(srvh + strlen(srv) + 1, host);
#ifdef DEBUG
	printf("hostname: '%s', len: %i\n", srvh, srvh_len);
#endif

	ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL);
#ifdef DEBUG
	printf("after ares_query\n");
#endif
	/* wait for query to complete */
	while (1) {
		FD_ZERO(&read_fds);
		FD_ZERO(&write_fds);
		nfds = ares_fds(channel, &read_fds, &write_fds);
		if (nfds == 0)
			break;
		tvp = ares_timeout(channel, NULL, &tv);
		count = select(nfds, &read_fds, &write_fds, NULL, tvp);
		if (count < 0 && errno != EINVAL) {
			perror("ares select");
			exit_code(2);
		}
		ares_process(channel, &read_fds, &write_fds);
	}
#ifdef DEBUG
	printf("end of while\n");
#endif
	*port = caport;
	if (caadr == 0 && ca_tmpname != NULL) {
		caadr = getaddress(ca_tmpname);
	}
	if (ca_tmpname != NULL)
		free(ca_tmpname);
	free(srvh);
	return caadr;
}
Пример #6
0
/* add a Via Header Field in the message. */
void add_via(char *mes)
{
	char *via_line, *via, *via2, *backup; 

	/* first build our own Via-header-line */
	via_line = malloc(VIA_SIP_STR_LEN+strlen(fqdn)+15);
	if (!via_line) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	snprintf(via_line, VIA_SIP_STR_LEN+strlen(fqdn)+5+10, "%s%s:%i;rport\r\n", VIA_SIP_STR, fqdn, lport);
	if (verbose > 2)
		printf("our Via-Line: %s\n", via_line);

	if (strlen(mes)+strlen(via_line)>= BUFSIZE){
		printf("can't add our Via Header Line because file is too big\n");
		exit_code(2);
	}
	via=STRSTR(mes, "\nVIA_STR");
	via2=STRSTR(mes, "\nVIA_SHORT_STR");
	if (via==NULL && via2==NULL ){
		/* We doesn't find a Via so we insert our via
		   direct after the first line. */
		via=strchr(mes,'\n');
		if(via == NULL) {
			printf("failed to find new line to insert Via\n");
			return;
		}
	}
	else if (via!=NULL && via2!=NULL && via2<via){
		/* the short via is above the long version */
		via = via2;
	}
	else if (via==NULL && via2!=NULL){
		/* their is only a short via */
		via = via2;
	}
	via++;
	if (!via) {
		printf("failed to find Via header\n");
		exit_code(1);
	}
	/* finnaly make a backup, insert our via and append the backup */
	backup=malloc((strlen(via)+1));
	if (!backup) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	strncpy(backup, via, strlen(via)+1);
	strncpy(via, via_line, strlen(via_line));
	strncpy(via+strlen(via_line), backup, strlen(backup)+1);
	if (verbose > 1)
		printf("New message with Via-Line:\n%s\n", mes);
	free(via_line);
	free(backup);
}
Пример #7
0
static inline void on_success(char *rep)
{
	if ((rep != NULL) && re && regexec(re, rep, 0, 0, 0) == REG_NOMATCH) {
		log_message(req);
		fprintf(stderr, "error: RegExp failed\n");
		exit_code(32, __PRETTY_FUNCTION__, "regular expression failed");
	} else {
		exit_code(0, __PRETTY_FUNCTION__, NULL);
	}
}
Пример #8
0
/* tries to take care of a redirection */
void handle_3xx(struct sockaddr_in *tadr)
{
	char *uscheme, *uuser, *uhost, *contact;

	printf("** received redirect ");
	if (warning_ext == 1) {
		printf("from ");
		warning_extract(rec);
		printf("\n");
	}
	else
		printf("\n");
	/* we'll try to handle 301 and 302 here, other 3xx are to complex */
	regcomp(&(regexps.redexp), "^SIP/[0-9]\\.[0-9] 30[125] ", 
			REG_EXTENDED|REG_NOSUB|REG_ICASE);
	if (regexec(&(regexps.redexp), rec, 0, 0, 0) == REG_NOERROR) {
		/* try to find the contact in the redirect */
		contact = uri_from_contact(rec);
		if (contact==NULL) {
			fprintf(stderr, "error: cannot find Contact in this "
				"redirect:\n%s\n", rec);
			exit_code(3, __PRETTY_FUNCTION__, "missing Contact header in reply");
		}
		/* correct our request */
		uri_replace(req, contact);
		new_transaction(req, rep);
		/* extract the needed information*/
		rport = 0;
		address = 0;
		parse_uri(contact, &uscheme, &uuser, &uhost, &rport);
		if (!rport)
			address = getsrvadr(uhost, &rport, &transport);
		if (!address)
			address = getaddress(uhost);
		if (!address){
			fprintf(stderr, "error: cannot determine host "
					"address from Contact of redirect:"
					"\n%s\n", rec);
			exit_code(2, __PRETTY_FUNCTION__, "missing host in Contact header");
		}
		if (!rport) {
			rport = 5060;
		}
		free(contact);
		if (!outbound_proxy)
			cdata.connected = set_target(tadr, address, rport, cdata.csock, cdata.connected);
	}
	else {
		fprintf(stderr, "error: cannot handle this redirect:"
				"\n%s\n", rec);
		exit_code(2, __PRETTY_FUNCTION__, "unsupported redirect reply");
	}
}
Пример #9
0
static inline unsigned long srv_ares(char *host, int *port, char *srv) {
	int nfds, count, srvh_len;
	char *srvh;
	fd_set read_fds, write_fds;
	struct timeval *tvp, tv;

	caport = 0;
	caadr = 0;
	ca_tmpname = NULL;
	dbg("starting ARES query\n");

	srvh_len = strlen(host) + strlen(srv) + 2;
	srvh = malloc(srvh_len);
	if (srvh == NULL) {
		printf("error: failed to allocate memory (%i) for ares query\n", srvh_len);
		exit_code(2, __PRETTY_FUNCTION__, "memory allocation failure");
	}
	memset(srvh, 0, srvh_len);
	strncpy(srvh, srv, strlen(srv));
	memcpy(srvh + strlen(srv), ".", 1);
	strcpy(srvh + strlen(srv) + 1, host);
	dbg("hostname: '%s', len: %i\n", srvh, srvh_len);

	ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL);
	dbg("ares_query finished, waiting for result...\n");
	/* wait for query to complete */
	while (1) {
		FD_ZERO(&read_fds);
		FD_ZERO(&write_fds);
		nfds = ares_fds(channel, &read_fds, &write_fds);
		if (nfds == 0)
			break;
		tvp = ares_timeout(channel, NULL, &tv);
		count = select(nfds, &read_fds, &write_fds, NULL, tvp);
		if (count < 0 && errno != EINVAL) {
			perror("ares select");
			exit_code(2, __PRETTY_FUNCTION__, "ares DNS resolution failure");
		}
		ares_process(channel, &read_fds, &write_fds);
	}
	dbg("ARES answer processed\n");
	*port = caport;
	if (caadr == 0 && ca_tmpname != NULL) {
		caadr = getaddress(ca_tmpname);
	}
	if (ca_tmpname != NULL)
		free(ca_tmpname);
	free(srvh);
	return caadr;
}
Пример #10
0
/* check for the existence of a Max-Forwards header field. if its 
   present it sets it to the given value, if not it will be inserted.*/
void set_maxforw(char *mes){
	char *max, *backup, *crlfi;

	if ((max=STRSTR(mes, "MAX_FRW_STR"))==NULL){
		/* no max-forwards found so insert it after the first line*/
		max=strchr(mes,'\n');
		if (!max) {
			printf("failed to find newline\n");
			exit_code(254);
		}
		max++;
		backup=malloc(strlen(max)+1);
		if (!backup) {
			printf("failed to allocate memory\n");
			exit_code(255);
		}
		strncpy(backup, max, (size_t)(strlen(max)+1));
		snprintf(max, MAX_FRW_STR_LEN+6, "%s%i\r\n", MAX_FRW_STR, maxforw);
		max=strchr(max,'\n');
		max++;
		strncpy(max, backup, strlen(backup)+1);
		free(backup);
		if (verbose > 1)
			printf("Max-Forwards %i inserted into header\n", maxforw);
		if (verbose > 2)
			printf("New message with inserted Max-Forwards:\n%s\n", mes);
	}
	else{
		/* found max-forwards => overwrite the value with maxforw*/
		crlfi=strchr(max,'\n');
		crlfi++;
		backup=malloc(strlen(crlfi)+1);
		if (!backup) {
			printf("failed to allocate memory\n");
			exit_code(255);
		}
		strncpy(backup, crlfi, strlen(crlfi)+1);
		crlfi=max + MAX_FRW_STR_LEN;
		snprintf(crlfi, 6, "%i\r\n", maxforw);
		crlfi=strchr(max,'\n');
		crlfi++;
		strncpy(crlfi, backup, strlen(backup)+1);
		free(backup);
		if (verbose > 1)
			printf("Max-Forwards set to %i\n", maxforw);
		if (verbose > 2)
			printf("New message with changed Max-Forwards:\n%s\n", mes);
	}
}
Пример #11
0
void init_con_data(struct sipsak_con_data *cd) {
	switch(address.af) {
		case AF_INET:
#ifdef DEBUG
			printf("ipv4 bind\n");
#endif
			memset(&cd->adr, 0, sizeof(struct sockaddr_in));
			cd->in_adr.sin_family = AF_INET;
			cd->in_adr.sin_addr.s_addr = htonl( INADDR_ANY);
			cd->in_adr.sin_port = htons((short)lport);
			break;
		case AF_INET6:
#ifdef DEBUG
			printf("ipv6 bind\n");
#endif
			memset(&cd->adr, 0, sizeof(struct sockaddr_in6));
			cd->in6_adr.sin6_family = AF_INET6;
			cd->in6_adr.sin6_addr = in6addr_any;
			cd->in6_adr.sin6_port = htons((short)lport);
			break;
		default:
			fprintf(stderr, "invalid address family: %d\n", cd->in_adr.sin_family);
			exit_code(2);
	}
}
Пример #12
0
void send_message(char* mes, struct sipsak_con_data *cd,
			struct sipsak_counter *sc, struct sipsak_sr_time *srt) {
	struct timezone tz;
	int ret;

	if (cd->dontsend == 0) {
		if (verbose > 2) {
			printf("\nrequest:\n%s", mes);
		}
		/* lets fire the request to the server and store when we did */
		if (cd->csock == -1) {
			ret = sendto(cd->usock, mes, strlen(mes), 0, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr));
		}
		else {
			ret = send(cd->csock, mes, strlen(mes), 0);
		}
		(void)gettimeofday(&(srt->sendtime), &tz);
		if (ret==-1) {
			printf("\n");
			perror("send failure");
			exit_code(2);
		}
#ifdef HAVE_INET_NTOP
		if (verbose > 2) {
			printf("\nsend to: %s:%s:%i\n", transport_str, target_dot, rport);
    }
#endif
		sc->send_counter++;
	}
	else {
		cd->dontsend = 0;
	}
}
Пример #13
0
void print_long_help() {
	print_version();
	printf(
		"  --help                     displays this help message\n"
		"  --version                  prints version string only\n"
		"  --filename=FILE            the file which contains the SIP message to send\n"
		"                               use - for standard input\n"
		"  --no-crlf                  de-activate CR (\\r) insertion\n"
		"  --sip-uri=SIPURI           the destination server uri in form\n"
		"                               sip:[user@]servername[:port]\n"
		"  --traceroute               activates the traceroute mode\n"
		);
	printf("  --usrloc-mode              activates the usrloc mode\n"
		"  --invite-mode              simulates a successful calls with itself\n"
		"  --message-mode             sends messages to itself\n"
		"  --contact=SIPURI           use the given uri as Contact in REGISTER\n"
		"  --appendix-begin=NUMBER    the starting number appendix to the user name (default: 0)\n"
		"  --appendix-end=NUMBER      the ending numer of the appendix to the user name\n"
		"  --sleep=NUMBER             sleep number ms before sending next request\n"
		);
	printf("  --expires=NUMBER           the expires header field value (default: 15)\n"
		"  --remove-bindings=NUMBER   activates randomly removing of user bindings\n"
		"  --flood-mode               activates the flood mode\n"
		"  --random-mode              activates the random modues (dangerous)\n"
		"  --trash-chars=NUMBER       the maximum number of trashed character in random mode\n"
		"                               (default: request length)\n"
		);
	printf("  --local-port=PORT          the local port to use (default: any)\n"
		"  --remote-port=PORT         the remote port to use (default: 5060)\n"
		"  --outbound-proxy=HOSTNAME  request target (outbound proxy)\n"
		"  --hostname=HOSTNAME        overwrites the local hostname in all headers\n"
		"  --max-forwards=NUMBER      the value for the max-forwards header field\n"
		"  --numeric                  use FQDN instead of IPs in the Via-Line\n");
	printf("  --processes=NUMBER         Divide the workflow among the number of processes\n"
		"  --auth-username=STRING     username for authentication\n"
		);
	printf("  --no-via                   deactivate the insertion of a Via-Line\n"
		"  --password=PASSWORD        password for authentication\n"
		"                               (if omitted password=username)\n"
		"  --ignore-redirects         ignore redirects\n"
		"  --verbose                  each v produces more verbosity (max. 3)\n"
		"  --extract-ip               extract IP from the warning in reply\n"
		"  --replace-string=STRING    replacement for a special mark in the message\n"
		"  --replace                  activates replacement of variables\n"
		);
	printf("  --nagios-code              returns exit codes Nagios compliant\n"
		"  --nagios-warn=NUMBER       return Nagios warning if retrans > number\n"
		"  --message-body=STRING      send a message with string as body\n"
		"  --disposition=STRING       Content-Disposition value\n"
		"  --search=REGEXP            search for a RegExp in replies and return error\n"
		"                             on failfure\n"
		"  --timing                   print the timing informations at the end\n"
		"  --symmetric                send and received on the same port\n"
		"  --from=SIPURI              use the given uri as From in MESSAGE\n"
		"  --timeout-factor=NUMBER    timeout multiplier for INVITE transactions\n"
		"                             and reliable transports (default: 64)\n"
		"  --transport=STRING         specify transport to be used\n"
		);
	exit_code(0);
}
Пример #14
0
/* replace the Content-Length value with the given value */
void set_cl(char* mes, int contentlen) {
	char *cl, *cr, *backup;

	if ((cl=STRSTR(ack, CON_LEN_STR)) == NULL &&
		(cl=STRSTR(ack, CON_LEN_SHORT_STR)) == NULL) {
		printf("missing Content-Lenght in message\n");
		return;
	}
	cr = strchr(cl, '\n');
	cr++;
	backup=malloc(strlen(cr)+1);
	if (!backup) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	strncpy(backup, cr, strlen(cr)+1);
	if (*cl == 'C')
		cr=cl + CON_LEN_STR_LEN;
	else
		cr=cl + 3;
	snprintf(cr, 6, "%i\r\n", contentlen);
	cr=strchr(cr, '\n');
	cr++;
	strncpy(cr, backup, strlen(backup)+1);
	free(backup);
	if (verbose > 1)
		printf("Content-Length set to %i\n", contentlen);
	if (verbose > 2)
		printf("New message with changed Content-Length:\n%s\n", mes);
}
Пример #15
0
/* tryes to find the warning header filed and prints out the IP */
void warning_extract(char *message)
{
	char *warning, *end, *mid, *server;
	int srvsize;

	if ((warning=STRSTR(message, "Warning:"))==NULL) {
		if (verbose > 0) printf("'no Warning header found' ");
		else printf("?? ");
		return;
	}
	end=strchr(warning, '"');
	end--;
	warning=strchr(warning, '3');
	warning=warning+4;
	mid=strchr(warning, ':');
	if (mid) end=mid;
	srvsize=end - warning + 1;
	server=malloc((size_t)srvsize);
	if (!server) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	memset(server, 0, (size_t)srvsize);
	server=strncpy(server, warning, (size_t)(srvsize - 1));
	printf("%s ", server);
	free(server);
}
Пример #16
0
unsigned long getaddress(char *host) {
	struct hostent* pent;
	long addr;

	if (strlen(host) == 0) {
		return 0;
	}
	if (is_ip(host)) {
		return inet_addr(host);
	}

	/* try the system's own resolution mechanism for dns lookup:
	 required only for domain names.
	 in spite of what the rfc2543 :D Using SRV DNS Records recommends,
	 we are leaving it to the operating system to do the name caching.

	 this is an important implementational issue especially in the light
	 dynamic dns servers like dynip.com or dyndns.com where a dial
	 ip address is dynamically assigned a sub domain like farhan.dynip.com

	 although expensive, this is a must to allow OS to take
	 the decision to expire the DNS records as it deems fit.
	*/
	pent = gethostbyname(host);
	if (!pent) {
		printf("'%s' is unresolveable\n", host);
		exit_code(2, __PRETTY_FUNCTION__, "hostname is not resolveable");
	}
	addr = *(uint32_t *) (pent->h_addr);
	return addr;
}
Пример #17
0
void increase_cseq(char *message)
{
	int cs;
	char *cs_s, *eol, *backup;

	cs = cseq(message);
	if ((cs < 1) && (verbose > 1)) {
		printf("CSeq increase failed because unable to extract CSeq number\n");
		return;
	}
	cs++;
	cs_s=STRSTR(message, CSEQ_STR);
	if (cs_s) {
		cs_s+=6;
		eol=strchr(cs_s, ' ');
		eol++;
		backup=malloc(strlen(eol)+1);
		if (!backup) {
			printf("failed to allocate memory\n");
			exit_code(255);
		}
		strncpy(backup, eol, (size_t)(strlen(eol)+1));
		snprintf(cs_s, 11, "%i ", cs);
		cs_s+=strlen(cs_s);
		strncpy(cs_s, backup, strlen(backup));
		free(backup);
	}
	else if (verbose > 1)
		printf("'CSeq' not found in message\n");
}
Пример #18
0
/* prints out some usage help and exits */
void print_help() {
	print_version();
	printf(
		"  -h                displays this help message\n"
		"  -V                prints version string only\n"
		"  -f FILE           the file which contains the SIP message to send\n"
		"                      use - for standard input\n"
		"  -L                de-activate CR (\\r) insertion in files\n"
		"  -s SIPURI         the destination server uri in form\n"
		"                      sip:[user@]servername[:port]\n"
		"  -T                activates the traceroute mode\n"
		"  -U                activates the usrloc mode\n"
		"  -I                simulates a successful calls with itself\n"
		"  -M                sends messages to itself\n"
		);
	printf(
		"  -C SIPURI         use the given uri as Contact in REGISTER\n"
		"  -b NUMBER         the starting number appendix to the user name (default: 0)\n"
		"  -e NUMBER         the ending numer of the appendix to the user name\n"
		"  -o NUMBER         sleep number ms before sending next request\n"
		"  -x NUMBER         the expires header field value (default: 15)\n"
		"  -z                activates randomly removing of user bindings\n"
		"  -F                activates the flood mode\n"
		);
	printf(
		"  -c NUMBER         the maximum CSeq number for flood mode (default: 2^31)\n"
		"  -R                activates the random modues (dangerous)\n"
		"  -t NUMBER         the maximum number of trashed character in random mode\n"
		"                      (default: request length)\n"
		"  -l PORT           the local port to use (default: any)\n"
		"  -r PORT           the remote port to use (default: 5060)\n"
		"  -p HOSTNAME       request target (outbound proxy)\n"
		);
	printf(
		"  -H HOSTNAME       overwrites the local hostname in all headers\n"
		"  -m NUMBER         the value for the max-forwards header field\n"
		"  -n                use FQDN instead of IPs in the Via-Line\n"
		"  -i                deactivate the insertion of a Via-Line\n"
		"  -a PASSWORD       password for authentication\n"
		"                      (if omitted password=\"\")\n"
		"  -u STRING         Authentication username\n"
		);
	printf(
		"  -d                ignore redirects\n"
		"  -v                each v produces more verbosity (max. 3)\n"
		"  -w                extract IP from the warning in reply\n"
		"  -g STRING         replacement for a special mark in the message\n"
		"  -G                activates replacement of variables\n"
		"  -N                returns exit codes Nagios compliant\n"
		"  -q STRING         search for a RegExp in replies and return error\n"
		"                    on failure\n");
	printf("  -W NUMBER         return Nagios warning if retrans > number\n"
		"  -B STRING         send a message with string as body\n"
		"  -O STRING         Content-Disposition value\n"
		"  -P NUMBER         Number of processes to start\n"
		"  -A                print timing informations\n"
		);
		exit_code(0);
}
Пример #19
0
  void Environment::halt_and_exit(STATE) {
    halt(state);
    int code = exit_code(state);
#ifdef ENABLE_LLVM
    llvm::llvm_shutdown();
#endif
    exit(code);
  }
Пример #20
0
		bool is_running(HANDLE process) 
		{
			if (exit_code(process) == STILL_ACTIVE) 
			{
				return wait(process, 0);
			}

			return false;
		}
Пример #21
0
/* checks if the strings contains special double marks and then
 * replace all occurences of this strings in the message */
void replace_strings(char *mes, char *strings) {
	char *pos, *atr, *val, *repl, *end;
	char sep;

	pos=atr=val=repl = NULL;
#ifdef DEBUG
	printf("replace_strings entered\nstrings: '%s'\n", strings);
#endif
	if ((isalnum(*strings) != 0) && 
		(isalnum(*(strings + strlen(strings) - 1)) != 0)) {
		replace_string(req, "$replace$", replace_str);
	}
	else {
		sep = *strings;
#ifdef DEBUG
		printf("sep: '%c'\n", sep);
#endif
		end = strings + strlen(strings);
		pos = strings + 1;
		while (pos < end) {
			atr = pos;
			pos = strchr(atr, sep);
			if (pos != NULL) {
				*pos = '\0';
				val = pos + 1;
				pos = strchr(val, sep);
				if (pos != NULL) {
					*pos = '\0';
					pos++;
				}
			}
#ifdef DEBUG
			printf("atr: '%s'\nval: '%s'\n", atr, val);
#endif
			if ((atr != NULL) && (val != NULL)) {
				repl = str_alloc(strlen(val) + 3);
				if (repl == NULL) {
					printf("failed to allocate memory\n");
					exit_code(2);
				}
				sprintf(repl, "$%s$", atr);
				replace_string(mes, repl, val);
				free(repl);
			}
#ifdef DEBUG
			printf("pos: '%s'\n", pos);
#endif
		}
	}
#ifdef DEBUG
	printf("mes:\n'%s'\n", mes);
#endif
}
Пример #22
0
/* takes care of replies in the readntrash mode */
void handle_randtrash()
{
	/* in randomzing trash we are expexting 4?? error codes
	   everything else should not be normal */
	if (regexec(&(regexps.errexp), rec, 0, 0, 0) == REG_NOERROR) {
		if (verbose > 2)
			printf("received:\n%s\n", rec);
		if (verbose > 1) {
			printf("received expected 4xx ");
			if (warning_ext == 1) {
				printf ("from ");
				warning_extract(rec);
				printf("\n");
			}
			else {
				printf("\n");
			}
		}
	}
	else {
		fprintf(stderr, "warning: did not received 4xx\n");
		if (verbose > 1) 
			printf("sended:\n%s\nreceived:\n%s\n", req, rec);
	}
	if (cseq_counter == nameend) {
		if (counters.randretrys == 0) {
			printf("random end reached. server survived :) respect!\n");
			exit_code(0, __PRETTY_FUNCTION__, NULL);
		}
		else {
			printf("maximum sendings reached but did not "
				"get a response on this request:\n%s\n", req);
			log_message(req);
			exit_code(3, __PRETTY_FUNCTION__, "missing reply on trashed request");
		}
	}
	else {
		trash_random(req);
	}
}
Пример #23
0
/* tries to convert the given string into an integer. it strips
 * white-spaces and exits if an error happens */
int str_to_int(char *num)
{
	int ret;

#ifdef HAVE_STRTOL
	errno = 0;
	ret = strtol(num, NULL, 10);
	if (errno == EINVAL || errno == ERANGE) {
		printf("%s\n", num);
		perror("integer converting error");
		exit_code(2);
	}
#else
	char backup;
	int len = strlen(num);
	char *start = num;
	char *end = num + len;

	while (!isdigit(*start) && isspace(*start) && start < end)
		start++;
	end = start;
	end++;
	while (end < num + len && *end != '\0' && !isspace(*end))
		end++;
	backup = *end;
	*end = '\0';
	if (!is_number(start)) {
		fprintf(stderr, "error: string is not a number: %s\n", start);
		exit_code(2);
	}
	ret = atoi(start);
	*end = backup;
	if (ret <= 0) {
		fprintf(stderr, "error: failed to convert string to integer: %s\n", num);
		exit_code(2);
	}
#endif
	return ret;
}
Пример #24
0
/* Finds the SRV records for the given host. It returns the target IP
 * address and fills the port and transport if a suitable SRV record
 * exists. Otherwise it returns 0. The function follows 3263: first
 * TLS, then TCP and finally UDP. */
int getsrvadr(char *host, struct addrinfo *res) {
	int ret = -1;

#ifdef HAVE_SVR

#ifdef HAVE_CARES_H
	int status;
	int optmask = ARES_OPT_FLAGS;
	struct ares_options options;

	options.flags = ARES_FLAG_NOCHECKRESP;
	options.servers = NULL;
	options.nservers = 0;

	status = ares_init_options(&channel, &options, optmask);
	if (status != ARES_SUCCESS) {
		printf("error: failed to initialize ares\n");
		exit_code(2, __PRETTY_FUNCTION__, "failed to init ares lib");
	}
#endif

#ifdef WITH_TLS_TRANSP
	printf("TLS transport not yet supported\n");
	exit_code(2);
#endif
	ret = getsrvaddress(host, service, SRV_SIP_TCP, SIP_TCP_TRANSPORT, res);
	if (ret >= 0) {
		if (verbose > 1)
			printf("using SRV record: %s.%s:%i\n", SRV_SIP_TLS, host, srvport);
	}
	else {
		ret = getsrvaddress(host, service, SRV_SIP_UDP, SIP_UDP_TRANSPORT, res);
		if (ret >= 0) {
			if (verbose > 1)
				printf("using SRV record: %s.%s:%s\n", SRV_SIP_UDP, host, service);
		}
	}
#ifdef WITH_TLS_TRANSP
	}
Пример #25
0
/* replaces the uri in first line of mes with the other uri */
void uri_replace(char *mes, char *uri)
{
	char *foo, *backup;

	foo=strchr(mes, '\n');
	if (!foo) {
		printf("failed to find newline\n");
		exit_code(254);
	}
	foo++;
	backup=malloc(strlen(foo)+1);
	if (!backup) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	strncpy(backup, foo, strlen(foo)+1);
	foo=STRSTR(mes, "sip");
	strncpy(foo, uri, strlen(uri));
	strncpy(foo+strlen(uri), SIP20_STR, SIP20_STR_LEN);
	strncpy(foo+strlen(uri)+SIP20_STR_LEN, backup, strlen(backup)+1);
	free(backup);
	if (verbose > 2)
		printf("Message with modified uri:\n%s\n", mes);
}
Пример #26
0
/* tries to allocate the given size of memory and sets it all to zero.
 * if the allocation fails it exits */
void *str_alloc(size_t size) {
	char *ptr;
#ifdef HAVE_CALLOC
	ptr = calloc(1, size);
#else
	ptr = malloc(size);
#endif
	if (ptr == NULL) {
		fprintf(stderr, "error: memory allocation failed\n");
		exit_code(255, __PRETTY_FUNCTION__, "memory allocation failure");
	}
#ifndef HAVE_CALLOC
	memset(ptr, 0, size);
#endif
	return ptr;
}
Пример #27
0
/* sipmly swappes the content of the two buffers */
void swap_buffers(char *fst, char *snd) {
	char *tmp;

	if (fst == snd)
		return;
	tmp = malloc(strlen(fst)+1);
	if (!tmp) {
		printf("failed to allocate memory\n");
		exit_code(255);
	}
	memset(tmp, 0, strlen(fst)+1);
	strcpy(tmp, fst);
	strcpy(fst, snd);
	strcpy(snd, tmp);
	free(tmp);
}
Пример #28
0
long getaddress(char *host)
{
	int i=0;
	int dotcount=0;
	char *p = host;
	struct hostent* pent;
	long l, *lp;

	/*try understanding if this is a valid ip address
	we are skipping the values of the octets specified here.
	for instance, this code will allow 952.0.320.567 through*/
	while (*p != '\0')
	{
		for (i = 0; i < 3; i++, p++)
			if (isdigit((int)*p) == 0)
				break;
		if (*p != '.')
			break;
		p++;
		dotcount++;
	}

	/* three dots with upto three digits in before, between and after ? */
	if (dotcount == 3 && i > 0 && i <= 3)
		return inet_addr(host);

	/* try the system's own resolution mechanism for dns lookup:
	 required only for domain names.
	 inspite of what the rfc2543 :D Using SRV DNS Records recommends,
	 we are leaving it to the operating system to do the name caching.

	 this is an important implementational issue especially in the light
	 dynamic dns servers like dynip.com or dyndns.com where a dial
	 ip address is dynamically assigned a sub domain like farhan.dynip.com

	 although expensive, this is a must to allow OS to take
	 the decision to expire the DNS records as it deems fit.
	*/
	pent = gethostbyname(host);
	if (!pent) {
		printf("'%s' is unresolveable\n", host);
		exit_code(2);
	}
	lp = (long *) (pent->h_addr);
	l = *lp;
	return l;
}
Пример #29
0
//**********************************************************************************
//	
//**********************************************************************************
CModalMessageBox::eExitCode	CModalMessageBox::Show()
{
	CProcess::Pause( true );

	while ( 1 )
	{
		eExitCode	exit_code( EXIT_NULL );

		CFrameWork::Process();

		if ( m_bExitCross == true && CInput::IsButtonClicked( CInput::CROSS ) == true )
		{
			exit_code = EXIT_CROSS;
		}

		if ( m_bExitCircle == true && CInput::IsButtonClicked( CInput::CIRCLE ) == true )
		{
			exit_code = EXIT_CIRCLE;
		}

		if ( m_bExitSquare == true && CInput::IsButtonClicked( CInput::SQUARE ) == true )
		{
			exit_code = EXIT_SQUARE;
		}

		if ( m_bExitTriangle == true && CInput::IsButtonClicked( CInput::TRIANGLE ) == true )
		{
			exit_code = EXIT_TRIANGLE;
		}

		//
		//	Have we clicked an exit button?
		//
		if ( exit_code != EXIT_NULL )
		{
			SetVisible( false );

			CProcess::Pause( false );

			return exit_code;
		}
	}

	return EXIT_TRIANGLE;
}
Пример #30
0
void check_socket_error(int socket, int size) {
	struct pollfd sockerr;
	int ret = 0;

	/* lets see if we at least received an icmp error */
	sockerr.fd=socket;
	sockerr.events=POLLERR;
	ret = poll(&sockerr, 1, 10);
	if (ret==1) {
		if (sockerr.revents && POLLERR) {
			recvfrom(socket, recv, size, 0, NULL, 0);
			printf("\n");
			perror("send failure");
			if (randtrash == 1) 
				printf ("last message before send failure:\n%s\n", req);
			exit_code(3);
		}
	}
}