int PeiNew(pei **ppei, int prot_id) { if (ppei == NULL) { return -1; } *ppei = DMemMalloc(sizeof(pei)); if (*ppei == NULL) { return -1; } PeiInit(*ppei); (*ppei)->prot_id = prot_id; (*ppei)->time = time(NULL); return 0; }
static packet* IppDissector(packet *pkt) { http_msg *msg; pei *ppei; pei_component *cmpn; int ind, fd; unsigned char buff[IPP_BUFFER_SIZE]; char *pcl_file, *pdf_file; char cmd[IPP_FILENAME_PATH_SIZE*2]; ssize_t len; unsigned int offset; ipp_ver ver; FILE *pcl; struct stat fst; ppei = NULL; /* display info */ msg = (http_msg *)pkt->data; LogPrintf(LV_DEBUG, "IPP Dissector"); #ifdef XPL_CHECK_CODE if (msg->serial == 0) { LogPrintf(LV_FATAL, "IPP Dissector serial error"); exit(-1); } #endif /* check if post and if PCL file */ if (msg->mtd != HTTP_MT_POST || msg->req_body_size == 0) { /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); return NULL; } /* open body file */ fd = open(msg->req_body_file, O_RDONLY); if (fd == 0) { LogPrintf(LV_ERROR, "Can't open file:%s", msg->req_body_file); /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); return NULL; } len = read(fd, buff, IPP_BUFFER_SIZE); offset = 0; /* IPP version */ if (buff[0] == 1) { if (buff[1] == 0) { ver = IPP_1_0; } else if (buff[1] == 1) { ver = IPP_1_1; } else { LogPrintf(LV_WARNING, "Unknow version: %u.%u", buff[0], buff[1]); close(fd); /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); return NULL; } } else { LogPrintf(LV_WARNING, "Unknow version: %u.%u", buff[0], buff[1]); close(fd); /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); return NULL; } offset += 2; /* check if print job */ if (ntohs(*((unsigned short *)(buff+offset))) != PRINT_JOB) { close(fd); /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); return NULL; } offset += 2; offset += 4; /* skeep request id */ #warning "to improve" offset = ParseAttributes(buff, offset, len); /* i hope that IPP_BUFFER_SIZE is alwayse correct */ /* create PCL file */ pcl_file = DMemMalloc(IPP_FILENAME_PATH_SIZE); pdf_file = DMemMalloc(IPP_FILENAME_PATH_SIZE); sprintf(pcl_file, "%s/%s/ipp_%lu_%p_%i.pcl", ProtTmpDir(), IPP_TMP_DIR, time(NULL), msg, incr); sprintf(pdf_file, "%s/%s/ipp_%lu_%p_%i.pdf", ProtTmpDir(), IPP_TMP_DIR, time(NULL), msg, incr); incr++; pcl = fopen(pcl_file, "w+"); fwrite(buff+offset, 1, len-offset, pcl); len = read(fd, buff, IPP_BUFFER_SIZE); while (len != 0) { fwrite(buff, 1, len, pcl); len = read(fd, buff, IPP_BUFFER_SIZE); } fclose(pcl); close(fd); /* pdf conversion */ sprintf(cmd, "%s -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=%s %s", pcl6_path, pdf_file, pcl_file); system(cmd); fst.st_size = 0; stat(pdf_file, &fst); /* compose pei */ ppei = DMemMalloc(sizeof(pei)); PeiInit(ppei); ppei->prot_id = prot_id; ppei->serial = pkt->serial; ppei->time_cap = pkt->cap_sec; ppei->stack = ProtCopyFrame(pkt->stk, TRUE); /* url */ ind = 0; cmpn = DMemMalloc(sizeof(pei_component)); memset(cmpn, 0, sizeof(pei_component)); cmpn->eid = pei_url_id; cmpn->id = ind; cmpn->time_cap = msg->start_cap; cmpn->time_cap_end = msg->end_cap; cmpn->strbuf = msg->uri; msg->uri = NULL; ppei->components = cmpn; /* pdf */ ind++; cmpn->next = DMemMalloc(sizeof(pei_component)); cmpn = cmpn->next; memset(cmpn, 0, sizeof(pei_component)); cmpn->eid = pei_pdffile_id; cmpn->id = ind; cmpn->time_cap = msg->start_cap; cmpn->time_cap_end = msg->end_cap; cmpn->file_path = pdf_file; cmpn->file_size = fst.st_size; if (msg->error) cmpn->err = ELMT_ER_PARTIAL; /* pcl */ ind++; cmpn->next = DMemMalloc(sizeof(pei_component)); cmpn = cmpn->next; memset(cmpn, 0, sizeof(pei_component)); cmpn->eid = pei_pclfile_id; cmpn->id = ind; cmpn->time_cap = msg->start_cap; cmpn->time_cap_end = msg->end_cap; cmpn->file_path = pcl_file; cmpn->file_size = msg->req_body_size - offset; if (msg->error) cmpn->err = ELMT_ER_PARTIAL; /* free memory */ HttpMsgRemove(msg); HttpMsgFree(msg); PktFree(pkt); /* insert pei */ PeiIns(ppei); return NULL; }