1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-08 19:02:06 +03:00

buffers: adapt pcap.c to ssh_buffer_(un)pack()

Reviewed-by: Andreas Schneider <asn@samba.org>
This commit is contained in:
Aris Adamantiadis
2014-04-17 17:08:39 +02:00
committed by Andreas Schneider
parent 9457685320
commit a182926024

View File

@@ -342,7 +342,7 @@ static int ssh_pcap_context_connect(ssh_pcap_context ctx){
int ssh_pcap_context_write(ssh_pcap_context ctx,enum ssh_pcap_direction direction int ssh_pcap_context_write(ssh_pcap_context ctx,enum ssh_pcap_direction direction
, void *data, uint32_t len, uint32_t origlen){ , void *data, uint32_t len, uint32_t origlen){
ssh_buffer ip; ssh_buffer ip;
int err; int rc;
if(ctx==NULL || ctx->file ==NULL) if(ctx==NULL || ctx->file ==NULL)
return SSH_ERROR; return SSH_ERROR;
if(ctx->connected==0) if(ctx->connected==0)
@@ -353,147 +353,104 @@ int ssh_pcap_context_write(ssh_pcap_context ctx,enum ssh_pcap_direction directio
ssh_set_error_oom(ctx->session); ssh_set_error_oom(ctx->session);
return SSH_ERROR; return SSH_ERROR;
} }
/* build an IP packet */
/* V4, 20 bytes */ /* build an IP packet */
err = buffer_add_u8(ip,4 << 4 | 5); rc = ssh_buffer_pack(ip,
if (err < 0) { "bbwwwbbw",
goto error; 4 << 4 | 5, /* V4, 20 bytes */
} 0, /* tos */
/* tos */ origlen + TCPIPHDR_LEN, /* total len */
err = buffer_add_u8(ip,0); ctx->file->ipsequence, /* IP id number */
if (err < 0) { 0, /* fragment offset */
goto error; 64, /* TTL */
} 6, /* protocol TCP=6 */
/* total len */ 0); /* checksum */
err = buffer_add_u16(ip,htons(origlen + TCPIPHDR_LEN));
if (err < 0) {
goto error;
}
/* IP id number */
err = buffer_add_u16(ip,htons(ctx->file->ipsequence));
if (err < 0) {
goto error;
}
ctx->file->ipsequence++; ctx->file->ipsequence++;
/* fragment offset */ if (rc != SSH_OK){
err = buffer_add_u16(ip,htons(0)); goto error;
if (err < 0) { }
goto error;
}
/* TTL */
err = buffer_add_u8(ip,64);
if (err < 0) {
goto error;
}
/* protocol TCP=6 */
err = buffer_add_u8(ip,6);
if (err < 0) {
goto error;
}
/* checksum */
err = buffer_add_u16(ip,0);
if (err < 0) {
goto error;
}
if(direction==SSH_PCAP_DIR_OUT){ if(direction==SSH_PCAP_DIR_OUT){
err = buffer_add_u32(ip,ctx->ipsource); rc = buffer_add_u32(ip,ctx->ipsource);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
err = buffer_add_u32(ip,ctx->ipdest); rc = buffer_add_u32(ip,ctx->ipdest);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
} else { } else {
err = buffer_add_u32(ip,ctx->ipdest); rc = buffer_add_u32(ip,ctx->ipdest);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
err = buffer_add_u32(ip,ctx->ipsource); rc = buffer_add_u32(ip,ctx->ipsource);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
} }
/* TCP */ /* TCP */
if(direction==SSH_PCAP_DIR_OUT){ if(direction==SSH_PCAP_DIR_OUT){
err = buffer_add_u16(ip,ctx->portsource); rc = buffer_add_u16(ip,ctx->portsource);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
err = buffer_add_u16(ip,ctx->portdest); rc = buffer_add_u16(ip,ctx->portdest);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
} else { } else {
err = buffer_add_u16(ip,ctx->portdest); rc = buffer_add_u16(ip,ctx->portdest);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
err = buffer_add_u16(ip,ctx->portsource); rc = buffer_add_u16(ip,ctx->portsource);
if (err < 0) { if (rc < 0) {
goto error; goto error;
} }
} }
/* sequence number */ /* sequence number */
if(direction==SSH_PCAP_DIR_OUT){ if(direction==SSH_PCAP_DIR_OUT){
err = buffer_add_u32(ip,ntohl(ctx->outsequence)); rc = ssh_buffer_pack(ip, "d", ctx->outsequence);
if (err < 0) { if (rc != SSH_OK) {
goto error; goto error;
} }
ctx->outsequence+=origlen; ctx->outsequence+=origlen;
} else { } else {
err = buffer_add_u32(ip,ntohl(ctx->insequence)); rc = ssh_buffer_pack(ip, "d", ctx->insequence);
if (err < 0) { if (rc != SSH_OK) {
goto error; goto error;
} }
ctx->insequence+=origlen; ctx->insequence+=origlen;
} }
/* ack number */ /* ack number */
if(direction==SSH_PCAP_DIR_OUT){ if(direction==SSH_PCAP_DIR_OUT){
err = buffer_add_u32(ip,ntohl(ctx->insequence)); rc = ssh_buffer_pack(ip, "d", ctx->insequence);
if (err < 0) { if (rc != SSH_OK) {
goto error; goto error;
} }
} else { } else {
err = buffer_add_u32(ip,ntohl(ctx->outsequence)); rc = ssh_buffer_pack(ip, "d", ctx->outsequence);
if (err < 0) { if (rc != SSH_OK) {
goto error; goto error;
} }
} }
/* header len = 20 = 5 * 32 bits, at offset 4*/
err = buffer_add_u8(ip,5 << 4); rc = ssh_buffer_pack(ip,
if (err < 0) { "bbwwwP",
5 << 4, /* header len = 20 = 5 * 32 bits, at offset 4*/
TH_PUSH | TH_ACK, /* flags */
65535, /* window */
0, /* checksum */
0, /* urgent data ptr */
(size_t)len, data); /* actual data */
if (rc != SSH_OK) {
goto error; goto error;
} }
/* flags */ rc=ssh_pcap_file_write_packet(ctx->file,ip,origlen + TCPIPHDR_LEN);
err = buffer_add_u8(ip,TH_PUSH | TH_ACK);
if (err < 0) {
goto error;
}
/* window */
err = buffer_add_u16(ip,htons(65535));
if (err < 0) {
goto error;
}
/* checksum */
err = buffer_add_u16(ip,htons(0));
if (err < 0) {
goto error;
}
/* urgent data ptr */
err = buffer_add_u16(ip,0);
if (err < 0) {
goto error;
}
/* actual data */
err = ssh_buffer_add_data(ip, data, len);
if (err < 0) {
goto error;
}
err=ssh_pcap_file_write_packet(ctx->file,ip,origlen + TCPIPHDR_LEN);
error: error:
ssh_buffer_free(ip); ssh_buffer_free(ip);
return err; return rc;
} }
/** @brief sets the pcap file used to trace the session /** @brief sets the pcap file used to trace the session