Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update content length from u16 to u32 #17

Merged
merged 5 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/doc-ver1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The FILE packet contains the contents of file. And each block was contained in o
| Length | 4 | The size of this packet. |
| File Id | 4 | File Id. As same as the one in ACK. |
| Block Seq | 2 | Block sequence number. Start from 0. |
| Content length | 2 | The length of content. |
| Content length | 4 | The length of content. |
| Content | Content length | The content of block. |

+ Actions
Expand All @@ -83,7 +83,7 @@ The END Packet is the last packet contain contents of file. It means the ending
| Length | 4 | The size of this packet. |
| File Id | 4 | File Id. As same as the one in ACK. |
| Block Seq | 2 | Block sequence number. Start from 0. |
| Content length | 2 | The length of content. |
| Content length | 4 | The length of content. |
| Content | Content length | The content of block. |

### GIVEME Packet
Expand Down
8 changes: 4 additions & 4 deletions src/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nftp_alloc(nftp ** pp)
p->hashcode = 0;
p->content = 0;
p->ctlen = 0;
if ((p->exbuf = malloc(sizeof(char) * 12)) == NULL) {
if ((p->exbuf = malloc(sizeof(char) * 14)) == NULL) {
return (NFTP_ERR_MEM);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ nftp_decode(nftp *p, uint8_t *v, size_t len)

nftp_get_u16(v + pos, p->blockseq); pos += 2;

nftp_get_u16(v + pos, p->ctlen); pos += 2;
nftp_get_u32(v + pos, p->ctlen); pos += 4;

if ((p->content = malloc(sizeof(char) * p->ctlen)) == NULL)
return (NFTP_ERR_MEM);
Expand Down Expand Up @@ -168,8 +168,8 @@ nftp_encode_iovs(nftp * p, nftp_iovs * iovs)
if (0 != nftp_iovs_append(iovs, (void *)(p->exbuf + 8), 2))
goto error;

nftp_put_u16(p->exbuf + 10, p->ctlen);
if (0 != nftp_iovs_append(iovs, (void *)(p->exbuf + 10), 2))
nftp_put_u32(p->exbuf + 10, p->ctlen);
if (0 != nftp_iovs_append(iovs, (void *)(p->exbuf + 10), 4))
goto error;

if (0 != nftp_iovs_append(iovs, (void *)p->content, p->ctlen))
Expand Down
11 changes: 10 additions & 1 deletion src/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ nftp_proto_maker(char *fpath, int type, int key, int n, char **rmsg, int *rlen)
// Note. No type check.
p->type = type;

p->len = 5 + 4 + 2 + 2 + len;
p->len = 5 + 4 + 2 + 4 + len;
p->fileid = NFTP_HASH((const uint8_t *)fname, (size_t)strlen(fname));
p->blockseq = n;

Expand Down Expand Up @@ -425,6 +425,13 @@ nftp_proto_handler(char *msg, int len, char **rmsg, int *rlen)
strlen(n->fname));
ctx->hashcode = n->hashcode;

if (ht_contains(&files, &n->fileid)) {
nftp_fatal("File with same fileid is processing [%d][%s]", n->fileid, n->fname);
nctx_free(ctx);
nftp_free(n);
return NFTP_ERR_HT;
}

iter = nftp_iter_alloc(NFTP_SCHEMA_VEC, fcb_reg);
nftp_iter_next(iter);
while (iter->key != NFTP_TAIL) {
Expand Down Expand Up @@ -713,6 +720,8 @@ nftp_proto_unregister(char * fname)
return NFTP_ERR_VEC;
if (0 == strcmp(fcb->fname, fname)) {
nftp_vec_delete(fcb_reg, (void **)&fcb, i);
free(fcb->fname);
free(fcb);
return (0);
}
}
Expand Down
15 changes: 8 additions & 7 deletions test/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,28 @@ test_codec_ack()
static int
test_codec_file()
{
int rv;
nftp * p;
size_t len;
uint8_t *v;

uint8_t demo1_file[] = {
0x03, 0x00, 0x00, 0x00, 0x13, // type & length
0x03, 0x00, 0x00, 0x00, 0x15, // type & length
0x7c, 0x6d, 0x8b, 0xab, // fileid
0x00, 0x02, 0x00, 0x06, // blockseq & length of content
0x00, 0x02, 0x00, 0x00, 0x00, 0x06, // blockseq & length of content
0x61, 0x62, 0x63, 0x64, 0x65, 0x66 // content
};

assert(0 == nftp_alloc(&p));

assert(0 == nftp_decode(p, demo1_file, sizeof(demo1_file)));
assert(0 == (rv = nftp_decode(p, demo1_file, sizeof(demo1_file))));

assert(NFTP_TYPE_FILE == p->type);
assert(sizeof(demo1_file) == p->len);
assert(((0x7c << 24) + (0x6d << 16) + (0x8b << 8) + (0xab)) == p->fileid);
assert(2 == p->blockseq);
assert(6 == p->ctlen);
assert(0 == strncmp((char *)(demo1_file + 13), (char *)p->content, 6));
assert(0 == strncmp((char *)(demo1_file + 15), (char *)p->content, 6));

assert(0 == nftp_encode(p, &v, &len));
assert(sizeof(demo1_file) == len);
Expand All @@ -147,9 +148,9 @@ test_codec_end()
uint8_t *v;

uint8_t demo1_end[] = {
0x04, 0x00, 0x00, 0x00, 0x13, // type & length
0x04, 0x00, 0x00, 0x00, 0x15, // type & length
0x7c, 0x6d, 0x8b, 0xab, // fileid
0x00, 0x02, 0x00, 0x06, // blockseq & length of content
0x00, 0x02, 0x00, 0x00, 0x00, 0x06, // blockseq & length of content
0x61, 0x62, 0x63, 0x64, 0x65, 0x66 // content
};

Expand All @@ -160,7 +161,7 @@ test_codec_end()
assert(NFTP_TYPE_END == p->type);
assert(sizeof(demo1_end) == p->len);
assert(((0x7c << 24) + (0x6d << 16) + (0x8b << 8) + (0xab)) == p->fileid);
assert(0 == strncmp((char *)(demo1_end + 13), (char *)p->content, 6));
assert(0 == strncmp((char *)(demo1_end + 15), (char *)p->content, 6));

assert(0 == nftp_encode(p, &v, &len));
assert(sizeof(demo1_end) == len);
Expand Down
Loading