static inline int ___query(ZMAP * zm, char *query, char *value, int value_len, int timeout) { int ok, i, ret; ZBUF *qb; ZMAP_MYSQL *db; MYSQL_RES *res = 0; MYSQL_ROW row; int numrows; char *p; db = (ZMAP_MYSQL *) (zm->db); qb = db->qb; zmap_query_expand(db->query_expand, db->query_format, query, 0); query = ZBUF_DATA(db->query_expand); ok = 0; res = 0; for (i = 0; i < 2; i++) { if (i) { ___close(zm); } if (___connect(zm) < 0) { return Z_ERR; } if (mysql_query(db->sql_db, query)) { continue; } if ((res = mysql_store_result(db->sql_db)) == 0) { continue; } ok = 1; break; } if (!ok) { ___close(zm); return Z_ERR; } numrows = mysql_num_rows(res); if (numrows == 0) { mysql_free_result(res); return Z_NONE; } ZBUF_RESET(qb); for (i = 0; i < numrows; i++) { row = mysql_fetch_row(res); zbuf_put(qb, '\n'); zbuf_strcat(qb, row[0]); } mysql_free_result(res); ZBUF_TERMINATE(qb); p = ZBUF_DATA(qb); ret = zstr_strncpy(value, p + 1, value_len); return ret; }
int CAuthorization::Parse( const char* hvalue ) { const char* space; const char* next = NULL; space = strchr(hvalue, ' '); /* SEARCH FOR SPACE */ if (space == NULL) return -1; if (space - hvalue < 1) return -1; this->auth_type = (char *) mm_malloc(space - hvalue + 1); zstr_strncpy(this->auth_type, hvalue, space - hvalue); for (; ;) { int parse_ok = 0; if (zstr_quoted_string_set("username", space, &(this->username), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("realm", space, &(this->realm), &next)) return -1; if (next == NULL) return 0; else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("nonce", space, &(this->nonce), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("uri", space, &(this->uri), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("response", space, &(this->response), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("digest", space, &(this->digest), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_token_set("algorithm", space, &(this->algorithm), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("cnonce", space, &(this->cnonce), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_quoted_string_set("opaque", space, &(this->opaque), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_token_set("qop", space, &(this->message_qop), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } if (zstr_token_set("nc", space, &(this->nonce_count), &next)) return -1; if (next == NULL) return 0; /* end of header detected! */ else if (next != space) { space = next; parse_ok++; } /* nothing was recognized: here, we should handle a list of unknown tokens where: token1 = ( token2 | quoted_text ) */ /* TODO */ if (0 == parse_ok) { const char* quote1, * quote2, * tmp; /* CAUTION */ /* parameter not understood!!! I'm too lazy to handle IT */ /* let's simply bypass it */ if (strlen(space) < 1) return 0; tmp = strchr(space + 1, ','); if (tmp == NULL) /* it was the last header */ return 0; quote1 = zstr_quote_find(space); if ((quote1 != NULL) && (quote1 < tmp)) /* this may be a quoted string! */ { quote2 = zstr_quote_find(quote1 + 1); if (quote2 == NULL) return -1; /* bad header format... */ if (tmp < quote2) /* the comma is inside the quotes! */ space = strchr(quote2, ','); else space = tmp; if (space == NULL) /* it was the last header */ return 0; } else space = tmp; /* continue parsing... */ } } return 0; /* ok */ }
int CFrom::Parse( const char *hvalue ) { const char* displayname; const char* url; const char* url_end; const char* gen_params; /* How to parse: we'll place the pointers: displayname => beginning of displayname url => beginning of url url_end => end of url gen_params => beginning of params examples: jack <sip:[email protected]>;tag=34erZ ^ ^ ^ ^ sip:[email protected];tag=34erZ ^ ^^ */ displayname = strchr(hvalue, '"'); url = strchr(hvalue, '<'); if (url != NULL) { url_end = strchr(url, '>'); if (url_end == NULL) return -1; } /* SIPit day2: this case was not supported first '"' is placed after '<' and after '>' <sip:[email protected];method=INVITE>;description="OPEN";expires=28800 if the fisrt quote is after '<' then this is not a quote for a displayname. */ if (displayname != NULL) { if (displayname > url) displayname = NULL; } if ((displayname == NULL) && (url != NULL)) { /* displayname IS A '*token' (not a quoted-string) */ if (hvalue != url) /* displayname exists */ { if (url - hvalue + 1 < 2) return -1; this->displayname = (char *) mm_malloc(url - hvalue + 1); if (this->displayname == NULL) return -1; zstr_clrncpy(this->displayname, hvalue, url - hvalue); } url++; /* place pointer on the beginning of url */ } else { if ((displayname != NULL) && (url != NULL)) { /* displayname IS A quoted-string (not a '*token') */ const char* first; const char* second = NULL; /* search for quotes */ first = zstr_quote_find(hvalue); if (first == NULL) return -1; /* missing quote */ second = zstr_quote_find(first + 1); if (second == NULL) return -1; /* missing quote */ if ((first > url)) return -1; if (second - first + 2 >= 2) { this->displayname = (char *) mm_malloc(second - first + 2); if (this->displayname == NULL) return -1; zstr_strncpy(this->displayname, first, second - first + 1); /* osip_clrspace(from->displayname); *//*should we do that? */ /* special case: "<sip:[email protected]>" <sip:[email protected]> */ } /* else displayname is empty? */ url = strchr(second + 1, '<'); if (url == NULL) return -1; /* '<' MUST exist */ url++; } else url = hvalue; /* field does not contains '<' and '>' */ } /* DISPLAY-NAME SET */ /* START of URL KNOWN */ url_end = strchr(url, '>'); if (url_end == NULL) /* sip:[email protected];tag=023 */ { /* We are sure ';' is the delimiter for from-parameters */ char* host = strchr(url, '@'); if (host != NULL) gen_params = strchr(host, ';'); else gen_params = strchr(url, ';'); if (gen_params != NULL) url_end = gen_params - 1; else url_end = url + strlen(url); } else /* jack <sip:[email protected];user=phone>;tag=azer */ { gen_params = strchr(url_end, ';'); url_end--; /* place pointer on the beginning of url */ } if (gen_params != NULL) /* now we are sure a param exist */ if (GenericParamParseAll(&this->gen_params, gen_params) == -1) { return -1; } /* set the url */ { char* tmp; int i = 0; if (url_end - url + 2 < 7) return -1; tmp = (char *) mm_malloc(url_end - url + 2); if (tmp == NULL) return -1; zstr_strncpy(tmp, url, url_end - url + 1); if (this->url == NULL) { this->url = new CUrl; } if (this->url != NULL) { this->url->Parse(tmp); } mm_free(tmp); if (i != 0) return -1; } return 0; }
int CVia::Parse( const char *hvalue ) { const char* version; const char* protocol; const char* host; const char* ipv6host; const char* port; const char* via_params; const char* comment; version = strchr(hvalue, '/'); if (version == NULL) return -1; protocol = strchr(version + 1, '/'); if (protocol == NULL) return -1; /* set the version */ if (protocol - version < 2) return -1; m_pcVersion = (char *) MallocAndZero(protocol - version); if (m_pcVersion == NULL) return -1; zstr_clrncpy(m_pcVersion, version + 1, protocol - version - 1); /* Here: we avoid matching an additionnal space */ host = strchr(protocol + 1, ' '); if (host == NULL) return -1; /* fixed in 0.8.4 */ if (host == protocol + 1) /* there are extra SPACE characters */ { while (0 == strncmp(host, " ", 1)) { host++; if (strlen(host) == 1) return -1; /* via is malformed */ } /* here, we match the real space located after the protocol name */ host = strchr(host + 1, ' '); if (host == NULL) return -1; /* fixed in 0.8.4 */ } /* set the protocol */ if (host - protocol < 2) return -1; m_pcProtocol = (char *) MallocAndZero(host - protocol); if (m_pcProtocol == NULL) return -1; zstr_clrncpy(m_pcProtocol, protocol + 1, host - protocol - 1); /* comments in Via are not allowed any more in the latest draft (09) */ comment = strchr(host, '('); if (comment != NULL) { char* end_comment; end_comment = strchr(host, ')'); if (end_comment == NULL) return -1; /* if '(' exist ')' MUST exist */ if (end_comment - comment < 2) return -1; m_pcComment = (char *) MallocAndZero(end_comment - comment); if (m_pcComment == NULL) return -1; zstr_clrncpy(m_pcComment, comment + 1, end_comment - comment - 1); comment--; } else comment = host + strlen(host); via_params = strchr(host, ';'); if ((via_params != NULL) && (via_params < comment)) /* via params exist */ { char* tmp; if (comment - via_params + 1 < 2) return -1; tmp = (char *) MallocAndZero(comment - via_params + 1); if (tmp == NULL) return -1; zstr_strncpy(tmp, via_params, comment - via_params); if (GenericParamParseAll(&(m_listVia_params), tmp)) { free(tmp); return -1; } free(tmp); } if (via_params == NULL) via_params = comment; /* add ipv6 support (0.8.4) */ /* Via: SIP/2.0/UDP [mlke::zeezf:ezfz:zef:zefzf]:port;.... */ ipv6host = strchr(host, '['); if (ipv6host != NULL && ipv6host < via_params) { port = strchr(ipv6host, ']'); if (port == NULL || port > via_params) return -1; if (port - ipv6host < 2) return -1; m_pcHost = (char *) MallocAndZero(port - ipv6host); if (m_pcHost == NULL) return -1; zstr_clrncpy(m_pcHost, ipv6host + 1, port - ipv6host - 1); port = strchr(port, ':'); } else { port = strchr(host, ':'); ipv6host = NULL; } if ((port != NULL) && (port < via_params)) { if (via_params - port < 2) return -1; m_pcPort = (char *) MallocAndZero(via_params - port); if (m_pcPort == NULL) return -1; zstr_clrncpy(m_pcPort, port + 1, via_params - port - 1); } else port = via_params; /* host is already set in the case of ipv6 */ if (ipv6host != NULL) return 0; if (port - host < 2) return -1; m_pcHost = (char *) MallocAndZero(port - host); if (m_pcHost == NULL) return -1; zstr_clrncpy(m_pcHost, host + 1, port - host - 1); return 0; }