Browse Source

List command bug fix, easier debugging

- List command was generating bad data size because it was adding
directory entry char to buffer before testing buffer overflow.
- Added MAVLINK_FTP_DEBUG define to easily turn on/off noisier
debugging output
sbg
Don Gagne 11 years ago
parent
commit
c8ecf59ab7
  1. 42
      src/modules/mavlink/mavlink_ftp.cpp
  2. 20
      src/modules/mavlink/mavlink_ftp.h

42
src/modules/mavlink/mavlink_ftp.cpp

@ -39,6 +39,9 @@
#include "mavlink_ftp.h" #include "mavlink_ftp.h"
// Uncomment the line below to get better debug output. Never commit with this left on.
//#define MAVLINK_FTP_DEBUG
MavlinkFTP *MavlinkFTP::_server; MavlinkFTP *MavlinkFTP::_server;
MavlinkFTP * MavlinkFTP *
@ -125,7 +128,9 @@ MavlinkFTP::_worker(Request *req)
warnx("ftp: bad crc"); warnx("ftp: bad crc");
} }
//printf("ftp: channel %u opc %u size %u offset %u\n", req->channel(), hdr->opcode, hdr->size, hdr->offset); #ifdef MAVLINK_FTP_DEBUG
printf("ftp: channel %u opc %u size %u offset %u\n", req->channel(), hdr->opcode, hdr->size, hdr->offset);
#endif
switch (hdr->opcode) { switch (hdr->opcode) {
case kCmdNone: case kCmdNone:
@ -172,7 +177,9 @@ out:
// handle success vs. error // handle success vs. error
if (errorCode == kErrNone) { if (errorCode == kErrNone) {
hdr->opcode = kRspAck; hdr->opcode = kRspAck;
//warnx("FTP: ack\n"); #ifdef MAVLINK_FTP_DEBUG
warnx("FTP: ack\n");
#endif
} else { } else {
warnx("FTP: nak %u", errorCode); warnx("FTP: nak %u", errorCode);
hdr->opcode = kRspNak; hdr->opcode = kRspNak;
@ -217,7 +224,9 @@ MavlinkFTP::_workList(Request *req)
return kErrNotDir; return kErrNotDir;
} }
//warnx("FTP: list %s offset %d", dirPath, hdr->offset); #ifdef MAVLINK_FTP_DEBUG
warnx("FTP: list %s offset %d", dirPath, hdr->offset);
#endif
ErrorCode errorCode = kErrNone; ErrorCode errorCode = kErrNone;
struct dirent entry, *result = nullptr; struct dirent entry, *result = nullptr;
@ -247,11 +256,13 @@ MavlinkFTP::_workList(Request *req)
uint32_t fileSize = 0; uint32_t fileSize = 0;
char buf[256]; char buf[256];
char direntType;
// store the type marker // Determine the directory entry type
switch (entry.d_type) { switch (entry.d_type) {
case DTYPE_FILE: case DTYPE_FILE:
hdr->data[offset++] = kDirentFile; // For files we get the file size as well
direntType = kDirentFile;
snprintf(buf, sizeof(buf), "%s/%s", dirPath, entry.d_name); snprintf(buf, sizeof(buf), "%s/%s", dirPath, entry.d_name);
struct stat st; struct stat st;
if (stat(buf, &st) == 0) { if (stat(buf, &st) == 0) {
@ -259,29 +270,34 @@ MavlinkFTP::_workList(Request *req)
} }
break; break;
case DTYPE_DIRECTORY: case DTYPE_DIRECTORY:
hdr->data[offset++] = kDirentDir; direntType = kDirentDir;
break; break;
default: default:
hdr->data[offset++] = kDirentUnknown; direntType = kDirentUnknown;
break; break;
} }
if (entry.d_type == DTYPE_FILE) { if (entry.d_type == DTYPE_FILE) {
// Files send filename and file length
snprintf(buf, sizeof(buf), "%s\t%d", entry.d_name, fileSize); snprintf(buf, sizeof(buf), "%s\t%d", entry.d_name, fileSize);
} else { } else {
// Everything else just sends name
strncpy(buf, entry.d_name, sizeof(buf)); strncpy(buf, entry.d_name, sizeof(buf));
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = 0;
} }
size_t nameLen = strlen(buf); size_t nameLen = strlen(buf);
// name too big to fit? // Do we have room for the name, the one char directory identifier and the null terminator?
if ((nameLen + offset + 2) > kMaxDataLength) { if ((offset + nameLen + 2) > kMaxDataLength) {
break; break;
} }
// copy the name, which we know will fit // Move the data into the buffer
hdr->data[offset++] = direntType;
strcpy((char *)&hdr->data[offset], buf); strcpy((char *)&hdr->data[offset], buf);
//printf("FTP: list %s %s\n", dirPath, (char *)&hdr->data[offset-1]); #ifdef MAVLINK_FTP_DEBUG
printf("FTP: list %s %s\n", dirPath, (char *)&hdr->data[offset-1]);
#endif
offset += nameLen + 1; offset += nameLen + 1;
} }
@ -342,7 +358,9 @@ MavlinkFTP::_workRead(Request *req)
} }
// Seek to the specified position // Seek to the specified position
//warnx("seek %d", hdr->offset); #ifdef MAVLINK_FTP_DEBUG
warnx("seek %d", hdr->offset);
#endif
if (lseek(_session_fds[session_index], hdr->offset, SEEK_SET) < 0) { if (lseek(_session_fds[session_index], hdr->offset, SEEK_SET) < 0) {
// Unable to see to the specified location // Unable to see to the specified location
warnx("seek fail"); warnx("seek fail");

20
src/modules/mavlink/mavlink_ftp.h

@ -154,15 +154,17 @@ private:
if (!success) { if (!success) {
warnx("FTP TX ERR"); warnx("FTP TX ERR");
} }
// else { #ifdef MAVLINK_FTP_DEBUG
// warnx("wrote: sys: %d, comp: %d, chan: %d, len: %d, checksum: %d", else {
// _mavlink->get_system_id(), warnx("wrote: sys: %d, comp: %d, chan: %d, len: %d, checksum: %d",
// _mavlink->get_component_id(), _mavlink->get_system_id(),
// _mavlink->get_channel(), _mavlink->get_component_id(),
// len, _mavlink->get_channel(),
// msg.checksum); len,
// } msg.checksum);
}
#endif
} }
uint8_t *rawData() { return &_message.data[0]; } uint8_t *rawData() { return &_message.data[0]; }

Loading…
Cancel
Save