From 1bf0540dd3fdacc4bd3f277f1068799bcdffbaae Mon Sep 17 00:00:00 2001 From: nils-ohlmeier Date: Sun, 10 Dec 2006 06:03:02 +0000 Subject: [PATCH] - 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 133160868a2c7a1a7d2859a57c927d6e299b2651) --- config.h.in | 3 + configure | 21 ++++- configure.ac | 2 +- header_f.c | 14 ++-- helper.c | 130 ++++++++++++++----------------- helper.h | 4 +- sipsak.c | 29 +++---- tests/check_helper.c | 182 +++++++++++++++++++++---------------------- transport.c | 56 ++++--------- 9 files changed, 215 insertions(+), 226 deletions(-) diff --git a/config.h.in b/config.h.in index 82ca20e..88f4c2a 100644 --- a/config.h.in +++ b/config.h.in @@ -130,6 +130,9 @@ /* Define to 1 if you have the `socket' function. */ #undef HAVE_SOCKET +/* Define to 1 if you have the header file. */ +#undef HAVE_STDARG_H + /* Define to 1 if you have the header file. */ #undef HAVE_STDINT_H diff --git a/configure b/configure index c1f9fe6..7ca1a56 100755 --- a/configure +++ b/configure @@ -5088,7 +5088,26 @@ fi 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 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 diff --git a/configure.ac b/configure.ac index 3fb36f9..01810e3 100644 --- a/configure.ac +++ b/configure.ac @@ -34,7 +34,7 @@ AC_SUBST([LIBS]) # Checks for header files. 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_HEADER_SYS_WAIT AC_HEADER_TIME diff --git a/header_f.c b/header_f.c index 0234919..3d0bbc7 100644 --- a/header_f.c +++ b/header_f.c @@ -198,7 +198,7 @@ void set_maxforw(char *mes, int value){ strncpy(backup, crlfi, strlen(crlfi)); crlfi=max + MAX_FRW_STR_LEN; if (value == -1) { - maxforward = str_to_int(crlfi); + maxforward = str_to_int(1, crlfi); maxforward++; } else { @@ -286,7 +286,7 @@ int get_cl(char* mes) { else { cl+=15; } - return str_to_int(cl); + return str_to_int(1, cl); } /* returns 1 if the rr_line contains the lr parameter @@ -417,7 +417,7 @@ int cseq(char *message) cseq=STRCASESTR(message, CSEQ_STR); if (cseq) { cseq+=6; - num=str_to_int(cseq); + num=str_to_int(1, cseq); if (num < 1) { if (verbose > 2) 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; if ((col2=strchr(*host,':'))!=NULL) { *col2 = '\0'; - *port = str_to_int(++col2); + *port = str_to_int(1, ++col2); } } else { *user = uri; *host = ++at; - *port = str_to_int(++col); + *port = str_to_int(1, ++col); } } else { @@ -496,12 +496,12 @@ void parse_uri(char *uri, char **scheme, char **user, char **host, int *port) *col2 = '\0'; *scheme = uri; *host = col; - *port = str_to_int(++col2); + *port = str_to_int(1, ++col2); } else { if (is_number(col)) { *host = uri; - *port = str_to_int(col); + *port = str_to_int(1, col); } else { *scheme = uri; diff --git a/helper.c b/helper.c index 38d06c7..c91ddc2 100644 --- a/helper.c +++ b/helper.c @@ -18,6 +18,9 @@ */ #include "sipsak.h" +#ifdef HAVE_STDARG_H +# include +#endif #ifdef HAVE_NETDB_H # include #endif @@ -144,9 +147,7 @@ static const unsigned char *parse_rr(const unsigned char *aptr, const unsigned c int status, type, dnsclass, dlen; struct in_addr addr; -#ifdef DEBUG - printf("ca_tmpname: %s\n", ca_tmpname); -#endif + dbg("ca_tmpname: %s\n", ca_tmpname); status = ares_expand_name(aptr, abuf, alen, &name, &len); if (status != ARES_SUCCESS) { 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) { free(name); caport = DNS__16BIT(aptr + 4); -#ifdef DEBUG - printf("caport: %i\n", caport); -#endif + dbg("caport: %i\n", caport); status = ares_expand_name(aptr + 6, abuf, alen, &name, &len); if (status != ARES_SUCCESS) { printf("error: failed to expand SRV name\n"); return NULL; } -#ifdef DEBUG - printf("SRV name: %s\n", name); -#endif + dbg("SRV name: %s\n", name); if (is_ip(name)) { caadr = inet_addr(name); free(name); @@ -227,9 +224,7 @@ static const unsigned char *skip_rr(const unsigned char *aptr, const unsigned ch long len; char *name; -#ifdef DEBUG - printf("skipping rr section...\n"); -#endif + dbg("skipping rr section...\n"); status = ares_expand_name(aptr, abuf, alen, &name, &len); aptr += len; dlen = DNS_RR_LEN(aptr); @@ -244,9 +239,7 @@ static const unsigned char *skip_query(const unsigned char *aptr, const unsigned long len; char *name; -#ifdef DEBUG - printf("skipping query section...\n"); -#endif + dbg("skipping query section...\n"); status = ares_expand_name(aptr, abuf, alen, &name, &len); aptr += len; 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; const unsigned char *aptr; -#ifdef DEBUG - printf("cares_callback: status=%i, alen=%i\n", status, alen); -#endif + dbg("cares_callback: status=%i, alen=%i\n", status, alen); if (status != ARES_SUCCESS) { if (verbose > 1) printf("ares failed: %s\n", ares_strerror(status)); @@ -271,10 +262,8 @@ static void cares_callback(void *arg, int status, unsigned char *abuf, int alen) ancount = DNS_HEADER_ANCOUNT(abuf); nscount = DNS_HEADER_NSCOUNT(abuf); arcount = DNS_HEADER_ARCOUNT(abuf); - -#ifdef DEBUG - printf("ancount: %i, nscount: %i, arcount: %i\n", ancount, nscount, arcount); -#endif + + dbg("ancount: %i, nscount: %i, arcount: %i\n", ancount, nscount, arcount); /* safety check */ if (alen < NS_HFIXEDSZ) @@ -308,9 +297,7 @@ inline unsigned long srv_ares(char *host, int *port, char *srv) { caport = 0; caadr = 0; ca_tmpname = NULL; -#ifdef DEBUG - printf("!!! ARES query !!!\n"); -#endif + dbg("starting ARES query\n"); srvh_len = strlen(host) + strlen(srv) + 2; srvh = malloc(srvh_len); @@ -322,14 +309,10 @@ inline unsigned long srv_ares(char *host, int *port, char *srv) { 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 + dbg("hostname: '%s', len: %i\n", srvh, srvh_len); ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL); -#ifdef DEBUG - printf("after ares_query\n"); -#endif + dbg("ares_query finished, waiting for result...\n"); /* wait for query to complete */ while (1) { 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); } -#ifdef DEBUG - printf("end of while\n"); -#endif + dbg("ARES answer processed\n"); *port = caport; if (caadr == 0 && ca_tmpname != NULL) { caadr = getaddress(ca_tmpname); @@ -499,7 +480,7 @@ unsigned long getsrvadr(char *host, int *port, unsigned int *transport) { /* because the full qualified domain name is needed by many other functions it will be determined by this function. */ -void get_fqdn(){ +void get_fqdn() { char hname[100], dname[100], hlp[18]; size_t namelen=100; struct hostent* he; @@ -577,7 +558,7 @@ void get_fqdn(){ /* this function searches for search in mess and replaces it with replacement */ -void replace_string(char *mess, char *search, char *replacement){ +void replace_string(char *mess, char *search, char *replacement) { char *backup, *insert; insert=STRCASESTR(mess, search); @@ -605,18 +586,14 @@ void replace_strings(char *mes, char *strings) { char sep; pos=atr=val=repl = NULL; -#ifdef DEBUG - printf("replace_strings entered\nstrings: '%s'\n", strings); -#endif + dbg("replace_strings entered\nstrings: '%s'\n", strings); 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 + dbg("sep: '%c'\n", sep); end = strings + strlen(strings); pos = strings + 1; while (pos < end) { @@ -631,9 +608,7 @@ void replace_strings(char *mes, char *strings) { pos++; } } -#ifdef DEBUG - printf("atr: '%s'\nval: '%s'\n", atr, val); -#endif + dbg("atr: '%s'\nval: '%s'\n", atr, val); if ((atr != NULL) && (val != NULL)) { repl = str_alloc(strlen(val) + 3); if (repl == NULL) { @@ -644,19 +619,15 @@ void replace_strings(char *mes, char *strings) { replace_string(mes, repl, val); free(repl); } -#ifdef DEBUG - printf("pos: '%s'\n", pos); -#endif + dbg("pos: '%s'\n", pos); } } -#ifdef DEBUG - printf("mes:\n'%s'\n", mes); -#endif + dbg("mes:\n'%s'\n", mes); } /* insert \r in front of all \n if it is not present allready * and and a trailing \r\n is not present */ -void insert_cr(char *mes){ +void insert_cr(char *mes) { char *lf, *pos, *backup; pos = mes; @@ -690,8 +661,7 @@ void swap_buffers(char *fst, char *snd) { free(tmp); } -void swap_ptr(char **fst, char **snd) -{ +void swap_ptr(char **fst, char **snd) { char *tmp; tmp = *fst; @@ -700,8 +670,7 @@ void swap_ptr(char **fst, char **snd) } /* trashes one character in buff randomly */ -void trash_random(char *message) -{ +void trash_random(char *message) { int r; float t; char *position; @@ -718,8 +687,7 @@ void trash_random(char *message) /* this function is taken from traceroute-1.4_p12 which is distributed under the GPL and it returns 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; 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 */ -int is_number(char *number) -{ +int is_number(char *number) { int digit = 1; if (strlen(number) == 0) { return 0; @@ -743,8 +710,7 @@ int is_number(char *number) /* 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 str_to_int(int mode, char *num) { int ret, len; char *end, *start; char *backup = NULL; @@ -755,6 +721,7 @@ int str_to_int(char *num) ret = 2; goto error; } + /* we need to make a backup to insert the zero char */ backup = malloc(len + 1); if (!backup) { fprintf(stderr, "error: failed to allocate memory\n"); @@ -773,12 +740,22 @@ int str_to_int(char *num) ret = 2; goto error; } - end--; - while (isspace(*end) && (end > start)) { + if (mode == 0) { end--; + while (isspace(*end) && (end > start)) { + end--; + } + if (end != (backup + len - 1)) { + end++; + *end = '\0'; + } } - if (end != (backup + len - 1)) { + else { + end = start; end++; + while ((end < backup + len) && *end != '\0' && !isspace(*end)) { + end++; + } *end = '\0'; } if (!is_number(start)) { @@ -799,16 +776,18 @@ error: if (backup) { free(backup); } + if (mode == 0) { + /* libcheck expects a return value not an exit code */ #ifndef RUNNING_CHECK - exit_code(ret); + exit_code(ret); #endif + } return (ret * - 1); } /* reads into the given buffer from standard input until the EOF * 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; 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. * if the allocation fails it exits */ -void *str_alloc(size_t size) -{ +void *str_alloc(size_t size) { char *ptr; #ifdef HAVE_CALLOC ptr = calloc(1, size); @@ -847,3 +825,15 @@ void *str_alloc(size_t size) #endif 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 +} diff --git a/helper.h b/helper.h index ab7c178..0cc55aa 100644 --- a/helper.h +++ b/helper.h @@ -84,9 +84,11 @@ double deltaT(struct timeval *t1p, struct timeval *t2p); 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); void *str_alloc(size_t size); + +void dbg(char* format, ...); #endif diff --git a/sipsak.c b/sipsak.c index fb68435..49460c6 100644 --- a/sipsak.c +++ b/sipsak.c @@ -357,10 +357,10 @@ int main(int argc, char *argv[]) } break; case 'A': - timing=str_to_int(optarg); + timing=str_to_int(0, optarg); break; case 'b': - namebeg=str_to_int(optarg); + namebeg=str_to_int(0, optarg); break; case 'B': mes_body=str_alloc(strlen(optarg) + 1); @@ -425,10 +425,10 @@ int main(int argc, char *argv[]) redirects=0; break; case 'D': - inv_final = str_to_int(optarg) * SIP_T1; + inv_final = str_to_int(0, optarg) * SIP_T1; break; case 'e': - nameend=str_to_int(optarg); + nameend=str_to_int(0, optarg); break; case 'E': if (strlen(optarg) == 3 && @@ -522,13 +522,13 @@ int main(int argc, char *argv[]) break; #endif case 'l': - lport=str_to_int(optarg); + lport=str_to_int(0, optarg); break; case 'L': fix_crlf=0; break; case 'm': - maxforw=str_to_int(optarg); + maxforw=str_to_int(0, optarg); break; case 'M': message=1; @@ -545,7 +545,7 @@ int main(int argc, char *argv[]) sleep_ms = -2; } else { - sleep_ms = str_to_int(optarg); + sleep_ms = str_to_int(0, optarg); } break; case 'O': @@ -586,7 +586,7 @@ int main(int argc, char *argv[]) outbound_proxy=1; break; case 'P': - processes=str_to_int(optarg); + processes=str_to_int(0, optarg); break; case 'q': if (re) { @@ -607,7 +607,7 @@ int main(int argc, char *argv[]) break; case 'r': port = 0; - port=str_to_int(optarg); + port=str_to_int(0, optarg); if (rport) { 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; break; case 't': - trashchar=str_to_int(optarg); + trashchar=str_to_int(0, optarg); break; case 'T': trace=1; @@ -736,6 +736,9 @@ int main(int argc, char *argv[]) #endif #ifdef HAVE_STRNCASECMP printf(", CMP_CASE_INSENSITIVE"); +#endif +#ifdef DEBUG + printf(", DEBUG"); #endif printf("\n"); exit_code(0); @@ -744,10 +747,10 @@ int main(int argc, char *argv[]) warning_ext=1; break; case 'W': - nagios_warn = str_to_int(optarg); + nagios_warn = str_to_int(0, optarg); break; case 'x': - expires_t=str_to_int(optarg); + expires_t=str_to_int(0, optarg); break; #ifdef HAVE_GETOPT_LONG case 'X': @@ -755,7 +758,7 @@ int main(int argc, char *argv[]) break; #endif case 'z': - rand_rem=str_to_int(optarg); + rand_rem=str_to_int(0, optarg); if (rand_rem < 0 || rand_rem > 100) { fprintf(stderr, "error: z option must between 0 and 100\n"); exit_code(2); diff --git a/tests/check_helper.c b/tests/check_helper.c index 085d80e..78adabd 100644 --- a/tests/check_helper.c +++ b/tests/check_helper.c @@ -9,104 +9,99 @@ #define RUNNING_CHECK 1 START_TEST (test_is_number) { - fail_unless(is_number("") == 0, - "is_number(\"\") returned %d, instead of 0", is_number("")); - fail_unless(is_number("a") == 0, - "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("10i") == 0, - "is_number(\"10i\") returned %d, instead of 0", is_number("10i")); - fail_unless(is_number("p01") == 0, - "is_number(\"p01\") returned %d, instead of 0", is_number("p01")); - fail_unless(is_number("p2q") == 0, - "is_number(\"p2q\") returned %d, instead of 0", is_number("p2q")); - 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")); + /* failure cases */ + fail_unless(is_number("") == 0, "is_number(\"\") returned %d, instead of 0", is_number("")); + fail_unless(is_number("a") == 0, "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("10i") == 0, "is_number(\"10i\") returned %d, instead of 0", is_number("10i")); + fail_unless(is_number("p01") == 0, "is_number(\"p01\") returned %d, instead of 0", is_number("p01")); + fail_unless(is_number("p2q") == 0, "is_number(\"p2q\") returned %d, instead of 0", is_number("p2q")); + fail_unless(is_number("1b3") == 0, "is_number(\"1b3\") returned %d, instead of 0", is_number("1b3")); + + /* sucess cases */ + 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 START_TEST (test_str_to_int) { - fail_unless(str_to_int("") == -2, - "str_to_int(\"\") returned %d, instead of -2", str_to_int("")); - fail_unless(str_to_int(" ") == -2, - "str_to_int(\" \") returned %d, instead of -2", str_to_int(" ")); - fail_unless(str_to_int(" ") == -2, - "str_to_int(\"\\t\") returned %d, instead of -2", str_to_int(" ")); - fail_unless(str_to_int(" ") == -2, - "str_to_int(\" \\t\") returned %d, instead of -2", str_to_int(" ")); - fail_unless(str_to_int("a") == -2, - "str_to_int(\"a\") returned %d, instead of -2", str_to_int("a")); - fail_unless(str_to_int(" a") == -2, - "str_to_int(\" a\") returned %d, instead of -2", str_to_int(" a")); - fail_unless(str_to_int(" a ") == -2, - "str_to_int(\" a \") returned %d, instead of -2", str_to_int(" a ")); - fail_unless(str_to_int("ABC") == -2, - "str_to_int(\"ABC\") returned %d, instead of -2", str_to_int("ABC")); - fail_unless(str_to_int(" ABC") == -2, - "str_to_int(\" ABC\") returned %d, instead of -2", str_to_int(" ABC")); - fail_unless(str_to_int(" ABC ") == -2, - "str_to_int(\" ABC\\t\") returned %d, instead of -2", str_to_int(" ABC ")); - fail_unless(str_to_int("1") == 1, - "str_to_int(\"1\") returned %d, instead of 1", str_to_int("1")); - fail_unless(str_to_int("10") == 10, - "str_to_int(\"10\") returned %d, instead of 10", str_to_int("10")); - fail_unless(str_to_int(" 10") == 10, - "str_to_int(\" 10\") returned %d, instead of 10", str_to_int(" 10")); - fail_unless(str_to_int(" 10 ") == 10, - "str_to_int(\" 10 \") returned %d, instead of 10", str_to_int(" 10 ")); - fail_unless(str_to_int(" 10 ") == 10, - "str_to_int(\"\\t 10 \\t\") returned %d, instead of 10", str_to_int(" 10 ")); + /* failure because empty */ + 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(1, "") == -2, "str_to_int(1, \"\") returned %d, instead of -2", str_to_int(1, "")); + 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(1, " ") == -2, "str_to_int(1, \" \") returned %d, instead of -2", str_to_int(1, " ")); + 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(1, " ") == -2, "str_to_int(1, \"\\t\") returned %d, instead of -2", str_to_int(1, " ")); + 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(1, " ") == -2, "str_to_int(1, \" \\t\") returned %d, instead of -2", str_to_int(1, " ")); + + /* failure because non-int */ + 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(1, "a") == -2, "str_to_int(1, \"a\") returned %d, instead of -2", str_to_int(1, "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(1, " a") == -2, "str_to_int(1, \" a\") returned %d, instead of -2", str_to_int(1, " 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(1, " a ") == -2, "str_to_int(1, \" a \") returned %d, instead of -2", str_to_int(1, " a ")); + 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, "ABC") == -2, "str_to_int(1, \"ABC\") returned %d, instead of -2", str_to_int(1, "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, " ABC") == -2, "str_to_int(1, \" ABC\") returned %d, instead of -2", str_to_int(1, " ABC")); + 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(1, " ABC ") == -2, "str_to_int(1, \" ABC\\t\") returned %d, instead of -2", str_to_int(1, " ABC ")); + + /* success cases */ + 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(1, "1") == 1, "str_to_int(1, \"1\") returned %d, instead of 1", str_to_int(1, "1")); + 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, \" 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 START_TEST (test_is_ip) { - fail_unless(is_ip("") == 0, - "is_ip(\"\") returned %d, instead of 0", is_ip("")); - fail_unless(is_ip("0") == 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("1000") == 0, - "is_ip(\"1000\") returned %d, instead of 0", is_ip("1000")); - fail_unless(is_ip("1.0") == 0, - "is_ip(\"1.0\") returned %d, instead of 0", is_ip("1.0")); - fail_unless(is_ip("1.2.0") == 0, - "is_ip(\"1.2.0\") returned %d, instead of 0", is_ip("1.2.0")); - 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("1000.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("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("0.0.1000.0") == 0, - "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")); + /* failure cases */ + fail_unless(is_ip("") == 0, "is_ip(\"\") returned %d, instead of 0", is_ip("")); + fail_unless(is_ip("0") == 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("1000") == 0, "is_ip(\"1000\") returned %d, instead of 0", is_ip("1000")); + fail_unless(is_ip("1.0") == 0, "is_ip(\"1.0\") returned %d, instead of 0", is_ip("1.0")); + fail_unless(is_ip("1.2.0") == 0, "is_ip(\"1.2.0\") returned %d, instead of 0", is_ip("1.2.0")); + 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("1000.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("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("0.0.1000.0") == 0, "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")); + + /* success cases */ + 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 limitation ;) */ + 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 START_TEST (test_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")); - // this should work also without DNS - fail_unless(getaddress("localhost") == 16777343, - "getaddress(\"localhost\") returned %lu, instead of 16777343", getaddress("localhost")); + /* failure case */ + fail_unless(getaddress("") == 0, "getaddress(\"\") returned %lu, instead of 0", getaddress("")); + + /* success cases */ + fail_unless(getaddress("127.0.0.1") == 16777343, "getaddress(\"127.0.0.1\") returned %lu, instead of 16777343", getaddress("127.0.0.1")); + /* this should work also without DNS */ + fail_unless(getaddress("localhost") == 16777343, "getaddress(\"localhost\") returned %lu, instead of 16777343", getaddress("localhost")); } END_TEST @@ -115,23 +110,22 @@ START_TEST (test_insert_cr) { memset(ta, '\0', 15); insert_cr(ta); - 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); + 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); + memset(ta, '\0', 15); memcpy(ta, "test", 4); insert_cr(ta); - 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); + 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); + memset(ta, '\0', 15); memcpy(ta, "test\n", 5); insert_cr(ta); - 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); + 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); + memset(ta, '\0', 15); memcpy(ta, "foo\nbar\n", 8); insert_cr(ta); - 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); + 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); } END_TEST diff --git a/transport.c b/transport.c index acf8949..66f1676 100644 --- a/transport.c +++ b/transport.c @@ -623,9 +623,7 @@ void create_sockets(struct sipsak_con_data *cd) { } # endif /* USE_OPENSSL */ #endif /* USE_GNUTLS */ -#ifdef DEBUG - printf("initialized tls socket %i\n", cd->csock); -#endif + dbg("initialized tls socket %i\n", cd->csock); } #endif /* WITH_TLS_TRANSP */ } @@ -658,9 +656,7 @@ void close_sockets(struct sipsak_con_data *cd) { #endif /* WITH_TLS_TRANSP */ shutdown(cd->csock, SHUT_RDWR); } -#ifdef DEBUG - printf("sockets closed\n"); -#endif + dbg("sockets closed\n"); } 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 */ if (cd->csock == -1) { -#ifdef DEBUG - printf("\nusing un-connected socket for sending\n"); -#endif + dbg("\nusing un-connected socket for sending\n"); ret = sendto(cd->usock, mes, strlen(mes), 0, (struct sockaddr *) &(cd->adr), sizeof(struct sockaddr)); } else { -#ifdef DEBUG - printf("\nusing connected socket for sending\n"); -#endif + dbg("\nusing connected socket for sending\n"); #ifdef WITH_TLS_TRANSP if (transport == SIP_TLS_TRANSPORT) { # ifdef USE_GNUTLS @@ -883,22 +875,16 @@ int complete_mes(char *mes, int size) { char *tmp = NULL; cl = get_cl(mes); -#ifdef DEBUG - printf("CL: %i\n", cl); -#endif + dbg("CL: %i\n", cl); if (cl < 0){ if (verbose > 0) printf("missing CL header; waiting for more bytes...\n"); return 0; } tmp = get_body(mes); -#ifdef DEBUG - printf("body: '%s'\n", tmp); -#endif + dbg("body: '%s'\n", tmp); headers = tmp - mes; -#ifdef DEBUG - printf("length: %i, headers: %i\n", size, headers); -#endif + dbg("length: %i, headers: %i\n", size, headers); len = headers + cl; if (len == size) { 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); srcport = ntohs(udp_hdr->uh_sport); dstport = ntohs(udp_hdr->uh_dport); -#ifdef DEBUG - printf("\nlport: %i, rport: %i\n", lport, rport); -#endif + dbg("\nlport: %i, rport: %i\n", lport, rport); if ((srcport == lport) && (dstport == rport)) { printf(" (type: %u, code: %u)", icmp_hdr->icmp_type, icmp_hdr->icmp_code); #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 ret = gnutls_handshake(tls_session); if (ret < 0) { -#ifdef DEBUG - printf("*** TLS Handshake FAILED!!!\n"); -#endif + dbg("TLS Handshake FAILED!!!\n"); gnutls_perror(ret); exit_code(3); } -#ifdef DEBUG - else { - printf("*** TLS Handshake was completed!\n"); -#ifdef DEBUG + else if (verbose > 2) { + dbg(" TLS Handshake was completed!\n"); gnutls_session_info(tls_session); -#endif verify_certificate_simple(tls_session, domainname); //verify_certificate_chain(tls_session, domainname, cert_chain, cert_chain_length); } -#endif # else /* USE_GNUTLS */ # ifdef USE_OPENSSL ret = SSL_connect(ssl); if (ret == 1) { -#ifdef DEBUG - printf("TLS connect successful\n"); - printf("TLS connect: new connection using %s %s %d\n", - SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl), - SSL_get_cipher_bits(ssl, 0)); -#endif + dbg("TLS connect successful\n"); + if (verbose > 2) { + printf("TLS connect: new connection using %s %s %d\n", + SSL_get_cipher_version(ssl), SSL_get_cipher_name(ssl), + SSL_get_cipher_bits(ssl, 0)); + } cert = SSL_get_peer_certificate(ssl); if (cert != 0) { tls_dump_cert_info("TLS connect: server certificate", cert);