- re-worked str_to_int one more time as the previous

behavior was broken after the last fix
- added a centralized debug message function

git-svn-id: http://svn.berlios.de/svnroot/repos/sipsak/trunk@425 75b5f7c7-cfd4-0310-b54c-e118b2c5249a
(cherry picked from commit c003018761a3373ae534ce603b072a2a75594c78)
(cherry picked from commit 133160868a)
mr3.8.5
nils-ohlmeier 19 years ago committed by Victor Seva
parent 62b8c53f14
commit 1bf0540dd3

@ -130,6 +130,9 @@
/* Define to 1 if you have the `socket' function. */ /* Define to 1 if you have the `socket' function. */
#undef HAVE_SOCKET #undef HAVE_SOCKET
/* Define to 1 if you have the <stdarg.h> header file. */
#undef HAVE_STDARG_H
/* Define to 1 if you have the <stdint.h> header file. */ /* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H #undef HAVE_STDINT_H

21
configure vendored

@ -5088,7 +5088,26 @@ fi
done done
for ac_header in ctype.h errno.h arpa/inet.h netdb.h netinet/in.h netinet/in_systm.h limits.h sys/poll.h regex.h signal.h stdlib.h stdio.h string.h sys/param.h sys/socket.h sys/time.h unistd.h sys/utsname.h
for ac_header in ctype.h errno.h arpa/inet.h netdb.h netinet/in.h netinet/in_systm.h limits.h sys/poll.h regex.h signal.h stdarg.h stdlib.h stdio.h string.h sys/param.h sys/socket.h sys/time.h unistd.h sys/utsname.h
do do
as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh` as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then

@ -34,7 +34,7 @@ AC_SUBST([LIBS])
# Checks for header files. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([ctype.h errno.h arpa/inet.h netdb.h netinet/in.h netinet/in_systm.h limits.h sys/poll.h regex.h signal.h stdlib.h stdio.h string.h sys/param.h sys/socket.h sys/time.h unistd.h sys/utsname.h],,[AC_MSG_ERROR([missing required header (see above)])],) AC_CHECK_HEADERS([ctype.h errno.h arpa/inet.h netdb.h netinet/in.h netinet/in_systm.h limits.h sys/poll.h regex.h signal.h stdarg.h stdlib.h stdio.h string.h sys/param.h sys/socket.h sys/time.h unistd.h sys/utsname.h],,[AC_MSG_ERROR([missing required header (see above)])],)
AC_CHECK_HEADERS([getopt.h]) AC_CHECK_HEADERS([getopt.h])
AC_HEADER_SYS_WAIT AC_HEADER_SYS_WAIT
AC_HEADER_TIME AC_HEADER_TIME

@ -198,7 +198,7 @@ void set_maxforw(char *mes, int value){
strncpy(backup, crlfi, strlen(crlfi)); strncpy(backup, crlfi, strlen(crlfi));
crlfi=max + MAX_FRW_STR_LEN; crlfi=max + MAX_FRW_STR_LEN;
if (value == -1) { if (value == -1) {
maxforward = str_to_int(crlfi); maxforward = str_to_int(1, crlfi);
maxforward++; maxforward++;
} }
else { else {
@ -286,7 +286,7 @@ int get_cl(char* mes) {
else { else {
cl+=15; cl+=15;
} }
return str_to_int(cl); return str_to_int(1, cl);
} }
/* returns 1 if the rr_line contains the lr parameter /* returns 1 if the rr_line contains the lr parameter
@ -417,7 +417,7 @@ int cseq(char *message)
cseq=STRCASESTR(message, CSEQ_STR); cseq=STRCASESTR(message, CSEQ_STR);
if (cseq) { if (cseq) {
cseq+=6; cseq+=6;
num=str_to_int(cseq); num=str_to_int(1, cseq);
if (num < 1) { if (num < 1) {
if (verbose > 2) if (verbose > 2)
printf("CSeq found but not convertable\n"); printf("CSeq found but not convertable\n");
@ -480,13 +480,13 @@ void parse_uri(char *uri, char **scheme, char **user, char **host, int *port)
*host = ++at; *host = ++at;
if ((col2=strchr(*host,':'))!=NULL) { if ((col2=strchr(*host,':'))!=NULL) {
*col2 = '\0'; *col2 = '\0';
*port = str_to_int(++col2); *port = str_to_int(1, ++col2);
} }
} }
else { else {
*user = uri; *user = uri;
*host = ++at; *host = ++at;
*port = str_to_int(++col); *port = str_to_int(1, ++col);
} }
} }
else { else {
@ -496,12 +496,12 @@ void parse_uri(char *uri, char **scheme, char **user, char **host, int *port)
*col2 = '\0'; *col2 = '\0';
*scheme = uri; *scheme = uri;
*host = col; *host = col;
*port = str_to_int(++col2); *port = str_to_int(1, ++col2);
} }
else { else {
if (is_number(col)) { if (is_number(col)) {
*host = uri; *host = uri;
*port = str_to_int(col); *port = str_to_int(1, col);
} }
else { else {
*scheme = uri; *scheme = uri;

@ -18,6 +18,9 @@
*/ */
#include "sipsak.h" #include "sipsak.h"
#ifdef HAVE_STDARG_H
# include <stdarg.h>
#endif
#ifdef HAVE_NETDB_H #ifdef HAVE_NETDB_H
# include <netdb.h> # include <netdb.h>
#endif #endif
@ -144,9 +147,7 @@ static const unsigned char *parse_rr(const unsigned char *aptr, const unsigned c
int status, type, dnsclass, dlen; int status, type, dnsclass, dlen;
struct in_addr addr; struct in_addr addr;
#ifdef DEBUG dbg("ca_tmpname: %s\n", ca_tmpname);
printf("ca_tmpname: %s\n", ca_tmpname);
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len); status = ares_expand_name(aptr, abuf, alen, &name, &len);
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
printf("error: failed to expand query name\n"); printf("error: failed to expand query name\n");
@ -182,17 +183,13 @@ static const unsigned char *parse_rr(const unsigned char *aptr, const unsigned c
if (type == CARES_TYPE_SRV) { if (type == CARES_TYPE_SRV) {
free(name); free(name);
caport = DNS__16BIT(aptr + 4); caport = DNS__16BIT(aptr + 4);
#ifdef DEBUG dbg("caport: %i\n", caport);
printf("caport: %i\n", caport);
#endif
status = ares_expand_name(aptr + 6, abuf, alen, &name, &len); status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
printf("error: failed to expand SRV name\n"); printf("error: failed to expand SRV name\n");
return NULL; return NULL;
} }
#ifdef DEBUG dbg("SRV name: %s\n", name);
printf("SRV name: %s\n", name);
#endif
if (is_ip(name)) { if (is_ip(name)) {
caadr = inet_addr(name); caadr = inet_addr(name);
free(name); free(name);
@ -227,9 +224,7 @@ static const unsigned char *skip_rr(const unsigned char *aptr, const unsigned ch
long len; long len;
char *name; char *name;
#ifdef DEBUG dbg("skipping rr section...\n");
printf("skipping rr section...\n");
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len); status = ares_expand_name(aptr, abuf, alen, &name, &len);
aptr += len; aptr += len;
dlen = DNS_RR_LEN(aptr); dlen = DNS_RR_LEN(aptr);
@ -244,9 +239,7 @@ static const unsigned char *skip_query(const unsigned char *aptr, const unsigned
long len; long len;
char *name; char *name;
#ifdef DEBUG dbg("skipping query section...\n");
printf("skipping query section...\n");
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len); status = ares_expand_name(aptr, abuf, alen, &name, &len);
aptr += len; aptr += len;
aptr += NS_QFIXEDSZ; aptr += NS_QFIXEDSZ;
@ -259,9 +252,7 @@ static void cares_callback(void *arg, int status, unsigned char *abuf, int alen)
unsigned int ancount, nscount, arcount; unsigned int ancount, nscount, arcount;
const unsigned char *aptr; const unsigned char *aptr;
#ifdef DEBUG dbg("cares_callback: status=%i, alen=%i\n", status, alen);
printf("cares_callback: status=%i, alen=%i\n", status, alen);
#endif
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
if (verbose > 1) if (verbose > 1)
printf("ares failed: %s\n", ares_strerror(status)); printf("ares failed: %s\n", ares_strerror(status));
@ -272,9 +263,7 @@ static void cares_callback(void *arg, int status, unsigned char *abuf, int alen)
nscount = DNS_HEADER_NSCOUNT(abuf); nscount = DNS_HEADER_NSCOUNT(abuf);
arcount = DNS_HEADER_ARCOUNT(abuf); arcount = DNS_HEADER_ARCOUNT(abuf);
#ifdef DEBUG dbg("ancount: %i, nscount: %i, arcount: %i\n", ancount, nscount, arcount);
printf("ancount: %i, nscount: %i, arcount: %i\n", ancount, nscount, arcount);
#endif
/* safety check */ /* safety check */
if (alen < NS_HFIXEDSZ) if (alen < NS_HFIXEDSZ)
@ -308,9 +297,7 @@ inline unsigned long srv_ares(char *host, int *port, char *srv) {
caport = 0; caport = 0;
caadr = 0; caadr = 0;
ca_tmpname = NULL; ca_tmpname = NULL;
#ifdef DEBUG dbg("starting ARES query\n");
printf("!!! ARES query !!!\n");
#endif
srvh_len = strlen(host) + strlen(srv) + 2; srvh_len = strlen(host) + strlen(srv) + 2;
srvh = malloc(srvh_len); srvh = malloc(srvh_len);
@ -322,14 +309,10 @@ inline unsigned long srv_ares(char *host, int *port, char *srv) {
strncpy(srvh, srv, strlen(srv)); strncpy(srvh, srv, strlen(srv));
memcpy(srvh + strlen(srv), ".", 1); memcpy(srvh + strlen(srv), ".", 1);
strcpy(srvh + strlen(srv) + 1, host); strcpy(srvh + strlen(srv) + 1, host);
#ifdef DEBUG dbg("hostname: '%s', len: %i\n", srvh, srvh_len);
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); ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL);
#ifdef DEBUG dbg("ares_query finished, waiting for result...\n");
printf("after ares_query\n");
#endif
/* wait for query to complete */ /* wait for query to complete */
while (1) { while (1) {
FD_ZERO(&read_fds); FD_ZERO(&read_fds);
@ -345,9 +328,7 @@ inline unsigned long srv_ares(char *host, int *port, char *srv) {
} }
ares_process(channel, &read_fds, &write_fds); ares_process(channel, &read_fds, &write_fds);
} }
#ifdef DEBUG dbg("ARES answer processed\n");
printf("end of while\n");
#endif
*port = caport; *port = caport;
if (caadr == 0 && ca_tmpname != NULL) { if (caadr == 0 && ca_tmpname != NULL) {
caadr = getaddress(ca_tmpname); caadr = getaddress(ca_tmpname);
@ -605,18 +586,14 @@ void replace_strings(char *mes, char *strings) {
char sep; char sep;
pos=atr=val=repl = NULL; pos=atr=val=repl = NULL;
#ifdef DEBUG dbg("replace_strings entered\nstrings: '%s'\n", strings);
printf("replace_strings entered\nstrings: '%s'\n", strings);
#endif
if ((isalnum(*strings) != 0) && if ((isalnum(*strings) != 0) &&
(isalnum(*(strings + strlen(strings) - 1)) != 0)) { (isalnum(*(strings + strlen(strings) - 1)) != 0)) {
replace_string(req, "$replace$", replace_str); replace_string(req, "$replace$", replace_str);
} }
else { else {
sep = *strings; sep = *strings;
#ifdef DEBUG dbg("sep: '%c'\n", sep);
printf("sep: '%c'\n", sep);
#endif
end = strings + strlen(strings); end = strings + strlen(strings);
pos = strings + 1; pos = strings + 1;
while (pos < end) { while (pos < end) {
@ -631,9 +608,7 @@ void replace_strings(char *mes, char *strings) {
pos++; pos++;
} }
} }
#ifdef DEBUG dbg("atr: '%s'\nval: '%s'\n", atr, val);
printf("atr: '%s'\nval: '%s'\n", atr, val);
#endif
if ((atr != NULL) && (val != NULL)) { if ((atr != NULL) && (val != NULL)) {
repl = str_alloc(strlen(val) + 3); repl = str_alloc(strlen(val) + 3);
if (repl == NULL) { if (repl == NULL) {
@ -644,14 +619,10 @@ void replace_strings(char *mes, char *strings) {
replace_string(mes, repl, val); replace_string(mes, repl, val);
free(repl); free(repl);
} }
#ifdef DEBUG dbg("pos: '%s'\n", pos);
printf("pos: '%s'\n", pos);
#endif
} }
} }
#ifdef DEBUG dbg("mes:\n'%s'\n", mes);
printf("mes:\n'%s'\n", mes);
#endif
} }
/* insert \r in front of all \n if it is not present allready /* insert \r in front of all \n if it is not present allready
@ -690,8 +661,7 @@ void swap_buffers(char *fst, char *snd) {
free(tmp); free(tmp);
} }
void swap_ptr(char **fst, char **snd) void swap_ptr(char **fst, char **snd) {
{
char *tmp; char *tmp;
tmp = *fst; tmp = *fst;
@ -700,8 +670,7 @@ void swap_ptr(char **fst, char **snd)
} }
/* trashes one character in buff randomly */ /* trashes one character in buff randomly */
void trash_random(char *message) void trash_random(char *message) {
{
int r; int r;
float t; float t;
char *position; char *position;
@ -718,8 +687,7 @@ void trash_random(char *message)
/* this function is taken from traceroute-1.4_p12 /* this function is taken from traceroute-1.4_p12
which is distributed under the GPL and it returns which is distributed under the GPL and it returns
the difference between to timeval structs */ the difference between to timeval structs */
double deltaT(struct timeval *t1p, struct timeval *t2p) double deltaT(struct timeval *t1p, struct timeval *t2p) {
{
register double dt; register double dt;
dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 + dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
@ -728,8 +696,7 @@ double deltaT(struct timeval *t1p, struct timeval *t2p)
} }
/* returns one if the string contains only numbers otherwise zero */ /* returns one if the string contains only numbers otherwise zero */
int is_number(char *number) int is_number(char *number) {
{
int digit = 1; int digit = 1;
if (strlen(number) == 0) { if (strlen(number) == 0) {
return 0; return 0;
@ -743,8 +710,7 @@ int is_number(char *number)
/* tries to convert the given string into an integer. it strips /* tries to convert the given string into an integer. it strips
* white-spaces and exits if an error happens */ * white-spaces and exits if an error happens */
int str_to_int(char *num) int str_to_int(int mode, char *num) {
{
int ret, len; int ret, len;
char *end, *start; char *end, *start;
char *backup = NULL; char *backup = NULL;
@ -755,6 +721,7 @@ int str_to_int(char *num)
ret = 2; ret = 2;
goto error; goto error;
} }
/* we need to make a backup to insert the zero char */
backup = malloc(len + 1); backup = malloc(len + 1);
if (!backup) { if (!backup) {
fprintf(stderr, "error: failed to allocate memory\n"); fprintf(stderr, "error: failed to allocate memory\n");
@ -773,6 +740,7 @@ int str_to_int(char *num)
ret = 2; ret = 2;
goto error; goto error;
} }
if (mode == 0) {
end--; end--;
while (isspace(*end) && (end > start)) { while (isspace(*end) && (end > start)) {
end--; end--;
@ -781,6 +749,15 @@ int str_to_int(char *num)
end++; end++;
*end = '\0'; *end = '\0';
} }
}
else {
end = start;
end++;
while ((end < backup + len) && *end != '\0' && !isspace(*end)) {
end++;
}
*end = '\0';
}
if (!is_number(start)) { if (!is_number(start)) {
fprintf(stderr, "error: string is not a number: '%s'\n", start); fprintf(stderr, "error: string is not a number: '%s'\n", start);
ret = 2; ret = 2;
@ -799,16 +776,18 @@ error:
if (backup) { if (backup) {
free(backup); free(backup);
} }
if (mode == 0) {
/* libcheck expects a return value not an exit code */
#ifndef RUNNING_CHECK #ifndef RUNNING_CHECK
exit_code(ret); exit_code(ret);
#endif #endif
}
return (ret * - 1); return (ret * - 1);
} }
/* reads into the given buffer from standard input until the EOF /* reads into the given buffer from standard input until the EOF
* character, LF character or the given size of the buffer is exceeded */ * character, LF character or the given size of the buffer is exceeded */
int read_stdin(char *buf, int size, int ret) int read_stdin(char *buf, int size, int ret) {
{
int i, j; int i, j;
for(i = 0; i < size - 1; i++) { for(i = 0; i < size - 1; i++) {
@ -830,8 +809,7 @@ int read_stdin(char *buf, int size, int ret)
/* tries to allocate the given size of memory and sets it all to zero. /* tries to allocate the given size of memory and sets it all to zero.
* if the allocation fails it exits */ * if the allocation fails it exits */
void *str_alloc(size_t size) void *str_alloc(size_t size) {
{
char *ptr; char *ptr;
#ifdef HAVE_CALLOC #ifdef HAVE_CALLOC
ptr = calloc(1, size); ptr = calloc(1, size);
@ -847,3 +825,15 @@ void *str_alloc(size_t size)
#endif #endif
return ptr; return ptr;
} }
void dbg(char* format, ...) {
#ifdef DEBUG
va_list ap;
fprintf(stderr, "DEBUG: ");
va_start(ap, format);
vfprintf(stderr, format, ap);
fflush(stderr);
va_end(ap);
#endif
}

@ -84,9 +84,11 @@ double deltaT(struct timeval *t1p, struct timeval *t2p);
int is_number(char *number); int is_number(char *number);
int str_to_int(char *num); int str_to_int(int mode, char *num);
int read_stdin(char *buf, int size, int ret); int read_stdin(char *buf, int size, int ret);
void *str_alloc(size_t size); void *str_alloc(size_t size);
void dbg(char* format, ...);
#endif #endif

@ -357,10 +357,10 @@ int main(int argc, char *argv[])
} }
break; break;
case 'A': case 'A':
timing=str_to_int(optarg); timing=str_to_int(0, optarg);
break; break;
case 'b': case 'b':
namebeg=str_to_int(optarg); namebeg=str_to_int(0, optarg);
break; break;
case 'B': case 'B':
mes_body=str_alloc(strlen(optarg) + 1); mes_body=str_alloc(strlen(optarg) + 1);
@ -425,10 +425,10 @@ int main(int argc, char *argv[])
redirects=0; redirects=0;
break; break;
case 'D': case 'D':
inv_final = str_to_int(optarg) * SIP_T1; inv_final = str_to_int(0, optarg) * SIP_T1;
break; break;
case 'e': case 'e':
nameend=str_to_int(optarg); nameend=str_to_int(0, optarg);
break; break;
case 'E': case 'E':
if (strlen(optarg) == 3 && if (strlen(optarg) == 3 &&
@ -522,13 +522,13 @@ int main(int argc, char *argv[])
break; break;
#endif #endif
case 'l': case 'l':
lport=str_to_int(optarg); lport=str_to_int(0, optarg);
break; break;
case 'L': case 'L':
fix_crlf=0; fix_crlf=0;
break; break;
case 'm': case 'm':
maxforw=str_to_int(optarg); maxforw=str_to_int(0, optarg);
break; break;
case 'M': case 'M':
message=1; message=1;
@ -545,7 +545,7 @@ int main(int argc, char *argv[])
sleep_ms = -2; sleep_ms = -2;
} }
else { else {
sleep_ms = str_to_int(optarg); sleep_ms = str_to_int(0, optarg);
} }
break; break;
case 'O': case 'O':
@ -586,7 +586,7 @@ int main(int argc, char *argv[])
outbound_proxy=1; outbound_proxy=1;
break; break;
case 'P': case 'P':
processes=str_to_int(optarg); processes=str_to_int(0, optarg);
break; break;
case 'q': case 'q':
if (re) { if (re) {
@ -607,7 +607,7 @@ int main(int argc, char *argv[])
break; break;
case 'r': case 'r':
port = 0; port = 0;
port=str_to_int(optarg); port=str_to_int(0, optarg);
if (rport) { if (rport) {
fprintf(stderr, "warning: you are overwritting the destination port with the r argument\n"); fprintf(stderr, "warning: you are overwritting the destination port with the r argument\n");
} }
@ -676,7 +676,7 @@ int main(int argc, char *argv[])
symmetric=1; symmetric=1;
break; break;
case 't': case 't':
trashchar=str_to_int(optarg); trashchar=str_to_int(0, optarg);
break; break;
case 'T': case 'T':
trace=1; trace=1;
@ -736,6 +736,9 @@ int main(int argc, char *argv[])
#endif #endif
#ifdef HAVE_STRNCASECMP #ifdef HAVE_STRNCASECMP
printf(", CMP_CASE_INSENSITIVE"); printf(", CMP_CASE_INSENSITIVE");
#endif
#ifdef DEBUG
printf(", DEBUG");
#endif #endif
printf("\n"); printf("\n");
exit_code(0); exit_code(0);
@ -744,10 +747,10 @@ int main(int argc, char *argv[])
warning_ext=1; warning_ext=1;
break; break;
case 'W': case 'W':
nagios_warn = str_to_int(optarg); nagios_warn = str_to_int(0, optarg);
break; break;
case 'x': case 'x':
expires_t=str_to_int(optarg); expires_t=str_to_int(0, optarg);
break; break;
#ifdef HAVE_GETOPT_LONG #ifdef HAVE_GETOPT_LONG
case 'X': case 'X':
@ -755,7 +758,7 @@ int main(int argc, char *argv[])
break; break;
#endif #endif
case 'z': case 'z':
rand_rem=str_to_int(optarg); rand_rem=str_to_int(0, optarg);
if (rand_rem < 0 || rand_rem > 100) { if (rand_rem < 0 || rand_rem > 100) {
fprintf(stderr, "error: z option must between 0 and 100\n"); fprintf(stderr, "error: z option must between 0 and 100\n");
exit_code(2); exit_code(2);

@ -9,104 +9,99 @@
#define RUNNING_CHECK 1 #define RUNNING_CHECK 1
START_TEST (test_is_number) { START_TEST (test_is_number) {
fail_unless(is_number("") == 0, /* failure cases */
"is_number(\"\") returned %d, instead of 0", is_number("")); fail_unless(is_number("") == 0, "is_number(\"\") returned %d, instead of 0", is_number(""));
fail_unless(is_number("a") == 0, fail_unless(is_number("a") == 0, "is_number(\"a\") returned %d, instead of 0", is_number("a"));
"is_number(\"a\") returned %d, instead of 0", is_number("a")); fail_unless(is_number("XYZ") == 0, "is_number(\"XYZ\") returned %d, instead of 0", is_number("XYZ"));
fail_unless(is_number("XYZ") == 0, fail_unless(is_number("10i") == 0, "is_number(\"10i\") returned %d, instead of 0", is_number("10i"));
"is_number(\"XYZ\") returned %d, instead of 0", is_number("XYZ")); fail_unless(is_number("p01") == 0, "is_number(\"p01\") returned %d, instead of 0", is_number("p01"));
fail_unless(is_number("10i") == 0, fail_unless(is_number("p2q") == 0, "is_number(\"p2q\") returned %d, instead of 0", is_number("p2q"));
"is_number(\"10i\") returned %d, instead of 0", is_number("10i")); fail_unless(is_number("1b3") == 0, "is_number(\"1b3\") returned %d, instead of 0", is_number("1b3"));
fail_unless(is_number("p01") == 0,
"is_number(\"p01\") returned %d, instead of 0", is_number("p01")); /* sucess cases */
fail_unless(is_number("p2q") == 0, fail_unless(is_number("1") == 1, "is_number(\"1\") returned %d, instead of 1", is_number("1"));
"is_number(\"p2q\") returned %d, instead of 0", is_number("p2q")); fail_unless(is_number("10") == 1, "is_number(\"10\") returned %d, instead of 1", is_number("10"));
fail_unless(is_number("1b3") == 0,
"is_number(\"1b3\") returned %d, instead of 0", is_number("1b3"));
fail_unless(is_number("1") == 1,
"is_number(\"1\") returned %d, instead of 1", is_number("1"));
fail_unless(is_number("10") == 1,
"is_number(\"10\") returned %d, instead of 1", is_number("10"));
} }
END_TEST END_TEST
START_TEST (test_str_to_int) { START_TEST (test_str_to_int) {
fail_unless(str_to_int("") == -2, /* failure because empty */
"str_to_int(\"\") returned %d, instead of -2", str_to_int("")); fail_unless(str_to_int(0, "") == -2, "str_to_int(0, \"\") returned %d, instead of -2", str_to_int(0, ""));
fail_unless(str_to_int(" ") == -2, fail_unless(str_to_int(1, "") == -2, "str_to_int(1, \"\") returned %d, instead of -2", str_to_int(1, ""));
"str_to_int(\" \") returned %d, instead of -2", str_to_int(" ")); fail_unless(str_to_int(0, " ") == -2, "str_to_int(0, \" \") returned %d, instead of -2", str_to_int(0, " "));
fail_unless(str_to_int(" ") == -2, fail_unless(str_to_int(1, " ") == -2, "str_to_int(1, \" \") returned %d, instead of -2", str_to_int(1, " "));
"str_to_int(\"\\t\") returned %d, instead of -2", str_to_int(" ")); fail_unless(str_to_int(0, " ") == -2, "str_to_int(0, \"\\t\") returned %d, instead of -2", str_to_int(0, " "));
fail_unless(str_to_int(" ") == -2, fail_unless(str_to_int(1, " ") == -2, "str_to_int(1, \"\\t\") returned %d, instead of -2", str_to_int(1, " "));
"str_to_int(\" \\t\") returned %d, instead of -2", str_to_int(" ")); fail_unless(str_to_int(0, " ") == -2, "str_to_int(0, \" \\t\") returned %d, instead of -2", str_to_int(0, " "));
fail_unless(str_to_int("a") == -2, fail_unless(str_to_int(1, " ") == -2, "str_to_int(1, \" \\t\") returned %d, instead of -2", str_to_int(1, " "));
"str_to_int(\"a\") returned %d, instead of -2", str_to_int("a"));
fail_unless(str_to_int(" a") == -2, /* failure because non-int */
"str_to_int(\" a\") returned %d, instead of -2", str_to_int(" a")); fail_unless(str_to_int(0, "a") == -2, "str_to_int(0, \"a\") returned %d, instead of -2", str_to_int(0, "a"));
fail_unless(str_to_int(" a ") == -2, fail_unless(str_to_int(1, "a") == -2, "str_to_int(1, \"a\") returned %d, instead of -2", str_to_int(1, "a"));
"str_to_int(\" a \") returned %d, instead of -2", str_to_int(" a ")); fail_unless(str_to_int(0, " a") == -2, "str_to_int(0, \" a\") returned %d, instead of -2", str_to_int(0, " a"));
fail_unless(str_to_int("ABC") == -2, fail_unless(str_to_int(1, " a") == -2, "str_to_int(1, \" a\") returned %d, instead of -2", str_to_int(1, " a"));
"str_to_int(\"ABC\") returned %d, instead of -2", str_to_int("ABC")); fail_unless(str_to_int(0, " a ") == -2, "str_to_int(0, \" a \") returned %d, instead of -2", str_to_int(0, " a "));
fail_unless(str_to_int(" ABC") == -2, fail_unless(str_to_int(1, " a ") == -2, "str_to_int(1, \" a \") returned %d, instead of -2", str_to_int(1, " a "));
"str_to_int(\" ABC\") returned %d, instead of -2", str_to_int(" ABC")); fail_unless(str_to_int(0, "ABC") == -2, "str_to_int(0, \"ABC\") returned %d, instead of -2", str_to_int(0, "ABC"));
fail_unless(str_to_int(" ABC ") == -2, fail_unless(str_to_int(1, "ABC") == -2, "str_to_int(1, \"ABC\") returned %d, instead of -2", str_to_int(1, "ABC"));
"str_to_int(\" ABC\\t\") returned %d, instead of -2", str_to_int(" ABC ")); fail_unless(str_to_int(0, " ABC") == -2, "str_to_int(0, \" ABC\") returned %d, instead of -2", str_to_int(0, " ABC"));
fail_unless(str_to_int("1") == 1, fail_unless(str_to_int(1, " ABC") == -2, "str_to_int(1, \" ABC\") returned %d, instead of -2", str_to_int(1, " ABC"));
"str_to_int(\"1\") returned %d, instead of 1", str_to_int("1")); fail_unless(str_to_int(0, " ABC ") == -2, "str_to_int(0, \" ABC\\t\") returned %d, instead of -2", str_to_int(0, " ABC "));
fail_unless(str_to_int("10") == 10, fail_unless(str_to_int(1, " ABC ") == -2, "str_to_int(1, \" ABC\\t\") returned %d, instead of -2", str_to_int(1, " ABC "));
"str_to_int(\"10\") returned %d, instead of 10", str_to_int("10"));
fail_unless(str_to_int(" 10") == 10, /* success cases */
"str_to_int(\" 10\") returned %d, instead of 10", str_to_int(" 10")); fail_unless(str_to_int(0, "1") == 1, "str_to_int(0, \"1\") returned %d, instead of 1", str_to_int(0, "1"));
fail_unless(str_to_int(" 10 ") == 10, fail_unless(str_to_int(1, "1") == 1, "str_to_int(1, \"1\") returned %d, instead of 1", str_to_int(1, "1"));
"str_to_int(\" 10 \") returned %d, instead of 10", str_to_int(" 10 ")); fail_unless(str_to_int(0, "10") == 10, "str_to_int(0, \"10\") returned %d, instead of 10", str_to_int(0, "10"));
fail_unless(str_to_int(" 10 ") == 10, fail_unless(str_to_int(1, "10") == 10, "str_to_int(1, \"10\") returned %d, instead of 10", str_to_int(1, "10"));
"str_to_int(\"\\t 10 \\t\") returned %d, instead of 10", str_to_int(" 10 ")); fail_unless(str_to_int(0, " 10") == 10, "str_to_int(0, \" 10\") returned %d, instead of 10", str_to_int(0, " 10"));
fail_unless(str_to_int(1, " 10") == 10, "str_to_int(1, \" 10\") returned %d, instead of 10", str_to_int(1, " 10"));
fail_unless(str_to_int(0, " 10 ") == 10, "str_to_int(0, \" 10 \") returned %d, instead of 10", str_to_int(0, " 10 "));
fail_unless(str_to_int(1, " 10 ") == 10, "str_to_int(1, \" 10 \") returned %d, instead of 10", str_to_int(1, " 10 "));
fail_unless(str_to_int(0, " 10 ") == 10, "str_to_int(0, \"\\t 10 \\t\") returned %d, instead of 10", str_to_int(0, " 10 "));
fail_unless(str_to_int(1, " 10 ") == 10, "str_to_int(1, \"\\t 10 \\t\") returned %d, instead of 10", str_to_int(1, " 10 "));
/* success and failures depending on the mode */
fail_unless(str_to_int(0, "1 a") == -2, "str_to_int(0, \"1 a\") returned %d, instead of -2", str_to_int(0, "1 a"));
fail_unless(str_to_int(1, "1 a") == 1, "str_to_int(1, \"1 a\") returned %d, instead of 1", str_to_int(1, "1"));
fail_unless(str_to_int(0, "10 B") == -2, "str_to_int(0, \"10\\tB\") returned %d, instead of -2", str_to_int(0, "10 B"));
fail_unless(str_to_int(1, "10 B") == 10, "str_to_int(1, \"10\\tB\") returned %d, instead of 10", str_to_int(1, "10 B"));
fail_unless(str_to_int(0, " 100 ABC ") == -2, "str_to_int(0, \" 100\\tABC \") returned %d, instead of -2", str_to_int(0, " 100 ABC "));
fail_unless(str_to_int(1, " 100 ABC ") == 100, "str_to_int(1, \" 100\\tABC \") returned %d, instead of 100", str_to_int(1, " 100 ABC "));
} }
END_TEST END_TEST
START_TEST (test_is_ip) { START_TEST (test_is_ip) {
fail_unless(is_ip("") == 0, /* failure cases */
"is_ip(\"\") returned %d, instead of 0", is_ip("")); fail_unless(is_ip("") == 0, "is_ip(\"\") returned %d, instead of 0", is_ip(""));
fail_unless(is_ip("0") == 0, fail_unless(is_ip("0") == 0, "is_ip(\"0\") returned %d, instead of 0", is_ip("0"));
"is_ip(\"0\") returned %d, instead of 0", is_ip("0")); fail_unless(is_ip("100") == 0, "is_ip(\"100\") returned %d, instead of 0", is_ip("100"));
fail_unless(is_ip("100") == 0, fail_unless(is_ip("1000") == 0, "is_ip(\"1000\") returned %d, instead of 0", is_ip("1000"));
"is_ip(\"100\") returned %d, instead of 0", is_ip("100")); fail_unless(is_ip("1.0") == 0, "is_ip(\"1.0\") returned %d, instead of 0", is_ip("1.0"));
fail_unless(is_ip("1000") == 0, fail_unless(is_ip("1.2.0") == 0, "is_ip(\"1.2.0\") returned %d, instead of 0", is_ip("1.2.0"));
"is_ip(\"1000\") returned %d, instead of 0", is_ip("1000")); fail_unless(is_ip("1.2.3.4.5") == 0, "is_ip(\"1.2.3.4.5\") returned %d, instead of 0", is_ip("1.2.3.4.5"));
fail_unless(is_ip("1.0") == 0, fail_unless(is_ip("1000.0.0.0") == 0, "is_ip(\"1000.0.0.0\") returned %d, instead of 0", is_ip("1000.0.0.0"));
"is_ip(\"1.0\") returned %d, instead of 0", is_ip("1.0")); fail_unless(is_ip("0.1000.0.0") == 0, "is_ip(\"0.1000.0.0\") returned %d, instead of 0", is_ip("0.1000.0.0"));
fail_unless(is_ip("1.2.0") == 0, fail_unless(is_ip("0.0.1000.0") == 0, "is_ip(\"0.0.1000.0\") returned %d, instead of 0", is_ip("0.0.1000.0"));
"is_ip(\"1.2.0\") returned %d, instead of 0", is_ip("1.2.0")); fail_unless(is_ip("0.0.0.1000") == 0, "is_ip(\"0.0.0.1000\") returned %d, instead of 0", is_ip("0.0.0.1000"));
fail_unless(is_ip("1.2.3.4.5") == 0,
"is_ip(\"1.2.3.4.5\") returned %d, instead of 0", is_ip("1.2.3.4.5")); /* success cases */
fail_unless(is_ip("1000.0.0.0") == 0, fail_unless(is_ip("0.0.0.0") == 1, "is_ip(\"0.0.0.0\") returned %d, instead of 1", is_ip("0.0.0.0"));
"is_ip(\"1000.0.0.0\") returned %d, instead of 0", is_ip("1000.0.0.0")); fail_unless(is_ip("1.2.3.4") == 1, "is_ip(\"1.2.3.4\") returned %d, instead of 1", is_ip("1.2.3.4"));
fail_unless(is_ip("0.1000.0.0") == 0, fail_unless(is_ip("192.168.1.1") == 1, "is_ip(\"192.168.1.1\") returned %d, instead of 1", is_ip("192.168.1.1"));
"is_ip(\"0.1000.0.0\") returned %d, instead of 0", is_ip("0.1000.0.0")); /* this is a known limitation ;) */
fail_unless(is_ip("0.0.1000.0") == 0, fail_unless(is_ip("999.999.999.999") == 1, "is_ip(\"999.999.999.999\") returned %d, instead of 1", is_ip("999.999.999.999"));
"is_ip(\"0.0.1000.0\") returned %d, instead of 0", is_ip("0.0.1000.0"));
fail_unless(is_ip("0.0.0.1000") == 0,
"is_ip(\"0.0.0.1000\") returned %d, instead of 0", is_ip("0.0.0.1000"));
fail_unless(is_ip("0.0.0.0") == 1,
"is_ip(\"0.0.0.0\") returned %d, instead of 1", is_ip("0.0.0.0"));
fail_unless(is_ip("1.2.3.4") == 1,
"is_ip(\"1.2.3.4\") returned %d, instead of 1", is_ip("1.2.3.4"));
fail_unless(is_ip("192.168.1.1") == 1,
"is_ip(\"192.168.1.1\") returned %d, instead of 1", is_ip("192.168.1.1"));
// this is a "known bug" ;)
fail_unless(is_ip("999.999.999.999") == 1,
"is_ip(\"999.999.999.999\") returned %d, instead of 1", is_ip("999.999.999.999"));
} }
END_TEST END_TEST
START_TEST (test_getaddress) { START_TEST (test_getaddress) {
fail_unless(getaddress("") == 0, /* failure case */
"getaddress(\"\") returned %lu, instead of 0", getaddress("")); fail_unless(getaddress("") == 0, "getaddress(\"\") returned %lu, instead of 0", getaddress(""));
fail_unless(getaddress("127.0.0.1") == 16777343,
"getaddress(\"127.0.0.1\") returned %lu, instead of 16777343", getaddress("127.0.0.1")); /* success cases */
// this should work also without DNS fail_unless(getaddress("127.0.0.1") == 16777343, "getaddress(\"127.0.0.1\") returned %lu, instead of 16777343", getaddress("127.0.0.1"));
fail_unless(getaddress("localhost") == 16777343, /* this should work also without DNS */
"getaddress(\"localhost\") returned %lu, instead of 16777343", getaddress("localhost")); fail_unless(getaddress("localhost") == 16777343, "getaddress(\"localhost\") returned %lu, instead of 16777343", getaddress("localhost"));
} }
END_TEST END_TEST
@ -115,23 +110,22 @@ START_TEST (test_insert_cr) {
memset(ta, '\0', 15); memset(ta, '\0', 15);
insert_cr(ta); insert_cr(ta);
fail_unless(memcmp(ta, "\r\n\0\0\0\0\0\0\0\0\0\0\0\0\0", 15) == 0, fail_unless(memcmp(ta, "\r\n\0\0\0\0\0\0\0\0\0\0\0\0\0", 15) == 0, "insert_cr(\"\") returned '%s', instead of \"\r\n\"", ta);
"insert_cr(\"\") returned '%s', instead of \"\r\n\"", ta);
memset(ta, '\0', 15); memset(ta, '\0', 15);
memcpy(ta, "test", 4); memcpy(ta, "test", 4);
insert_cr(ta); insert_cr(ta);
fail_unless(memcmp(ta, "test\r\n\0\0\0\0\0\0\0\0\0", 15) == 0, fail_unless(memcmp(ta, "test\r\n\0\0\0\0\0\0\0\0\0", 15) == 0, "insert_cr(\"test\") returned '%s', instead of \"test\r\n\"", ta);
"insert_cr(\"test\") returned '%s', instead of \"test\r\n\"", ta);
memset(ta, '\0', 15); memset(ta, '\0', 15);
memcpy(ta, "test\n", 5); memcpy(ta, "test\n", 5);
insert_cr(ta); insert_cr(ta);
fail_unless(memcmp(ta, "test\r\n\r\n\0\0\0\0\0\0\0", 15) == 0, fail_unless(memcmp(ta, "test\r\n\r\n\0\0\0\0\0\0\0", 15) == 0, "insert_cr(\"test\\n\") returned '%s', instead of \"test\\r\\n\"", ta);
"insert_cr(\"test\\n\") returned '%s', instead of \"test\\r\\n\"", ta);
memset(ta, '\0', 15); memset(ta, '\0', 15);
memcpy(ta, "foo\nbar\n", 8); memcpy(ta, "foo\nbar\n", 8);
insert_cr(ta); insert_cr(ta);
fail_unless(memcmp(ta, "foo\r\nbar\r\n\r\n\0\0\0", 15) == 0, fail_unless(memcmp(ta, "foo\r\nbar\r\n\r\n\0\0\0", 15) == 0, "insert_cr(\"foo\\nbar\\n\") returned '%s', instead of \"foo\\r\\nbar\\r\\n\"", ta);
"insert_cr(\"foo\\nbar\\n\") returned '%s', instead of \"foo\\r\\nbar\\r\\n\"", ta);
} }
END_TEST END_TEST

@ -623,9 +623,7 @@ void create_sockets(struct sipsak_con_data *cd) {
} }
# endif /* USE_OPENSSL */ # endif /* USE_OPENSSL */
#endif /* USE_GNUTLS */ #endif /* USE_GNUTLS */
#ifdef DEBUG dbg("initialized tls socket %i\n", cd->csock);
printf("initialized tls socket %i\n", cd->csock);
#endif
} }
#endif /* WITH_TLS_TRANSP */ #endif /* WITH_TLS_TRANSP */
} }
@ -658,9 +656,7 @@ void close_sockets(struct sipsak_con_data *cd) {
#endif /* WITH_TLS_TRANSP */ #endif /* WITH_TLS_TRANSP */
shutdown(cd->csock, SHUT_RDWR); shutdown(cd->csock, SHUT_RDWR);
} }
#ifdef DEBUG dbg("sockets closed\n");
printf("sockets closed\n");
#endif
} }
void send_message(char* mes, struct sipsak_con_data *cd, void send_message(char* mes, struct sipsak_con_data *cd,
@ -674,15 +670,11 @@ void send_message(char* mes, struct sipsak_con_data *cd,
} }
/* lets fire the request to the server and store when we did */ /* lets fire the request to the server and store when we did */
if (cd->csock == -1) { if (cd->csock == -1) {
#ifdef DEBUG dbg("\nusing un-connected socket for sending\n");
printf("\nusing un-connected socket for sending\n");
#endif
ret = sendto(cd->usock, mes, strlen(mes), 0, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr)); ret = sendto(cd->usock, mes, strlen(mes), 0, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr));
} }
else { else {
#ifdef DEBUG dbg("\nusing connected socket for sending\n");
printf("\nusing connected socket for sending\n");
#endif
#ifdef WITH_TLS_TRANSP #ifdef WITH_TLS_TRANSP
if (transport == SIP_TLS_TRANSPORT) { if (transport == SIP_TLS_TRANSPORT) {
# ifdef USE_GNUTLS # ifdef USE_GNUTLS
@ -883,22 +875,16 @@ int complete_mes(char *mes, int size) {
char *tmp = NULL; char *tmp = NULL;
cl = get_cl(mes); cl = get_cl(mes);
#ifdef DEBUG dbg("CL: %i\n", cl);
printf("CL: %i\n", cl);
#endif
if (cl < 0){ if (cl < 0){
if (verbose > 0) if (verbose > 0)
printf("missing CL header; waiting for more bytes...\n"); printf("missing CL header; waiting for more bytes...\n");
return 0; return 0;
} }
tmp = get_body(mes); tmp = get_body(mes);
#ifdef DEBUG dbg("body: '%s'\n", tmp);
printf("body: '%s'\n", tmp);
#endif
headers = tmp - mes; headers = tmp - mes;
#ifdef DEBUG dbg("length: %i, headers: %i\n", size, headers);
printf("length: %i, headers: %i\n", size, headers);
#endif
len = headers + cl; len = headers + cl;
if (len == size) { if (len == size) {
if (verbose > 0) if (verbose > 0)
@ -1005,9 +991,7 @@ int recv_message(char *buf, int size, int inv_trans,
udp_hdr = (struct udphdr *) ((char *)s_ip_hdr + s_ip_len); udp_hdr = (struct udphdr *) ((char *)s_ip_hdr + s_ip_len);
srcport = ntohs(udp_hdr->uh_sport); srcport = ntohs(udp_hdr->uh_sport);
dstport = ntohs(udp_hdr->uh_dport); dstport = ntohs(udp_hdr->uh_dport);
#ifdef DEBUG dbg("\nlport: %i, rport: %i\n", lport, rport);
printf("\nlport: %i, rport: %i\n", lport, rport);
#endif
if ((srcport == lport) && (dstport == rport)) { if ((srcport == lport) && (dstport == rport)) {
printf(" (type: %u, code: %u)", icmp_hdr->icmp_type, icmp_hdr->icmp_code); printf(" (type: %u, code: %u)", icmp_hdr->icmp_type, icmp_hdr->icmp_code);
#ifdef HAVE_INET_NTOP #ifdef HAVE_INET_NTOP
@ -1133,32 +1117,26 @@ int set_target(struct sockaddr_in *adr, unsigned long target, int port, int sock
# ifdef USE_GNUTLS # ifdef USE_GNUTLS
ret = gnutls_handshake(tls_session); ret = gnutls_handshake(tls_session);
if (ret < 0) { if (ret < 0) {
#ifdef DEBUG dbg("TLS Handshake FAILED!!!\n");
printf("*** TLS Handshake FAILED!!!\n");
#endif
gnutls_perror(ret); gnutls_perror(ret);
exit_code(3); exit_code(3);
} }
#ifdef DEBUG else if (verbose > 2) {
else { dbg(" TLS Handshake was completed!\n");
printf("*** TLS Handshake was completed!\n");
#ifdef DEBUG
gnutls_session_info(tls_session); gnutls_session_info(tls_session);
#endif
verify_certificate_simple(tls_session, domainname); verify_certificate_simple(tls_session, domainname);
//verify_certificate_chain(tls_session, domainname, cert_chain, cert_chain_length); //verify_certificate_chain(tls_session, domainname, cert_chain, cert_chain_length);
} }
#endif
# else /* USE_GNUTLS */ # else /* USE_GNUTLS */
# ifdef USE_OPENSSL # ifdef USE_OPENSSL
ret = SSL_connect(ssl); ret = SSL_connect(ssl);
if (ret == 1) { if (ret == 1) {
#ifdef DEBUG dbg("TLS connect successful\n");
printf("TLS connect successful\n"); if (verbose > 2) {
printf("TLS connect: new connection using %s %s %d\n", printf("TLS connect: new connection using %s %s %d\n",
SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl), SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl),
SSL_get_cipher_bits(ssl, 0)); SSL_get_cipher_bits(ssl, 0));
#endif }
cert = SSL_get_peer_certificate(ssl); cert = SSL_get_peer_certificate(ssl);
if (cert != 0) { if (cert != 0) {
tls_dump_cert_info("TLS connect: server certificate", cert); tls_dump_cert_info("TLS connect: server certificate", cert);

Loading…
Cancel
Save