aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/interface_toolbar_reader.cpp
blob: 8fc5d6755678df83bf411cee433af01b68ca85c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* interface_toolbar_reader.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+*/

#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;

#ifdef _WIN32
int InterfaceToolbarReader::async_pipe_read(void *data, int nbyte)
{
    BOOL success;
    DWORD nof_bytes_read;
    OVERLAPPED overlap;
    int bytes_read = -1;

    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.
        bytes_read = nof_bytes_read;
    }
    else if (!success && GetLastError() == ERROR_IO_PENDING)
    {
        // The operation is still pending, wait for a signal.
        if (WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0)
        {
            // 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.
                bytes_read = nof_bytes_read;
            }
        }
    }

    CloseHandle(overlap.hEvent);
    return bytes_read;
}
#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;
}

void InterfaceToolbarReader::loop()
{
    QByteArray header;
    QByteArray payload;

#ifndef _WIN32
    struct timeval timeout;
    fd_set readfds;
    fd_in_ = ws_open(control_in_.toUtf8(), O_RDONLY | O_BINARY | O_NONBLOCK, 0);

    if (fd_in_ == -1)
    {
        emit finished();
        return;
    }
#endif

    header.resize(header_size);

    forever
    {
#ifndef _WIN32
        FD_ZERO(&readfds);
        FD_SET(fd_in_, &readfds);

        timeout.tv_sec = 2;
        timeout.tv_usec = 0;

        int ret = select(fd_in_ + 1, &readfds, NULL, NULL, &timeout);
        if (ret == -1)
        {
            break;
        }

#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
        if (QThread::currentThread()->isInterruptionRequested())
        {
            break;
        }
#endif

        if (ret == 0 || !FD_ISSET(fd_in_, &readfds))
        {
            continue;
        }
#endif

        // Read the header from the pipe.
        if (pipe_read(header.data(), header_size) != header_size)
        {
            break;
        }

        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);
        }
    }

#ifndef _WIN32
    ws_close(fd_in_);
#endif

    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:
 */