/* * Write a log message out to all output channels. */ static void log_output(Logger *logger) { LOG_Output *out; for (out = listHead(&logger->outputs); out; out = listNext(out)) { pthread_mutex_lock(&out->output); switch(out->type) { case LOG_OT_UDP: case LOG_OT_TCP: case LOG_OT_FD: tcpWrite(out->u.fd, bufGet(&logger->scratch), bufLen(&logger->scratch)); break; case LOG_OT_FILE: case LOG_OT_FP: fwrite(bufGet(&logger->scratch), bufLen(&logger->scratch), 1, out->u.fp); fflush(out->u.fp); break; case LOG_OT_SYSLOG: syslog(out->u.priority, "%s", bufGet(&logger->scratch)); break; } pthread_mutex_unlock(&out->output); } }
PUBLIC bool websProcessUploadData(Webs *wp) { char *line, *nextTok; ssize len, nbytes; bool canProceed; line = 0; canProceed = 1; while (canProceed && !wp->finalized && wp->uploadState != UPLOAD_CONTENT_END) { if (wp->uploadState == UPLOAD_BOUNDARY || wp->uploadState == UPLOAD_CONTENT_HEADER) { /* Parse the next input line */ line = wp->input.servp; if ((nextTok = memchr(line, '\n', bufLen(&wp->input))) == 0) { /* Incomplete line */ canProceed = 0; break; } *nextTok++ = '\0'; nbytes = nextTok - line; websConsumeInput(wp, nbytes); strim(line, "\r", WEBS_TRIM_END); len = strlen(line); if (line[len - 1] == '\r') { line[len - 1] = '\0'; } } switch (wp->uploadState) { case 0: initUpload(wp); break; case UPLOAD_BOUNDARY: processContentBoundary(wp, line); break; case UPLOAD_CONTENT_HEADER: processUploadHeader(wp, line); break; case UPLOAD_CONTENT_DATA: canProceed = processContentData(wp); if (bufLen(&wp->input) < wp->boundaryLen) { /* Incomplete boundary - return to get more data */ canProceed = 0; } break; case UPLOAD_CONTENT_END: break; } } bufCompact(&wp->input); return canProceed; }
/* * Return -1, 1 or 0 if <left> is smaller than, greater than or equal to <right>, either in size, or * (when both have the same size) according to memcmp(). */ int bufCompare(const Buffer *left, const Buffer *right) { int len1 = bufLen(left); int len2 = bufLen(right); if (len1 < len2) return -1; else if (len2 > len1) return 1; else return memcmp(bufGet(left), bufGet(right), len1); }
int PushCliConn::checkPackLen(){ head h; if(bufLen() < (int)sizeof(h)){ return 0; } peekBuf((char*)&h, sizeof(h)); h.magic = htonl(h.magic); h.len = htonl(h.len); if(h.len < (int)sizeof(h) || h.len > 1024 * 1024){ return -1; } if(h.len <= bufLen()){ return h.len; } return 0; }
PUBLIC int websProcessCgiData(Webs *wp) { ssize nbytes; nbytes = bufLen(&wp->input); trace(5, "cgi: write %d bytes to CGI program", nbytes); if (write(wp->cgifd, wp->input.servp, (int) nbytes) != nbytes) { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR| WEBS_CLOSE, "Can't write to CGI gateway"); return -1; } websConsumeInput(wp, nbytes); trace(5, "cgi: write %d bytes to CGI program", nbytes); return 0; }
static void ns_handle_data(Dispatcher *dis, int fd, void *udata) { char data[9000]; NS *ns = (NS *) dis; NS_Connection *conn = paGet(&ns->connections, fd); int n; P dbgPrint(stderr, "Reading from fd %d...\n", fd); n = read(fd, data, sizeof(data)); P dbgPrint(stderr, "read() returned %d.\n", n); if (n > 0) { P dbgPrint(stderr, "Adding to incoming buffer.\n"); bufAdd(&conn->incoming, data, n); if (ns->on_socket_cb != NULL) { P dbgPrint(stderr, "Calling on_socket_cb.\n"); ns->on_socket_cb(ns, fd, bufGet(&conn->incoming), bufLen(&conn->incoming), ns->on_socket_udata); } } else if (n == 0) { P dbgPrint(stderr, "End of file, disconnecting.\n"); nsDisconnect(ns, fd); if (ns->on_disconnect_cb != NULL) { P dbgPrint(stderr, "Calling on_disconnect_cb.\n"); ns->on_disconnect_cb(ns, fd, ns->on_disconnect_udata); } } else { P dbgPrint(stderr, "Error, disconnecting.\n"); nsDisconnect(ns, fd); if (ns->on_error_cb != NULL) { P dbgPrint(stderr, "Calling on_error_cb.\n"); ns->on_error_cb(ns, fd, errno, ns->on_error_udata); } } }
/* * Handle writable file descriptor <fd>. */ void disHandleWritable(Dispatcher *dis, int fd) { int r; DIS_File *file = paGet(&dis->files, fd); dbgAssert(stderr, file != NULL, "unknown file descriptor: %d\n", fd); r = write(fd, bufGet(&file->outgoing), bufLen(&file->outgoing)); if (r > 0) { bufTrim(&file->outgoing, r, 0); } }
PUBLIC int websProcessPutData(Webs *wp) { ssize nbytes; assert(wp); assert(wp->putfd >= 0); assert(wp->input.buf); nbytes = bufLen(&wp->input); wp->putLen += nbytes; if (wp->putLen > ME_GOAHEAD_LIMIT_PUT) { websError(wp, HTTP_CODE_REQUEST_TOO_LARGE | WEBS_CLOSE, "Put file too large"); return -1; } if (write(wp->putfd, wp->input.servp, (int) nbytes) != nbytes) { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR | WEBS_CLOSE, "Cannot write to file"); return -1; } websConsumeInput(wp, nbytes); return 0; }
/** @brief Send message to the server (PC or Retail system) @param size - return size of data @param decrypt - if TRUE, then decrypt received data by session key @param control - return control character @return TRUE if OK, or error code */ Bool tcpSendMessage(int Handle, byte *Data, uint32 DataSize, uint32 TimeoutMSec) { StreamMethod Method = smRecvSend; int ret; Bool result = FALSE; uint32 size = DataSize; uint32 AlignedSize = GetAlignedSize(size); byte LengthField[MAXLENGTHFIELDSIZE]; byte LengthFieldSize; tlvMakeLengthField(size, LengthField, &LengthFieldSize); byte MaxAttempsCount = 5; byte ack; tBuffer sendbuf; byte DataToSend[AlignedSize + MAXLENGTHFIELDSIZE + 1 + MAXCRCBUFFERSIZE]; // + max data size buffer, ETX, max CRC buffer bufInit(&sendbuf, DataToSend, sizeof(DataToSend)); // init buffer for writing (and clear of DataToSend) bufApp(&sendbuf, LengthField, LengthFieldSize); memcpy(DataToSend + sendbuf.pos, Data, DataSize); // просто копирование, без шифрования sendbuf.pos += AlignedSize; // DataToSend уже лежит в буфере на отправку bufAppByte(&sendbuf, pccETX); // append ETX to buffer byte CRCBuffer[MAXCRCBUFFERSIZE]; byte CRCBufferSize = crcMakeField(sendbuf.ptr, (word)sendbuf.pos, CRCBuffer); bufApp(&sendbuf, CRCBuffer, CRCBufferSize); /* printf("CRC Buffer: "); PrintHEX(CRCBuffer, CRCBufferSize);*/ byte AttempsCount = 1; lblSendAgain: ret = sendByte(Handle, Method, pccSTX); // send control char "start of packet" if (ret > 0) { ret = sendBuffer(Handle, Method, bufPtr(&sendbuf), bufLen(&sendbuf)); // send packet if (ret == bufLen(&sendbuf)) { // and wait for acknowledgment lblWaitAskAgain: ack = 0; ret = recvByte(Handle, Method, &ack, TimeoutMSec); if (ret > 0) { switch (ack) { case pccACK: // good result = TRUE; break; case pccBEL: // please, wait sendByte(Handle, Method, pccBEL); goto lblWaitAskAgain; case pccNAK: // send packet again if (AttempsCount <= MaxAttempsCount) { AttempsCount++; goto lblSendAgain; } else result = FALSE; break; default: // unknown state result = FALSE; break; } } else result = FALSE; } else result = FALSE; } else result = FALSE; //ptarr = memFree(ptarr); return result; }
static int processContentData(Webs *wp) { WebsUpload *file; WebsBuf *content; ssize size, nbytes; char *data, *bp; content = &wp->input; file = wp->currentFile; size = bufLen(content); if (size < wp->boundaryLen) { /* Incomplete boundary. Return and get more data */ return 0; } if ((bp = getBoundary(wp, content->servp, size)) == 0) { trace(7, "uploadFilter: Got boundary filename %x", wp->clientFilename); if (wp->clientFilename) { /* No signature found yet. probably more data to come. Must handle split boundaries. */ data = content->servp; nbytes = ((int) (content->endp - data)) - (wp->boundaryLen - 1); if (nbytes > 0 && writeToFile(wp, content->servp, nbytes) < 0) { return -1; } websConsumeInput(wp, nbytes); /* Get more data */ return 0; } } data = content->servp; nbytes = (bp) ? (bp - data) : bufLen(content); if (nbytes > 0) { websConsumeInput(wp, nbytes); /* This is the CRLF before the boundary */ if (nbytes >= 2 && data[nbytes - 2] == '\r' && data[nbytes - 1] == '\n') { nbytes -= 2; } if (wp->clientFilename) { /* Write the last bit of file data and add to the list of files and define environment variables */ if (writeToFile(wp, data, nbytes) < 0) { return -1; } hashEnter(wp->files, wp->uploadVar, valueSymbol(file), 0); defineUploadVars(wp); } else { /* Normal string form data variables */ data[nbytes] = '\0'; trace(5, "uploadFilter: form[%s] = %s", wp->uploadVar, data); websDecodeUrl(wp->uploadVar, wp->uploadVar, -1); websDecodeUrl(data, data, -1); websSetVar(wp, wp->uploadVar, data); } } if (wp->clientFilename) { /* Now have all the data (we've seen the boundary) */ close(wp->upfd); wp->upfd = -1; wp->clientFilename = 0; wfree(wp->uploadTmp); wp->uploadTmp = 0; } wp->uploadState = UPLOAD_BOUNDARY; return 0; }
PUBLIC int websProcessUploadData(Webs *wp) { char *line, *nextTok; ssize len, nbytes; int done, rc; for (done = 0, line = 0; !done; ) { if (wp->uploadState == UPLOAD_BOUNDARY || wp->uploadState == UPLOAD_CONTENT_HEADER) { /* Parse the next input line */ line = wp->input.servp; if ((nextTok = memchr(line, '\n', bufLen(&wp->input))) == 0) { /* Incomplete line */ break; } *nextTok++ = '\0'; nbytes = nextTok - line; websConsumeInput(wp, nbytes); strim(line, "\r", WEBS_TRIM_END); len = strlen(line); if (line[len - 1] == '\r') { line[len - 1] = '\0'; } } switch (wp->uploadState) { case 0: if (initUpload(wp) < 0) { done++; } break; case UPLOAD_BOUNDARY: if (processContentBoundary(wp, line) < 0) { done++; } break; case UPLOAD_CONTENT_HEADER: if (processUploadHeader(wp, line) < 0) { done++; } break; case UPLOAD_CONTENT_DATA: if ((rc = processContentData(wp)) < 0) { done++; } if (bufLen(&wp->input) < wp->boundaryLen) { /* Incomplete boundary - return to get more data */ done++; } break; case UPLOAD_CONTENT_END: done++; break; } } if (!websValid(wp)) { return -1; } bufCompact(&wp->input); return 0; }
/* * Concatenate <addition> onto <base> and return <base>. */ Buffer *bufCat(Buffer *base, const Buffer *addition) { bufAdd(base, bufGet(addition), bufLen(addition)); return base; }
/* * Return the number of bytes available in the incoming buffer for <fd>. */ int nsAvailable(NS *ns, int fd) { NS_Connection *conn = paGet(&ns->connections, fd); return bufLen(&conn->incoming); }
int main(int argc, char *argv[]) { Buffer buf1 = { 0 }; Buffer buf2 = { 0 }; Buffer *buf3; bufClear(&buf1); make_sure_that(bufLen(&buf1) == 0); make_sure_that(bufIsEmpty(&buf1)); bufAdd(&buf1, "ABCDEF", 3); make_sure_that(bufLen(&buf1) == 3); make_sure_that(!bufIsEmpty(&buf1)); make_sure_that(strcmp(bufGet(&buf1), "ABC") == 0); bufAddC(&buf1, 'D'); make_sure_that(bufLen(&buf1) == 4); make_sure_that(strcmp(bufGet(&buf1), "ABCD") == 0); bufAddF(&buf1, "%d", 1234); make_sure_that(bufLen(&buf1) == 8); make_sure_that(strcmp(bufGet(&buf1), "ABCD1234") == 0); bufAddS(&buf1, "XYZ"); make_sure_that(bufLen(&buf1) == 11); make_sure_that(strcmp(bufGet(&buf1), "ABCD1234XYZ") == 0); bufSet(&buf1, "ABCDEF", 3); make_sure_that(bufLen(&buf1) == 3); make_sure_that(strcmp(bufGet(&buf1), "ABC") == 0); bufSetC(&buf1, 'D'); make_sure_that(bufLen(&buf1) == 1); make_sure_that(strcmp(bufGet(&buf1), "D") == 0); bufSetF(&buf1, "%d", 1234); make_sure_that(bufLen(&buf1) == 4); make_sure_that(strcmp(bufGet(&buf1), "1234") == 0); bufSetS(&buf1, "ABCDEF"); make_sure_that(bufLen(&buf1) == 6); make_sure_that(strcmp(bufGet(&buf1), "ABCDEF") == 0); bufClear(&buf1); make_sure_that(bufLen(&buf1) == 0); make_sure_that(strcmp(bufGet(&buf1), "") == 0); bufSet(&buf1, "ABC", 3); bufSet(&buf2, "DEF", 3); buf3 = bufCat(&buf1, &buf2); make_sure_that(&buf1 == buf3); make_sure_that(bufLen(&buf1) == 6); make_sure_that(strcmp(bufGet(&buf1), "ABCDEF") == 0); make_sure_that(bufLen(&buf2) == 3); make_sure_that(strcmp(bufGet(&buf2), "DEF") == 0); bufSetF(&buf1, "ABCDEF"); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 0, 0)), "ABCDEF") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 1, 0)), "BCDEF") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 0, 1)), "BCDE") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 1, 1)), "CD") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 3, 3)), "") == 0); bufPack(&buf1, PACK_INT8, 0x01, PACK_INT16, 0x0123, PACK_INT32, 0x01234567, PACK_INT64, 0x0123456789ABCDEF, PACK_FLOAT, 0.0, PACK_DOUBLE, 0.0, PACK_STRING, "Hoi1", PACK_DATA, "Hoi2", 4, PACK_RAW, "Hoi3", 4, END); make_sure_that(bufLen(&buf1) == 47); make_sure_that(memcmp(bufGet(&buf1), "\x01" "\x01\x23" "\x01\x23\x45\x67" "\x01\x23\x45\x67\x89\xAB\xCD\xEF" "\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x04Hoi1" "\x00\x00\x00\x04Hoi2" "Hoi3", 47) == 0); { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; float f32; double f64; char *string, *data, raw[4]; int data_size; bufUnpack(&buf1, PACK_INT8, &u8, PACK_INT16, &u16, PACK_INT32, &u32, PACK_INT64, &u64, PACK_FLOAT, &f32, PACK_DOUBLE, &f64, PACK_STRING, &string, PACK_DATA, &data, &data_size, PACK_RAW, &raw, sizeof(raw), END); make_sure_that(u8 == 0x01); make_sure_that(u16 == 0x0123); make_sure_that(u32 == 0x01234567); make_sure_that(u64 == 0x0123456789ABCDEF); make_sure_that(f32 == 0.0); make_sure_that(f64 == 0.0); make_sure_that(memcmp(string, "Hoi1", 5) == 0); make_sure_that(memcmp(data, "Hoi2", 4) == 0); make_sure_that(data_size == 4); make_sure_that(memcmp(raw, "Hoi3", 4) == 0); } { bufUnpack(&buf1, PACK_INT8, NULL, PACK_INT16, NULL, PACK_INT32, NULL, PACK_INT64, NULL, PACK_FLOAT, NULL, PACK_DOUBLE, NULL, PACK_STRING, NULL, PACK_DATA, NULL, NULL, PACK_RAW, NULL, 4, END); } { const char *name[] = { "Mills", "Berry", "Buck", "Stipe" }; bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, TRUE, "%s", name[0]); make_sure_that(strcmp(bufGet(&buf1), "Mills") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[1]); make_sure_that(strcmp(bufGet(&buf1), "Mills and Berry") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[1]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[2]); make_sure_that(strcmp(bufGet(&buf1), "Mills, Berry and Buck") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[1]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[2]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[3]); make_sure_that(strcmp(bufGet(&buf1), "Mills, Berry, Buck and Stipe") == 0); } bufReset(&buf1); bufReset(&buf2); return errors; }
/* * Prepare a call to select() based on the files and timeouts set in <dis>. The necessary parameters * to select() are returned through <nfds>, <rfds>, <wfds> and <tv> (exception-fds should be set to * NULL). <*tv> is set to point to an appropriate timeout value, or NULL if no timeout is to be set. * This function will clear <rfds> and <wfds>, so if callers want to add their own file descriptors, * they should do so after calling this function. This function returns -1 if the first timeout * should already have occurred, otherwise 0. */ int disPrepareSelect(Dispatcher *dis, int *nfds, fd_set *rfds, fd_set *wfds, struct timeval **tv) { int fd; double delta_t; DIS_Timer *timer; *nfds = paCount(&dis->files); FD_ZERO(rfds); FD_ZERO(wfds); P dbgPrint(stderr, "File descriptors:"); for (fd = 0; fd < *nfds; fd++) { DIS_File *file = paGet(&dis->files, fd); if (file == NULL) continue; P fprintf(stderr, " %d", fd); FD_SET(fd, rfds); if (bufLen(&file->outgoing) > 0) FD_SET(fd, wfds); P { fprintf(stderr, " (%s%s)", FD_ISSET(fd, rfds) ? "r" : "", FD_ISSET(fd, wfds) ? "w" : ""); } } P fprintf(stderr, "\n"); P dbgPrint(stderr, "%d pending timers:\n", listLength(&dis->timers)); P for (timer = listHead(&dis->timers); timer; timer = listNext(timer)) { fprintf(stderr, "\t%f seconds\n", timer->t - nowd()); } if ((timer = listHead(&dis->timers)) == NULL) { *tv = NULL; } else if ((delta_t = timer->t - nowd()) < 0) { #if 0 P dbgPrint(stderr, "First timer %f seconds ago, return -1\n", -delta_t); return -1; #endif dis->tv.tv_sec = 0; dis->tv.tv_usec = 0; *tv = &dis->tv; } else { P dbgPrint(stderr, "First timer in %f seconds.\n", delta_t); dis->tv.tv_sec = (int) delta_t; dis->tv.tv_usec = 1000000 * fmod(delta_t, 1.0); *tv = &dis->tv; } return 0; }
void mcat(int argc, char **argv, int type) { struct device *dev; struct device out_dev; char drive, name[EXPAND_BUF]; char errmsg[200]; Stream_t *Stream; char buf[BUF_SIZE]; mt_off_t address = 0; char mode = O_RDONLY; int optindex = 1; size_t len; noPrivileges = 1; if (argc < 2) { usage(); } if (argv[1][0] == '-') { if (argv[1][1] != 'w') { usage(); } mode = O_WRONLY; optindex++; } if (argc - optindex < 1) usage(); if (!argv[optindex][0] || argv[optindex][1] != ':' || argv[optindex][2]) { usage(); } drive = toupper(argv[optindex][0]); /* check out a drive whose letter and parameters match */ sprintf(errmsg, "Drive '%c:' not supported", drive); Stream = NULL; for (dev=devices; dev->name; dev++) { FREE(&Stream); if (dev->drive != drive) continue; out_dev = *dev; expand(dev->name,name); #ifdef USING_NEW_VOLD strcpy(name, getVoldName(dev, name)); #endif Stream = 0; #ifdef USE_XDF Stream = XdfOpen(&out_dev, name, mode, errmsg, 0); if(Stream) out_dev.use_2m = 0x7f; #endif #ifdef USE_FLOPPYD if(!Stream) Stream = FloppydOpen(&out_dev, dev, name, mode, errmsg, 0, 1); #endif if (!Stream) Stream = SimpleFileOpen(&out_dev, dev, name, mode, errmsg, 0, 1, 0); if( !Stream) continue; break; } /* print error msg if needed */ if ( dev->drive == 0 ){ FREE(&Stream); fprintf(stderr,"%s\n",errmsg); exit(1); } if (mode == O_WRONLY) { mt_size_t size=0; size = out_dev.sectors * out_dev.heads * out_dev.tracks; size *= 512; while ((len = fread(buf, 1, bufLen(BUF_SIZE, size, address), stdin)) > 0) { int r = WRITES(Stream, buf, address, len); fprintf(stderr, "Wrote to %d\n", (int) address); if(r < 0) break; address += len; } } else { while ((len = READS(Stream, buf, address, BUF_SIZE)) > 0) { fwrite(buf, 1, len, stdout); address += len; } } FREE(&Stream); exit(0); }
/* * Process input from <in> and write the result to <out>. The name of the original file from which * prototypes are to be extracted is in <input>. */ static int process(const char *input, FILE *in, FILE *out) { char *current_file = strdup(input); Buffer comment = { 0 }, declaration = { 0 }; int c; while ((c = fgetc(in)) != EOF) { if (c == '#') { handle_preprocessor_line(in, ¤t_file); } else if (c == '/') { bufSetC(&comment, c); handle_comment(in, &comment); } else if (!isspace(c) && c != ';') { bufSetC(&declaration, c); handle_declaration(in, &declaration); if (strchr(bufGet(&declaration), '(') != NULL && strcmp(current_file, input) == 0) { const char *str; int len; while (TRUE) { str = bufGet(&declaration); len = bufLen(&declaration); if (isspace(str[0])) bufTrim(&declaration, 1, 0); else break; } while (TRUE) { str = bufGet(&declaration); len = bufLen(&declaration); if (isspace(str[len - 1])) bufTrim(&declaration, 0, 1); else break; } if (include_static == TRUE || strncmp(str, "static", 6) != 0) { fputc('\n', out); if (include_comment && bufLen(&comment) > 0) { fputs(bufGet(&comment), out); fputc('\n', out); } fputs(str, out); if (str[len - 1] != ';') fputc(';', out); fputc('\n', out); } } bufClear(&comment); } } bufReset(&comment); bufReset(&declaration); free(current_file); return 0; }