char *print_connection(connection_t *connection) { char *result = NULL; char *connection_info = NULL; char *request_endpoint_info = NULL; char *response_endpoint_info = NULL; int success; success = asprintf( &connection_info, "%s\n | %-39s |\n%s\n | %-39s |\n%s\n | %-18s | %-18s |\n%s\n | %-18s | %-18s |\n%s\n", get_hdr_line(0), "Connection by Call-ID", get_line(0), connection->call_id, get_hdr_line(1), "Request IP", "Response IP", get_line(1), connection->request_endpoint != NULL ? *(connection->request_endpoint_ip) : "none", connection->response_endpoint != NULL ? *(connection->response_endpoint_ip) : "none", get_line(0) ); if (success == -1) { ERR("asprintf failed to allocate memory\n"); return NULL; } request_endpoint_info = print_endpoint(connection->request_endpoint, "Request endpoint"); response_endpoint_info = print_endpoint(connection->response_endpoint, "Response endpoint"); success = asprintf( &result, "%s\n%s%s\n\n\n", connection_info, request_endpoint_info, response_endpoint_info ); if (connection_info != NULL) free(connection_info); if (request_endpoint_info != NULL) free(request_endpoint_info); if (response_endpoint_info != NULL) free(response_endpoint_info); if (success == -1) { ERR("asprintf failed to allocate memory\n"); return NULL; } return result; }
/* This function parses the header of an email that * can be read from an open file handle. * Note, T2D: There are a few short cuts that should be fixed. */ int parse_header(FILE *fp, email_hdr_t *eh, char *errstr) { int len; int blank; /* various flags to indicate what header parts were already seen */ int header= 0; int gotsubj= 0; int gotenc= 0; int gotset= 0; int waitbound= 0; char *p; char *s; char buf[CDDBBUFSIZ]; char buf2[CDDBBUFSIZ]; char set[CDDBBUFSIZ]; char enc[CDDBBUFSIZ]; eh->eh_flags = 0; eh->eh_class = EC_NONE; eh->eh_charset = CC_US_ASCII; eh->eh_encoding = CE_7BIT; while(get_hdr_line(buf, sizeof(buf), fp)) { strcpy(buf2, buf); /* pointless copy */ blank = (buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n')); if(!strncmp(buf, from, strlen(from)) || !strncmp(buf, x_cddbd_from, strlen(rpath)) || (!header && !strncmp(buf, rpath, strlen(rpath)))) { p = buf; while(*p != '\0' && !isspace(*p)) p++; while(*p != '\0' && isspace(*p)) p++; if(*p != '\0') { cpy_sender(p, eh->eh_rcpt, eh->eh_to, eh->eh_host); eh->eh_flags |= EH_RCPT; header++; } continue; } if(!header) continue; if(!strncmp(buf, subj, strlen(subj))) { p = buf; while(*p != '\0' && !isspace(*p)) p++; while(*p != '\0' && isspace(*p)) p++; if(sscanf(p, cmd_send_subj, eh->eh_serial)) eh->eh_class = EC_COMMAND; else if(sscanf(p, sub_send_subj, eh->eh_category, &eh->eh_discid) == 2) eh->eh_class = EC_SUBMIT; else { strcpy(errstr, "Malformed subject in header"); return EE_ERROR; } strip_crlf(p); strncpy(eh->eh_subj, p, sizeof(eh->eh_subj)); eh->eh_subj[sizeof(eh->eh_subj) - 1] = '\0'; gotsubj++; eh->eh_flags |= EH_SUBJECT; } else if(!cddbd_strncasecmp(buf, x_cddbd_crc, strlen(x_cddbd_crc))) { /* Skip junk. */ p = buf + strlen(x_cddbd_crc) + 1; while(*p != '\0' && !isxdigit(*p)) p++; /* Find the crc. */ if(sscanf(p, "%255[a-zA-Z0-9]", buf2) != 1 || strlen(buf2) != CDDBXCRCLEN) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Malformed %s in header", x_cddbd_crc); return EE_ERROR; } strcpy(eh->eh_crc, buf2); eh->eh_flags |= EH_CRC; } else if(!cddbd_strncasecmp(buf, x_cddbd_echo, strlen(x_cddbd_echo))) { /* Skip junk. */ p = buf + strlen(x_cddbd_echo) + 1; while(*p != '\0' && isspace(*p)) p++; /* Find the echo string. */ if(sscanf(p, "%255[^\n\r]", buf2) != 1 || strlen(buf2) > CDDBXECHOLEN) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Invalid %s in header", x_cddbd_echo); return EE_ERROR; } strcpy(eh->eh_echo, buf2); eh->eh_flags |= EH_ECHO; } else if(!cddbd_strncasecmp(buf, x_cddbd_note, strlen(x_cddbd_note))) { /* Skip junk. */ p = buf + strlen(x_cddbd_note) + 1; while(*p != '\0' && isspace(*p)) p++; /* Find the note string. */ if(sscanf(p, "%255[^\n\r]", buf2) != 1 || strlen(buf2) > CDDBXNOTELEN) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Invalid %s in header", x_cddbd_note); return EE_ERROR; } strcpy(eh->eh_note, buf2); eh->eh_flags |= EH_NOTE; } else if(!cddbd_strncasecmp(buf, content_type, strlen(content_type))) { if((p = cddbd_strcasestr(buf, charset)) != 0) { /* Skip junk. */ p += strlen(charset); while(*p != '\0' && !isalpha(*p) && !isdigit(*p)) p++; /* Find the charset. */ if(sscanf(p, "%255[a-zA-Z0-9_-]", set) != 1) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Malformed %s in header", content_type); return EE_ERROR; } gotset++; } if((p = cddbd_strcasestr(buf, boundary)) != 0) { /* Skip junk. */ p += strlen(boundary); while(*p != '\0') { if(*p == '=') { p++; break; } p++; } if(*p == '"') { p++; s = "%255[^\"]"; } else s = "%255s"; /* Find the boundary string. */ if(sscanf(p, s, eh->eh_boundary) != 1) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Malformed %s in header", content_type); return EE_ERROR; } len = strlen(eh->eh_boundary); if(eh->eh_boundary[len - 1] == '"' || eh->eh_boundary[len - 1] == ';') eh->eh_boundary[len - 1] = '\0'; eh->eh_flags |= EH_BOUNDARY; waitbound++; } } else if(!cddbd_strncasecmp(buf, content_encoding, strlen(content_encoding))) { /* Skip junk. */ p = buf + strlen(content_encoding); while(*p != '\0' && !isalpha(*p) && !isdigit(*p)) p++; /* Find the encoding. */ if(sscanf(p, "%[a-zA-Z0-9_-]", enc) != 1) { cddbd_snprintf(errstr, CDDBBUFSIZ, "Malformed %s in header", content_encoding); return EE_ERROR; } gotenc++; } else if(waitbound && strstr(buf, eh->eh_boundary) != NULL) { waitbound = 0; } else if(blank && !waitbound) { get_rmt_hostname(-1, eh->eh_host, rhost); if(!gotsubj) { strcpy(errstr, "Missing subject in header"); return EE_ERROR; } if(gotenc && !get_encoding(enc, eh, errstr)) return EE_ERROR; if(gotset && !get_charset(set, eh, errstr)) return EE_ERROR; return EE_OK; } } if(waitbound) strcpy(errstr, "Missing boundary string"); else strcpy(errstr, "Malformed email header"); return EE_ERROR; }