int packet_fxp_init(Buffer *buff, Buffer *preped_buff) { u_char *cp; u_int msg_len; u_int xmsg_len; u_int msg_ver; cp = buffer_ptr(buff); // 1. Ensure extensions are not forwarded. Drop packet size down to 5 bytes + 4 byte length. Later byte are ignored through buffer_consume. msg_len = get_u32(cp); xmsg_len = 5; put_u32(cp, xmsg_len); // 2. Ensure client version number presented is not greater than that supported. cp += 5; msg_ver = get_u32(cp); if (msg_ver > 3) msg_ver = 3; put_u32(cp, msg_ver); // Copy packet over to prepared buffer buffer_append(preped_buff, buffer_ptr(buff), xmsg_len + sizeof(u_int)); buffer_consume(buff, msg_len + sizeof(u_int)); return 1; }
static void isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *mlp) { isdn_net_dev *idev; struct sk_buff *skb; unsigned char *p; if (list_empty(&mlp->online)) { isdn_BUG(); return; } idev = list_entry(mlp->online.next, isdn_net_dev, online); skb = isdn_net_ciscohdlck_alloc_skb(idev, 4 + 14); if (!skb) return; p = skb_put(skb, 4 + 14); /* cisco header */ p += put_u8 (p, CISCO_ADDR_UNICAST); p += put_u8 (p, CISCO_CTRL); p += put_u16(p, CISCO_TYPE_SLARP); /* slarp request */ p += put_u32(p, CISCO_SLARP_REQUEST); p += put_u32(p, 0); // address p += put_u32(p, 0); // netmask p += put_u16(p, 0); // unused isdn_net_write_super(idev, skb); }
static byte * mrt_put_bgp4_hdr(byte *buf, struct bgp_conn *conn, int as4) { struct bgp_proto *p = conn->bgp; if (as4) { put_u32(buf+0, p->remote_as); put_u32(buf+4, p->local_as); buf+=8; } else { put_u16(buf+0, (p->remote_as <= 0xFFFF) ? p->remote_as : AS_TRANS); put_u16(buf+2, (p->local_as <= 0xFFFF) ? p->local_as : AS_TRANS); buf+=4; } put_u16(buf+0, (p->neigh && p->neigh->iface) ? p->neigh->iface->index : 0); put_u16(buf+2, BGP_AF); buf+=4; buf = put_ipa(buf, conn->sk ? conn->sk->daddr : IPA_NONE); buf = put_ipa(buf, conn->sk ? conn->sk->saddr : IPA_NONE); return buf; }
Sophon_Result sophon_insfile_store (Sophon_VM *vm, Sophon_Module *mod, Sophon_IOFunc func, Sophon_Ptr data) { Sophon_U16 i; SOPHON_ASSERT(vm && mod && func); put_u32(func, data, SOPHON_INSFILE_MAGIC); put_u32(func, data, SOPHON_INSFILE_VERSION); if (mod->name) put_str(vm, func, data, mod->name); else put_u32(func, data, 0); put_u16(func, data, mod->const_count); for (i = 0; i < mod->const_count; i++) { put_value(vm, func, data, mod->consts[i]); } put_u16(func, data, mod->func_count); for (i = 0; i < mod->func_count; i++) { put_func(vm, func, data, mod->funcs[i]); } return SOPHON_OK; }
int send_cmd_turbo(libusb_device_handle* fd, int turbo_on) { struct tf_packet req; trace(3, printf("send_cmd_turbo\n")); put_u16(&req.length, 12); put_u32(&req.cmd, CMD_TURBO); put_u32(&req.data, turbo_on); return send_tf_packet(fd, &req); }
ssize_t send_cmd_turbo(int fd, int turbo_on) { struct tf_packet req; trace(2, fprintf(stderr, "%s\n", __func__)); put_u16(&req.length, 12); put_u32(&req.cmd, CMD_TURBO); put_u32(&req.data, turbo_on); return send_tf_packet(fd, &req); }
int packet_fxp_fsetstat(Buffer *buff, Buffer *preped_buff) { u_int msg_len; u_int xmsg_len; // File names u_int file_len; u_char *handle; // Copy first part of packet over to prepared buffer msg_len = get_u32(buffer_ptr(buff)); xmsg_len = msg_len; buffer_append(preped_buff, buffer_ptr(buff), 9); buffer_consume(buff, 9); xmsg_len -= 5; // Copy handle handle = buffer_get_string(buff, &file_len); buffer_put_string(preped_buff, (char*) handle, file_len); xmsg_len -= (file_len + 4); // Copy attributes through, cleaning extensions where required parse_attrs(buff, preped_buff, &msg_len, &xmsg_len); // Copy any remaining packet data over buffer_append(preped_buff, buffer_ptr(buff), xmsg_len); buffer_consume(buff, xmsg_len); // Rewrite message length put_u32(buffer_end(preped_buff)-msg_len-4, msg_len); return 1; }
int packet_fxp_mkdir(Buffer *buff, Buffer *preped_buff) { u_int msg_len; u_int xmsg_len; // File names u_int file_len; u_char *filename; // Copy first part of packet over to prepared buffer msg_len = get_u32(buffer_ptr(buff)); xmsg_len = msg_len; buffer_append(preped_buff, buffer_ptr(buff), 9); buffer_consume(buff, 9); xmsg_len -= 5; // Rewrite path filename = buffer_get_string(buff, &file_len); filename = unchroot_filename(filename, (u_char*) user_homedir); buffer_put_cstring(preped_buff, (char*) filename); xmsg_len -= (file_len + 4); msg_len += (strlen((char*) filename) - file_len); // Copy attributes through, cleaning extensions where required parse_attrs(buff, preped_buff, &msg_len, &xmsg_len); // Copy any remaining packet data over buffer_append(preped_buff, buffer_ptr(buff), xmsg_len); buffer_consume(buff, xmsg_len); // Rewrite message length put_u32(buffer_end(preped_buff)-msg_len-4, msg_len); return 1; }
int packet_fxp_realpath(Buffer *buff, Buffer *preped_buff) { u_int msg_len, xmsg_len; u_int file_len; u_char *filename; // Copy first part of packet over to prepared buffer msg_len = get_u32(buffer_ptr(buff)); xmsg_len = msg_len; buffer_append(preped_buff, buffer_ptr(buff), 9); buffer_consume(buff, 9); xmsg_len -= 5; // Rewrite path filename = buffer_get_string(buff, &file_len); filename = unchroot_filename(filename, (u_char*) user_homedir); buffer_put_cstring(preped_buff, (char*) filename); xmsg_len -= (file_len + 4); msg_len += (strlen((char*) filename) - file_len); // Copy packet over to prepared buffer buffer_append(preped_buff, buffer_ptr(buff), xmsg_len); buffer_consume(buff, xmsg_len); // Replace length with new length put_u32(buffer_end(preped_buff)-msg_len-4, msg_len); return 1; }
void buffer_put_int(Buffer *buffer, unsigned int value) { char buf[4]; put_u32(buf, value); buffer_append(buffer, buf, 4); }
int send_cmd_hdd_file_send_with_offset(libusb_device_handle* fd, __u8 dir, char *path, __u64 offset) { struct tf_packet req; __u16 packetSize, pad; int pathLen = strlen(path) + 1; trace(3, printf("send_cmd_hdd_file_send_with_offset: %s [%ld]\n",path,offset)); if((PACKET_HEAD_SIZE + 1 + 2 + pathLen) >= MAXIMUM_PACKET_SIZE) { fprintf(stdout, "ERROR: Path is too long.\n"); return -1; } packetSize = PACKET_HEAD_SIZE + 1 + 2 + pathLen +8; pad = ((packetSize + 1) & ~1 ) - packetSize; //printf("Packet padding: %d\n",pad); pad = 0;//((packetSize + 1) & ~1 ) - packetSize; //printf("Packet padding: %d\n",pad); packetSize +=pad; put_u16(&req.length, packetSize); put_u32(&req.cmd, CMD_HDD_FILE_SEND); req.data[0] = dir; put_u16(&req.data[1], (__u16) pathLen); strcpy((char *) &req.data[3], path); put_u64( &req.data[3+pathLen+pad],offset); return send_tf_packet(fd, &req); }
int send_cmd_hdd_rename(libusb_device_handle* fd, char *src, char *dst) { struct tf_packet req; __u16 packetSize; __u16 srcLen = strlen(src) + 1; __u16 dstLen = strlen(dst) + 1; trace(3, printf("send_cmd_hdd_rename: %s : %s\n",src,dst)); if((PACKET_HEAD_SIZE + 2 + srcLen + 2 + dstLen) >= MAXIMUM_PACKET_SIZE) { fprintf(stdout, "ERROR: Combination of source and destination paths is too long.\n"); return -1; } packetSize = PACKET_HEAD_SIZE + 2 + srcLen + 2 + dstLen; packetSize = (packetSize + 1) & ~1; put_u16(&req.length, packetSize); put_u32(&req.cmd, CMD_HDD_RENAME); put_u16(&req.data[0], srcLen); strcpy((char *) &req.data[2], src); put_u16(&req.data[2 + srcLen], dstLen); strcpy((char *) &req.data[2 + srcLen + 2], dst); return send_tf_packet(fd, &req); }
void mm_log_handler(LogLevel level, const char *msg, void *ctx) { Buffer log_msg; struct monitor *mon = (struct monitor *)ctx; if (mon->m_log_sendfd == -1) fatal("%s: no log channel", __func__); buffer_init(&log_msg); /* * Placeholder for packet length. Will be filled in with the actual * packet length once the packet has been constucted. This saves * fragile math. */ buffer_put_int(&log_msg, 0); buffer_put_int(&log_msg, level); buffer_put_cstring(&log_msg, msg); put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4); if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg), buffer_len(&log_msg)) != buffer_len(&log_msg)) fatal("%s: write: %s", __func__, strerror(errno)); buffer_free(&log_msg); }
u_char * mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) { static u_char m[EVP_MAX_MD_SIZE]; u_char b[4]; if (mac->mac_len > sizeof(m)) fatal("mac_compute: mac too long %u %lu", mac->mac_len, (u_long)sizeof(m)); switch (mac->type) { case SSH_EVP: put_u32(b, seqno); /* reset HMAC context */ HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); HMAC_Update(&mac->evp_ctx, b, sizeof(b)); HMAC_Update(&mac->evp_ctx, data, datalen); HMAC_Final(&mac->evp_ctx, m, NULL); break; default: fatal("mac_compute: unknown MAC type"); } return (m); }
/* Converts 'learn' into a "struct nx_action_learn" and appends that action to * 'ofpacts'. */ void learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow) { const struct ofpact_learn_spec *spec; struct nx_action_learn *nal; size_t start_ofs; start_ofs = openflow->size; nal = ofputil_put_NXAST_LEARN(openflow); nal->idle_timeout = htons(learn->idle_timeout); nal->hard_timeout = htons(learn->hard_timeout); nal->fin_idle_timeout = htons(learn->fin_idle_timeout); nal->fin_hard_timeout = htons(learn->fin_hard_timeout); nal->priority = htons(learn->priority); nal->cookie = htonll(learn->cookie); nal->flags = htons(learn->flags); nal->table_id = learn->table_id; for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) { put_u16(openflow, spec->n_bits | spec->dst_type | spec->src_type); if (spec->src_type == NX_LEARN_SRC_FIELD) { put_u32(openflow, spec->src.field->nxm_header); put_u16(openflow, spec->src.ofs); } else { size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16); uint8_t *bits = ofpbuf_put_zeros(openflow, n_dst_bytes); bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0, bits, n_dst_bytes, 0, spec->n_bits); } if (spec->dst_type == NX_LEARN_DST_MATCH || spec->dst_type == NX_LEARN_DST_LOAD) { put_u32(openflow, spec->dst.field->nxm_header); put_u16(openflow, spec->dst.ofs); } } if ((openflow->size - start_ofs) % 8) { ofpbuf_put_zeros(openflow, 8 - (openflow->size - start_ofs) % 8); } nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal); nal->len = htons(openflow->size - start_ofs); }
int send_cmd_hdd_size(libusb_device_handle* fd) { struct tf_packet req; trace(3, printf("send_cmd_hdd_size\n")); put_u16(&req.length, 8); put_u32(&req.cmd, CMD_HDD_SIZE); return send_tf_packet(fd, &req); }
int send_cmd_reset(libusb_device_handle* fd) { struct tf_packet req; trace(3, printf("send_cmd_reset\n")); put_u16(&req.length, 8); put_u32(&req.cmd, CMD_RESET); return send_tf_packet(fd, &req); }
ssize_t send_cmd_hdd_size(int fd) { struct tf_packet req; trace(2, fprintf(stderr, "%s\n", __func__)); put_u16(&req.length, 8); put_u32(&req.cmd, CMD_HDD_SIZE); return send_tf_packet(fd, &req); }
static int handle_to_string(int handle, u_char **stringp, int *hlenp) { if (stringp == NULL || hlenp == NULL) return -1; *stringp = xmalloc(sizeof(int32_t)); put_u32(*stringp, handle); *hlenp = sizeof(int32_t); return 0; }
bool wav_dump( const char* filename, int bytespersample, int channels, int freq, void const* data, int bytecnt) { // room for file header, fmt chunk and data chunk header uint8_t buf[(8+4)+(8+18) + 8]; uint8_t* p=buf; p=put_4CC(p,"RIFF"); p=put_u32(p,4 + (8+18) + (8+bytecnt)); p=put_4CC(p,"WAVE"); // fmt chunk p=put_4CC(p,"fmt "); p=put_u32(p,18); // chunksize p=put_u16(p,0x0001); // wFormatTag (WAVE_FORMAT_PCM) p=put_u16(p,channels); // nChannels p=put_u32(p,freq); // nSamplesPerSec p=put_u32(p,freq*channels*bytespersample); // nAvgBytesPerSec p=put_u16(p,bytespersample*channels); // nBlockAlign p=put_u16(p,bytespersample*8); // wBitsPerSample p=put_u16(p,0); // cbSize // data chunk (header only) p=put_4CC(p,"data"); p=put_u32(p,bytecnt); FILE* fp = fopen(filename,"wb"); if (!fp) return false; if (fwrite(buf,sizeof(buf),1,fp) != 1) { fclose(fp); return false; } if (fwrite(data,bytecnt,1,fp) != 1) { fclose(fp); return false; } fclose(fp); return true; }
u8 *translateTree(AST *tree) { FILE *fp = NULL; u16 i, j, k; u8 *bin = NULL; u32 binlen; fp = tmpfile(); if (fp == NULL) return NULL; put_u32(0, fp); put_u32(0, fp); for (i = 0; i < tree->ngames; i++) { putFireStr(toFireStr(tree->games[i].s), fp); put_u16(tree->games[i].ncheats, fp); for (j = 0; j < tree->games[i].ncheats; j++) { putFireStr(toFireStr(tree->games[i].cheats[j].s), fp); put_u16(tree->games[i].cheats[j].ncodes, fp); for (k = 0; k < tree->games[i].cheats[j].ncodes; k++) { put_u32(tree->games[i].cheats[j].codes[2 * k], fp); put_u32(tree->games[i].cheats[j].codes[(2 * k) + 1], fp); } } } put_u32(BIN_MKR_EOF, fp); binlen = ftell(fp); bin = (u8*) readFromFile(fp, 0, binlen); if (bin == NULL) return NULL; fclose(fp); // Delete temporary file *(u32*) &bin[0] = binlen - 8; *(u32*) &bin[4] = get_crc32(bin + 8, binlen - 8); return bin; }
static void send_msg(Buffer *m) { u_char buf[4]; int mlen = buffer_len(m); put_u32(buf, mlen); if (atomicio(vwrite, fd, buf, 4) != 4 || atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m)) error("write to helper failed"); buffer_consume(m, mlen); }
static void isdn_net_ciscohdlck_slarp_send_reply(isdn_net_dev *idev) { isdn_net_local *mlp = idev->mlp; struct sk_buff *skb; unsigned char *p; struct in_device *in_dev = NULL; u32 addr = 0; /* local ipv4 address */ u32 mask = 0; /* local netmask */ if ((in_dev = mlp->dev.ip_ptr) != NULL) { /* take primary(first) address of interface */ struct in_ifaddr *ifa = in_dev->ifa_list; if (ifa != NULL) { addr = ifa->ifa_local; mask = ifa->ifa_mask; } } skb = isdn_net_ciscohdlck_alloc_skb(idev, 4 + 14); if (!skb) return; p = skb_put(skb, 4 + 14); /* cisco header */ p += put_u8 (p, CISCO_ADDR_UNICAST); p += put_u8 (p, CISCO_CTRL); p += put_u16(p, CISCO_TYPE_SLARP); /* slarp reply, send own ip/netmask; if values are nonsense remote * should think we are unable to provide it with an address via SLARP */ p += put_u32(p, CISCO_SLARP_REPLY); p += put_u32(p, addr); // address p += put_u32(p, mask); // netmask p += put_u16(p, 0); // unused isdn_net_write_super(idev, skb); }
void mm_request_send(int sock, enum monitor_reqtype type, Buffer *m) { u_int mlen = buffer_len(m); u_char buf[5]; debug3("%s entering: type %d", __func__, type); put_u32(buf, mlen + 1); buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) fatal("%s: write: %s", __func__, strerror(errno)); if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) fatal("%s: write: %s", __func__, strerror(errno)); }
static void put_str (Sophon_VM *vm, Sophon_IOFunc func, Sophon_Ptr data, Sophon_String *str) { Sophon_Char *cstr; Sophon_U32 len; Sophon_U16 c; cstr = sophon_string_chars(vm, str); len = sophon_string_length(vm, str); put_u32(func, data, len); while (len--) { c = *cstr++; put_u16(func, data, c); } }
static int ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply) { u_int l, len; char buf[1024]; /* Get the length of the message, and format it in the buffer. */ len = buffer_len(request); put_u32(buf, len); /* Send the length and then the packet to the agent. */ if (atomicio(vwrite, auth->fd, buf, 4) != 4 || atomicio(vwrite, auth->fd, buffer_ptr(request), buffer_len(request)) != buffer_len(request)) { error("Error writing to authentication socket."); return 0; } /* * Wait for response from the agent. First read the length of the * response packet. */ if (atomicio(read, auth->fd, buf, 4) != 4) { error("Error reading response length from authentication socket."); return 0; } /* Extract the length, and check it for sanity. */ len = get_u32(buf); if (len > 256 * 1024) fatal("Authentication response too long: %u", len); /* Read the rest of the response in to the buffer. */ buffer_clear(reply); while (len > 0) { l = len; if (l > sizeof(buf)) l = sizeof(buf); if (atomicio(read, auth->fd, buf, l) != l) { error("Error reading response from authentication socket."); return 0; } buffer_append(reply, buf, l); len -= l; } return 1; }
int send_cmd_hdd_del(libusb_device_handle* fd, char *path) { struct tf_packet req; __u16 packetSize; int pathLen = strlen(path) + 1; trace(3, printf("send_cmd_hdd_del\n")); if((PACKET_HEAD_SIZE + pathLen) >= MAXIMUM_PACKET_SIZE) { fprintf(stdout, "ERROR: Path is too long.\n"); return -1; } packetSize = PACKET_HEAD_SIZE + pathLen; packetSize = (packetSize + 1) & ~1; put_u16(&req.length, packetSize); put_u32(&req.cmd, CMD_HDD_DEL); strcpy((char *) req.data, path); return send_tf_packet(fd, &req); }
/* Communicate with agent: send request and read reply */ static int ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply) { int r; size_t l, len; char buf[1024]; /* Get the length of the message, and format it in the buffer. */ len = sshbuf_len(request); put_u32(buf, len); /* Send the length and then the packet to the agent. */ if (atomicio(vwrite, sock, buf, 4) != 4 || atomicio(vwrite, sock, (u_char *)sshbuf_ptr(request), sshbuf_len(request)) != sshbuf_len(request)) return SSH_ERR_AGENT_COMMUNICATION; /* * Wait for response from the agent. First read the length of the * response packet. */ if (atomicio(read, sock, buf, 4) != 4) return SSH_ERR_AGENT_COMMUNICATION; /* Extract the length, and check it for sanity. */ len = get_u32(buf); if (len > MAX_AGENT_REPLY_LEN) return SSH_ERR_INVALID_FORMAT; /* Read the rest of the response in to the buffer. */ sshbuf_reset(reply); while (len > 0) { l = len; if (l > sizeof(buf)) l = sizeof(buf); if (atomicio(read, sock, buf, l) != l) return SSH_ERR_AGENT_COMMUNICATION; if ((r = sshbuf_put(reply, buf, l)) != 0) return r; len -= l; } return 0; }
u_char * mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) { static union { u_char m[EVP_MAX_MD_SIZE]; u_int64_t for_align; } u; u_char b[4]; #ifdef UMAC_HAS_BEEN_UNBROKEN u_char nonce[8]; #endif if (mac->mac_len > sizeof(u)) fatal("mac_compute: mac too long %u %lu", mac->mac_len, (u_long)sizeof(u)); switch (mac->type) { case SSH_EVP: put_u32(b, seqno); /* reset HMAC context */ HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); HMAC_Update(&mac->evp_ctx, b, sizeof(b)); HMAC_Update(&mac->evp_ctx, data, datalen); HMAC_Final(&mac->evp_ctx, u.m, NULL); break; #ifdef UMAC_HAS_BEEN_UNBROKEN case SSH_UMAC: put_u64(nonce, seqno); umac_update(mac->umac_ctx, data, datalen); umac_final(mac->umac_ctx, u.m, nonce); break; case SSH_UMAC128: put_u64(nonce, seqno); umac128_update(mac->umac_ctx, data, datalen); umac128_final(mac->umac_ctx, u.m, nonce); break; #endif default: fatal("mac_compute: unknown MAC type"); } return (u.m); }
int ssh_msg_send(int fd, u_char type, struct sshbuf *m) { u_char buf[5]; u_int mlen = sshbuf_len(m); debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff); put_u32(buf, mlen + 1); buf[4] = type; /* 1st byte of payload is mesg-type */ if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) { error("ssh_msg_send: write"); return (-1); } if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) { error("ssh_msg_send: write"); return (-1); } return (0); }