2017-04-15 21:30:30 +00:00
|
|
|
/* interface_toolbar_reader.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "interface_toolbar_reader.h"
|
|
|
|
#include "sync_pipe.h"
|
|
|
|
#include "wsutil/file_util.h"
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
const int header_size = 6;
|
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int InterfaceToolbarReader::async_pipe_read(void *data, int nbyte)
|
|
|
|
{
|
|
|
|
BOOL success;
|
2017-08-28 13:27:41 +00:00
|
|
|
DWORD nof_bytes_read;
|
2017-08-25 09:28:34 +00:00
|
|
|
OVERLAPPED overlap;
|
2017-08-28 13:27:41 +00:00
|
|
|
int bytes_read = -1;
|
2017-08-25 09:28:34 +00:00
|
|
|
|
|
|
|
overlap.Pointer = 0;
|
|
|
|
overlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (overlap.hEvent == NULL)
|
|
|
|
{
|
|
|
|
// CreateEvent failed with error code GetLastError()
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = ReadFile(control_in_, data, nbyte, &nof_bytes_read, &overlap);
|
|
|
|
|
|
|
|
if (success && nof_bytes_read != 0)
|
|
|
|
{
|
|
|
|
// The read operation completed successfully.
|
2017-08-28 13:27:41 +00:00
|
|
|
bytes_read = nof_bytes_read;
|
2017-08-25 09:28:34 +00:00
|
|
|
}
|
2017-08-28 13:27:41 +00:00
|
|
|
else if (!success && GetLastError() == ERROR_IO_PENDING)
|
2017-08-25 09:28:34 +00:00
|
|
|
{
|
|
|
|
// The operation is still pending, wait for a signal.
|
2017-08-28 13:27:41 +00:00
|
|
|
if (WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0)
|
2017-08-25 09:28:34 +00:00
|
|
|
{
|
|
|
|
// The wait operation has completed.
|
|
|
|
success = GetOverlappedResult(control_in_, &overlap, &nof_bytes_read, FALSE);
|
|
|
|
|
|
|
|
if (success && nof_bytes_read != 0)
|
|
|
|
{
|
|
|
|
// The get result operation completed successfully.
|
2017-08-28 13:27:41 +00:00
|
|
|
bytes_read = nof_bytes_read;
|
2017-08-25 09:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 13:27:41 +00:00
|
|
|
CloseHandle(overlap.hEvent);
|
|
|
|
return bytes_read;
|
2017-08-25 09:28:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int InterfaceToolbarReader::pipe_read(char *data, int nbyte)
|
|
|
|
{
|
|
|
|
int total_len = 0;
|
|
|
|
|
|
|
|
while (total_len < nbyte)
|
|
|
|
{
|
|
|
|
char *data_ptr = data + total_len;
|
|
|
|
int data_len = nbyte - total_len;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
int read_len = async_pipe_read(data_ptr, data_len);
|
|
|
|
#else
|
|
|
|
int read_len = (int)ws_read(fd_in_, data_ptr, data_len);
|
|
|
|
#endif
|
|
|
|
if (read_len == -1)
|
|
|
|
{
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
total_len += read_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
|
|
|
|
if (QThread::currentThread()->isInterruptionRequested())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_len;
|
|
|
|
}
|
2017-04-15 21:30:30 +00:00
|
|
|
|
|
|
|
void InterfaceToolbarReader::loop()
|
|
|
|
{
|
|
|
|
QByteArray header;
|
|
|
|
QByteArray payload;
|
2017-08-25 09:28:34 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct timeval timeout;
|
2017-04-15 21:30:30 +00:00
|
|
|
fd_set readfds;
|
2017-08-25 09:28:34 +00:00
|
|
|
fd_in_ = ws_open(control_in_.toUtf8(), O_RDONLY | O_BINARY | O_NONBLOCK, 0);
|
2017-04-15 21:30:30 +00:00
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
if (fd_in_ == -1)
|
2017-04-15 21:30:30 +00:00
|
|
|
{
|
|
|
|
emit finished();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-25 09:28:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
header.resize(header_size);
|
2017-04-15 21:30:30 +00:00
|
|
|
|
|
|
|
forever
|
|
|
|
{
|
2017-08-25 09:28:34 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-15 21:30:30 +00:00
|
|
|
FD_ZERO(&readfds);
|
2017-08-25 09:28:34 +00:00
|
|
|
FD_SET(fd_in_, &readfds);
|
2017-04-15 21:30:30 +00:00
|
|
|
|
|
|
|
timeout.tv_sec = 2;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
int ret = select(fd_in_ + 1, &readfds, NULL, NULL, &timeout);
|
2017-04-15 21:30:30 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-08-25 09:28:34 +00:00
|
|
|
|
2017-04-15 21:30:30 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
|
|
|
|
if (QThread::currentThread()->isInterruptionRequested())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
if (ret == 0 || !FD_ISSET(fd_in_, &readfds))
|
2017-04-15 21:30:30 +00:00
|
|
|
{
|
2017-08-25 09:28:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-15 21:30:30 +00:00
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
// Read the header from the pipe.
|
|
|
|
if (pipe_read(header.data(), header_size) != header_size)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-04-15 21:30:30 +00:00
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
unsigned char high_nibble = header[1] & 0xFF;
|
|
|
|
unsigned char mid_nibble = header[2] & 0xFF;
|
|
|
|
unsigned char low_nibble = header[3] & 0xFF;
|
|
|
|
int payload_len = (int)((high_nibble << 16) + (mid_nibble << 8) + low_nibble) - 2;
|
|
|
|
|
|
|
|
payload.resize(payload_len);
|
|
|
|
// Read the payload from the pipe.
|
|
|
|
if (pipe_read(payload.data(), payload_len) != payload_len)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header[0] == SP_TOOLBAR_CTRL)
|
|
|
|
{
|
|
|
|
emit received(ifname_, (unsigned char)header[4], (unsigned char)header[5], payload);
|
2017-04-15 21:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 09:28:34 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
ws_close(fd_in_);
|
2017-04-15 21:30:30 +00:00
|
|
|
#endif
|
2017-08-25 09:28:34 +00:00
|
|
|
|
2017-04-15 21:30:30 +00:00
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|