void send_stream_format(int fd, const char *fmt, ...) { unsigned int len; va_list ap; char s[FORMAT_STRING_SIZE]; va_start(ap, fmt); len = vsnprintf(s, FORMAT_STRING_SIZE, fmt, ap); va_end(ap); if (len >= FORMAT_STRING_SIZE) { char *s2 = malloc(len + 1); va_start(ap, fmt); vsprintf(s2, fmt, ap); va_end(ap); send_stream(fd, s2, len); free(s2); } else send_stream(fd, s, len); flush_stream(fd); }
void PanoServerTCP::SendAllImages() { QTcpSocket* conn = latest_conn_; latest_conn_ = 0; const vector<ImageInfo>& img_infos = view_->getImageInfos(); for (vector<ImageInfo>::const_iterator ii = img_infos.begin(); ii != img_infos.end(); ++ii) { QByteArray img_data; QBuffer buffer(&img_data); buffer.open(QIODevice::WriteOnly); ii->image->save(&buffer, "PNG"); QByteArray to_send; QDataStream send_stream(&to_send, QIODevice::WriteOnly); send_stream.setByteOrder(QDataStream::LittleEndian); int preload_sz = sizeof(float) * 4 + sizeof(int); int total_sz = preload_sz + img_data.size(); send_stream << 0x13030002 << total_sz << ii->img_id << ii->scale << ii->rotation << ii->trans_x << ii->trans_y; to_send.append(img_data); Write(to_send, conn); } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udpstream_process, ev, data) { static uint16_t streamno; static struct etimer et; static uip_ipaddr_t *ipaddr; PROCESS_BEGIN(); PROCESS_PAUSE(); printf("Formatting Coffee FS...\n"); cfs_coffee_format(); printf("done.\n"); /* We need re-initialize queuebuf after formatting */ queuebuf_init(); /* Start service registration */ servreg_hack_init(); ipaddr = set_global_address(); if(node_id == SINK_ID) { /* The sink creates a dag and waits for UDP datagrams */ create_rpl_dag(ipaddr); servreg_hack_register(SERVICE_ID, ipaddr); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); while(1) { PROCESS_WAIT_EVENT(); } } else if(node_id == SENDER_ID) { /* The sender looks for the sink and sends UDP streams */ ipaddr = NULL; simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); etimer_set(&et, 10*CLOCK_SECOND); etimer_restart(&et); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); if(ipaddr != NULL) { streamno++; send_stream(ipaddr, streamno); } else { ipaddr = servreg_hack_lookup(SERVICE_ID); if(ipaddr != NULL) { etimer_set(&et, 2*CLOCK_SECOND); printf("Streaming to "); uip_debug_ipaddr_print(ipaddr); printf("\n"); } else { printf("Service %d not found\n", SERVICE_ID); } } etimer_restart(&et); } } PROCESS_END(); }
static TDHS_INLINE int write_data_to_response(easy_request_t *req, uint32_t data_length, const char* data, tdhs_client_wait_t &client_wait) { tdhs_packet_t &packet = *((tdhs_packet_t*) (req->ipacket)); easy_buf_t *b = packet.wbuff; tb_assert(b!=NULL); if ((uint32_t) (b->end - b->last) < sizeof(uint32_t)) { // make sure data_len can be write if (send_stream(client_wait, packet, req, b) != EASY_OK) { return EASY_ERROR; } } *((uint32_t*) (b->last)) = htonl(data_length); b->last += sizeof(uint32_t); packet.length += sizeof(uint32_t); if (data_length > 0) { uint32_t read_pos = 0; while (true) { uint32_t write_len = min((uint32_t) (b->end - b->last),(data_length - read_pos)); easy_debug_log("TDHS:send_stream write_len[%d] ", write_len); easy_debug_log("TDHS:buf info pos[%p] last[%p] end[%p]", b->pos, b->last, b->end); if (write_len > 0) { memcpy(b->last, data + read_pos, write_len); b->last += write_len; packet.length += write_len; read_pos += write_len; } if (read_pos < data_length) { easy_debug_log("TDHS:send_stream read_pos[%d] data_len[%d]", read_pos, data_length); if (send_stream(client_wait, packet, req, b) != EASY_OK) { return EASY_ERROR; } } else { easy_debug_log( "TDHS:send_stream_done read_pos[%d] data_len[%d]", read_pos, data_length); break; } } } return EASY_OK; }
static bool process_hunk(char const *program, struct notify_info *notify, struct memory_block_data const *payload, struct memory_block_signature const *signature) { struct signature_algorithm *sigalg = NULL; struct decompression_algorithm *decompalg = NULL; sigalg = create_sigalg(signature); if (!sigalg) goto err; decompalg = create_decompress(payload); if (!decompalg && payload->compression != STREAM_COMPRESS_NONE) goto err; if (!send_stream(program, notify, payload, sigalg, decompalg)) goto err; if (!signature_update(sigalg, payload->mem.stream->extra_salt, payload->mem.stream->extra_salt_len) || !signature_update(sigalg, signature->shdr->salt, sizeof signature->shdr->salt) || !signature_update(sigalg, signature->hhdr, sizeof *signature->hhdr)) goto err; if (signature->mem.len && !verify_signature(signature, sigalg)) goto err; if (!finish_stream(program, payload, sigalg)) goto err; signature_free(sigalg); return true; err: decompression_free(decompalg); signature_free(sigalg); return false; }
void send_stream_ascii(int fd, const char *msg) { send_stream(fd, msg, strlen(msg)); }
/* FTW callback for scanning in IDSESSION mode * Returns SUCCESS on success, CL_EXXX or BREAK on error */ static int parallel_callback(STATBUF *sb, char *filename, const char *path, enum cli_ftw_reason reason, struct cli_ftw_cbdata *data) { struct client_parallel_data *c = (struct client_parallel_data *)data->data; struct SCANID *cid; int res = CL_CLEAN; if(chkpath(path)) return CL_SUCCESS; c->files++; switch(reason) { case error_stat: logg("!Can't access file %s\n", path); c->errors++; return CL_SUCCESS; case error_mem: logg("!Memory allocation failed in ftw\n"); c->errors++; return CL_EMEM; case warning_skipped_dir: logg("^Directory recursion limit reached\n"); return CL_SUCCESS; case warning_skipped_special: logg("^%s: Not supported file type\n", path); c->errors++; case warning_skipped_link: case visit_directory_toplev: return CL_SUCCESS; case visit_file: break; } while(1) { /* consume all the available input to let some of the clamd * threads blocked on send() to be dead. * by doing so we shouldn't deadlock on the next recv() */ fd_set rfds, wfds; FD_ZERO(&rfds); FD_SET(c->sockd, &rfds); FD_ZERO(&wfds); FD_SET(c->sockd, &wfds); if(select(c->sockd + 1, &rfds, &wfds, NULL, NULL) < 0) { if(errno == EINTR) continue; free(filename); logg("!select() failed during session: %s\n", strerror(errno)); return CL_BREAK; } if(FD_ISSET(c->sockd, &rfds)) { if(dspresult(c)) { free(filename); return CL_BREAK; } else continue; } if(FD_ISSET(c->sockd, &wfds)) break; } cid = (struct SCANID *)malloc(sizeof(struct SCANID)); if(!cid) { free(filename); logg("!Failed to allocate scanid entry: %s\n", strerror(errno)); return CL_BREAK; } cid->id = ++c->lastid; cid->file = filename; cid->next = c->ids; c->ids = cid; switch(c->scantype) { #ifdef HAVE_FD_PASSING case FILDES: res = send_fdpass(c->sockd, filename); break; #endif case STREAM: res = send_stream(c->sockd, filename); break; } if(res <= 0) { c->printok = 0; c->errors++; c->ids = cid->next; c->lastid--; free(cid); free(filename); return res ? CL_BREAK : CL_SUCCESS; } return CL_SUCCESS; }
/* Sends a proper scan request to clamd and parses its replies * This is used only in non IDSESSION mode * Returns the number of infected files or -1 on error * NOTE: filename may be NULL for STREAM scantype. */ int dsresult(int sockd, int scantype, const char *filename, int *printok, int *errors) { int infected = 0, len = 0, beenthere = 0; char *bol, *eol; struct RCVLN rcv; STATBUF sb; if(filename && chkpath(filename)) return 0; recvlninit(&rcv, sockd); switch(scantype) { case MULTI: case CONT: case ALLMATCH: if (!filename) { logg("Filename cannot be NULL for MULTISCAN or CONTSCAN.\n"); return -1; } len = strlen(filename) + strlen(scancmd[scantype]) + 3; if (!(bol = malloc(len))) { logg("!Cannot allocate a command buffer: %s\n", strerror(errno)); return -1; } sprintf(bol, "z%s %s", scancmd[scantype], filename); if(sendln(sockd, bol, len)) { free(bol); return -1; } free(bol); break; case STREAM: /* NULL filename safe in send_stream() */ len = send_stream(sockd, filename); break; #ifdef HAVE_FD_PASSING case FILDES: /* NULL filename safe in send_fdpass() */ len = send_fdpass(sockd, filename); break; #endif } if(len <=0) { *printok = 0; if(errors) (*errors)++; return len; } while((len = recvln(&rcv, &bol, &eol))) { if(len == -1) return -1; beenthere = 1; if(!filename) logg("~%s\n", bol); if(len > 7) { char *colon = strrchr(bol, ':'); if(colon && colon[1] != ' ') { char *br; *colon = 0; br = strrchr(bol, '('); if(br) *br = 0; colon = strrchr(bol, ':'); } if(!colon) { char * unkco = "UNKNOWN COMMAND"; if (!strncmp(bol, unkco, sizeof(unkco) - 1)) logg("clamd replied \"UNKNOWN COMMAND\". Command was %s\n", (scantype < 0 || scantype > MAX_SCANTYPE) ? "unidentified" : scancmd[scantype]); else logg("Failed to parse reply: \"%s\"\n", bol); return -1; } else if(!memcmp(eol - 7, " FOUND", 6)) { *(eol - 7) = 0; *printok = 0; infected++; if(filename) { if(scantype >= STREAM) { logg("~%s%s FOUND\n", filename, colon); if(action) action(filename); } else { logg("~%s FOUND\n", bol); *colon = '\0'; if(action) action(bol); } } } else if(!memcmp(eol-7, " ERROR", 6)) { if(errors) (*errors)++; *printok = 0; if(filename) { if(scantype >= STREAM) logg("~%s%s\n", filename, colon); else logg("~%s\n", bol); } } } } if(!beenthere) { if (!filename) { logg("STDIN: noreply from clamd\n."); return -1; } if(CLAMSTAT(filename, &sb) == -1) { logg("~%s: stat() failed with %s, clamd may not be responding\n", filename, strerror(errno)); return -1; } if(!S_ISDIR(sb.st_mode)) { logg("~%s: no reply from clamd\n", filename); return -1; } } return infected; }
/* Sends a proper scan request to clamd and parses its replies * This is used only in non IDSESSION mode * Returns the number of infected files or -1 on error */ int dsresult(int sockd, int scantype, const char *filename, int *printok) { int infected = 0, len, beenthere = 0; char *bol, *eol; struct RCVLN rcv; recvlninit(&rcv, sockd); switch(scantype) { case MULTI: case CONT: len = strlen(filename) + strlen(scancmd[scantype]) + 3; if (!(bol = malloc(len))) { logg("!Cannot allocate a command buffer: %s\n", strerror(errno)); return -1; } sprintf(bol, "z%s %s", scancmd[scantype], filename); if(sendln(sockd, bol, len)) return -1; free(bol); break; case STREAM: len = send_stream(sockd, filename); break; #ifdef HAVE_FD_PASSING case FILDES: len = send_fdpass(sockd, filename); break; #endif } if(len <=0) { *printok = 0; return len; } while((len = recvln(&rcv, &bol, &eol))) { if(len == -1) return -1; beenthere = 1; if(!filename) logg("~%s\n", bol); if(len > 7) { char *colon = strrchr(bol, ':'); if(!colon) { logg("Failed to parse reply\n"); return -1; } else if(!memcmp(eol - 7, " FOUND", 6)) { *printok = 0; infected++; if(filename) { if(scantype >= STREAM) { logg("~%s%s\n", filename, colon); if(action) action(filename); } else { logg("~%s\n", bol); *colon = '\0'; if(action) action(bol); } } } else if(!memcmp(eol-7, " ERROR", 6)) { *printok = 0; if(filename) { if(scantype >= STREAM) logg("~%s%s\n", filename, colon); else logg("~%s\n", bol); } } } } if(!beenthere) { logg("~%s: no reply from clamd\n", filename ? filename : "STDIN"); return -1; } return infected; }
/* * Function obex_object_send() * * Send away all headers attached to an object. Returns: * 1 on sucessfully done * 0 on progress made * < 0 on error */ int obex_object_send(obex_t *self, obex_object_t *object, int allowfinalcmd, int forcefinalbit) { struct obex_header_element *h; buf_t *txmsg; int actual, finished = 0; uint16_t tx_left; int addmore = TRUE; int real_opcode; DEBUG(4, "\n"); /* Don't do anything of object is suspended */ if (object->suspend) return 0; /* Calc how many bytes of headers we can fit in this package */ tx_left = self->mtu_tx - sizeof(struct obex_common_hdr); switch (self->trans.type) { #ifdef HAVE_IRDA case OBEX_TRANS_IRDA: if (self->trans.mtu > 0 && self->mtu_tx > self->trans.mtu) tx_left -= self->mtu_tx%self->trans.mtu; break; #endif /*HAVE_IRDA*/ default: break; } /* Reuse transmit buffer */ txmsg = buf_reuse(self->tx_msg); /* Add nonheader-data first if any (SETPATH, CONNECT)*/ if (object->tx_nonhdr_data) { DEBUG(4, "Adding %d bytes of non-headerdata\n", object->tx_nonhdr_data->data_size); buf_insert_end(txmsg, object->tx_nonhdr_data->data, object->tx_nonhdr_data->data_size); buf_free(object->tx_nonhdr_data); object->tx_nonhdr_data = NULL; } DEBUG(4, "4\n"); /* Take headers from the tx queue and try to stuff as many as possible into the tx-msg */ while (addmore == TRUE && object->tx_headerq != NULL) { h = object->tx_headerq->data; if (h->stream) { /* This is a streaming body */ if (h->flags & OBEX_FL_SUSPEND) object->suspend = 1; actual = send_stream(self, h, txmsg, tx_left); if (actual < 0 ) return -1; tx_left -= actual; } else if (h->hi == OBEX_HDR_BODY) { /* The body may be fragmented over several packets. */ if (h->flags & OBEX_FL_SUSPEND) object->suspend = 1; tx_left -= send_body(object, h, txmsg, tx_left); } else if(h->hi == OBEX_HDR_EMPTY) { if (h->flags & OBEX_FL_SUSPEND) object->suspend = 1; object->tx_headerq = slist_remove(object->tx_headerq, h); free(h); } else if (h->length <= tx_left) { /* There is room for more data in tx msg */ DEBUG(4, "Adding non-body header\n"); buf_insert_end(txmsg, h->buf->data, h->length); tx_left -= h->length; if (h->flags & OBEX_FL_SUSPEND) object->suspend = 1; /* Remove from tx-queue */ object->tx_headerq = slist_remove(object->tx_headerq, h); buf_free(h->buf); free(h); } else if (h->length > self->mtu_tx) { /* Header is bigger than MTU. This should not happen, because OBEX_ObjectAddHeader() rejects headers bigger than the MTU */ DEBUG(0, "ERROR! header to big for MTU\n"); return -1; } else { /* This header won't fit. */ addmore = FALSE; } if (object->suspend) addmore = FALSE; if (tx_left == 0) addmore = FALSE; }; /* Decide which command to use, and if to use final-bit */ if (object->tx_headerq) { /* Have more headers (or body) to send */ /* In server, final bit is always set. * In client, final bit is set only when we finish sending. * Jean II */ if (forcefinalbit) real_opcode = object->opcode | OBEX_FINAL; else real_opcode = object->opcode; finished = 0; } else if (allowfinalcmd == FALSE) { /* Have no yet any headers to send, but not allowed to send * final command (== server, receiving incomming request) */ real_opcode = object->opcode | OBEX_FINAL; finished = 0; } else { /* Have no more headers to send, and allowed to send final * command (== end data we are sending) */ real_opcode = object->lastopcode | OBEX_FINAL; finished = 1; } DEBUG(4, "Sending package with opcode %d\n", real_opcode); actual = obex_data_request(self, txmsg, real_opcode); if (actual < 0) { DEBUG(4, "Send error\n"); return actual; } else return finished; }
void PanoServerTCP::IncomingData() { QTcpSocket* conn = (QTcpSocket*) sender(); vector<TcpConnection>::iterator host = std::find(connections_.begin(), connections_.end(), TcpConnection(conn)); if (host == connections_.end()) { log("Client to registered at TCP server."); return; } if (conn->bytesAvailable() <= 0) { log("Error on incoming data. Packet is empty!"); return; } QByteArray data = conn->readAll(); int data_pos = 0; // Multiple packages possible. while (data_pos < data.length()) { // Continue package? if (host->pkg.length()) { int remaining_data_bytes = data.length() - data_pos; int remaining_bytes = host->target_sz - host->pkg.length(); if (remaining_data_bytes <= remaining_bytes) { host->pkg.append(data.mid(data_pos, remaining_data_bytes)); data_pos += remaining_data_bytes; } else { // Another package is waiting. host->pkg.append(data.mid(data_pos, remaining_bytes)); data_pos += remaining_bytes; } } else { // New package. QDataStream data_stream(data.mid(data_pos)); data_stream.setByteOrder(QDataStream::LittleEndian); int msg_id; int length; data_stream >> msg_id; data_stream >> length; data_pos += sizeof(int) * 2; int remaining_data_bytes = data.length() - data_pos; switch (msg_id) { case 0x13030002: { log("New image uploaded"); // Most likely more packages are incoming ... host->target_sz = length; host->msg_id = msg_id; if (length > remaining_data_bytes) { host->pkg.append(data.mid(data_pos, remaining_data_bytes)); data_pos += remaining_data_bytes; } else { host->pkg.append(data.mid(data_pos, length)); data_pos += length; } break; } case 0x13030005: { // Image lock request. QDataStream img_id_stream(data.mid(data_pos)); img_id_stream.setByteOrder(QDataStream::LittleEndian); int img_id; img_id_stream >> img_id; data_pos += sizeof(int); if (view_->aquireLock(img_id, conn)) { // Notify all clients. SendLockToClients(img_id); } else { log("Failed lock request from client. Lock already aquired."); } break; } case 0x13030006: { // Image release request. QDataStream img_id_stream(data.mid(data_pos)); img_id_stream.setByteOrder(QDataStream::LittleEndian); int img_id; img_id_stream >> img_id; data_pos += sizeof(int); if (view_->releaseLock(img_id, conn)) { // Notify all clients. SendReleaseToClients(img_id); } else { log("Failed release request. Inconsistency error occured.\n"); } break; } default: log("Unknown package incoming!"); break; } } // Read completely? if (host->pkg.length() == host->target_sz) { switch(host->msg_id) { case 0x13030002: { // Get initial transformation. QDataStream pkg_stream(host->pkg); pkg_stream.setByteOrder(QDataStream::LittleEndian); float scale, rotation, trans_x, trans_y; pkg_stream >> scale >> rotation >> trans_x >> trans_y; QByteArray img_data = host->pkg.mid(sizeof(float) * 4); QImage img = QImage::fromData(img_data, "png"); int img_id = view_->AddImage(img); log(QString("Assigned id ") + QString::number(img_id) + " to image. Sending ACK."); view_->updateImgPosition(img_id, scale, rotation, trans_x, trans_y); // Send img_id back. QByteArray img_id_package; int img_id_sz = sizeof(int); QDataStream img_id_stream(&img_id_package, QIODevice::WriteOnly); img_id_stream.setByteOrder(QDataStream::LittleEndian); img_id_stream << 0x13030003 << img_id_sz << img_id; Write(img_id_package, conn, 0); // Send to remaining cients. QByteArray to_send; QDataStream send_stream(&to_send, QIODevice::WriteOnly); send_stream.setByteOrder(QDataStream::LittleEndian); int preload_sz = sizeof(float) * 4 + sizeof(int); int total_sz = preload_sz + img_data.size(); send_stream << host->msg_id << total_sz << img_id << scale << rotation << trans_x << trans_y; to_send.append(img_data); Write(to_send, 0, conn); break; } default: break; } host->pkg.clear(); } } }
void do_menu_command( long menuResult) { short menuID; short menuItem; menuID= GET_MENU_ID(menuResult); menuItem= GET_MENU_ITEM(menuResult); switch (menuID) { case mApple: switch (menuItem) { case iAbout: break; default: #if defined(OP_PLATFORM_MAC_CFM) && !defined(OP_PLATFORM_MAC_CARBON_FLAG) { Str255 daName; GetMenuItemText(GetMenuHandle(mApple), menuItem, daName); OpenDeskAcc(daName); } #endif break; } break; case mFile: switch (menuItem) { case iOpen: case iOpenPassive: handle_open(menuItem==iOpen, false); break; case iOpenActiveUI: case iOpenPassiveUI: handle_open(menuItem==iOpenActiveUI, true); break; case iClose: close_front_window(); break; case iQuit: handle_quit(); break; } break; case mSpecial: switch(menuItem) { case iSendPacket: send_packet(); break; case iSendStream: send_stream(); break; case iProtocolAlive: is_alive(); break; case iAcceptingConnections: accepting_connections= !accepting_connections; check_menu_item(menuID, menuItem, accepting_connections); break; case iActiveEnumeration: active_enumeration= !active_enumeration; check_menu_item(menuID, menuItem, active_enumeration); break; case iSetTimeout: set_timeout(); break; case iGetInfo: report_info(); break; case iGetConfigString: // get_config_string(); break; } break; case mWindowMenu: switch(menuItem) { case iCascade: case iTile: cascade_or_tile_windows(menuItem==iCascade); break; } } #if OP_PLATFORM_MAC_CFM || OP_PLATFORM_MAC_MACHO HiliteMenu(0); #endif adjust_menus(); return; }