aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHervé Poussineau <hpoussin@reactos.org>2012-09-10 20:52:25 +0200
committerJan Kiszka <jan.kiszka@siemens.com>2012-09-13 12:38:14 +0200
commit0962988a73632918da09a44cf200ce22ab6398e8 (patch)
tree179e64c2a8d83b758d0fd88f04a6e7cdc4d165ad
parent68a9cbb2bbcb8687f0e3d6f4847b01925dffdd17 (diff)
downloadslirp-0962988a73632918da09a44cf200ce22ab6398e8.zip
slirp-0962988a73632918da09a44cf200ce22ab6398e8.tar.gz
slirp-0962988a73632918da09a44cf200ce22ab6398e8.tar.bz2
slirp: improve TFTP performance
When transferring a file, keep it open during the whole transfer, instead of opening/closing it for each block. Signed-off-by: Hervé Poussineau <hpoussin@reactos.org> Reviewed-by: Aurelien Jarno <aurelien@aurel32.net> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
-rw-r--r--tftp.c20
-rw-r--r--tftp.h1
2 files changed, 13 insertions, 8 deletions
diff --git a/tftp.c b/tftp.c
index 0a9bba6..7009675 100644
--- a/tftp.c
+++ b/tftp.c
@@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt)
static void tftp_session_terminate(struct tftp_session *spt)
{
+ if (spt->fd >= 0) {
+ close(spt->fd);
+ spt->fd = -1;
+ }
g_free(spt->filename);
spt->slirp = NULL;
}
@@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
/* sessions time out after 5 inactive seconds */
if ((int)(curtime - spt->timestamp) > 5000) {
- g_free(spt->filename);
+ tftp_session_terminate(spt);
goto found;
}
}
@@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
found:
memset(spt, 0, sizeof(*spt));
memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
+ spt->fd = -1;
spt->client_port = tp->udp.uh_sport;
spt->slirp = slirp;
@@ -96,23 +101,22 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
uint8_t *buf, int len)
{
- int fd;
int bytes_read = 0;
- fd = open(spt->filename, O_RDONLY | O_BINARY);
+ if (spt->fd < 0) {
+ spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
+ }
- if (fd < 0) {
+ if (spt->fd < 0) {
return -1;
}
if (len) {
- lseek(fd, block_nr * 512, SEEK_SET);
+ lseek(spt->fd, block_nr * 512, SEEK_SET);
- bytes_read = read(fd, buf, len);
+ bytes_read = read(spt->fd, buf, len);
}
- close(fd);
-
return bytes_read;
}
diff --git a/tftp.h b/tftp.h
index 3412e81..b0952e2 100644
--- a/tftp.h
+++ b/tftp.h
@@ -33,6 +33,7 @@ struct tftp_t {
struct tftp_session {
Slirp *slirp;
char *filename;
+ int fd;
struct in_addr client_ip;
uint16_t client_port;