Skip to content

Commit

Permalink
net: restrict access EdgeTriggerEvents members
Browse files Browse the repository at this point in the history
- File descriptor creation and destruction are handled within ETE, no
  reason to expose read-write access to it.
- (Un)registerPipe should only be used within WakeupPipe, remove from
  public view.
  • Loading branch information
kwvg committed May 14, 2024
1 parent f24520a commit ec99294
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ void CConnman::SocketEventsKqueue(std::set<SOCKET>& recv_set,
timeout.tv_nsec = (only_poll ? 0 : SELECT_TIMEOUT_MILLISECONDS % 1000) * 1000 * 1000;

int n{-1};
ToggleWakeupPipe([&](){n = kevent(Assert(m_edge_trig_events)->m_fd, nullptr, 0, events, maxEvents, &timeout);});
ToggleWakeupPipe([&](){n = kevent(Assert(m_edge_trig_events)->GetFileDescriptor(), nullptr, 0, events, maxEvents, &timeout);});
if (n == -1) {
LogPrintf("kevent wait error\n");
} else if (n > 0) {
Expand Down Expand Up @@ -1631,7 +1631,7 @@ void CConnman::SocketEventsEpoll(std::set<SOCKET>& recv_set,
epoll_event events[maxEvents];

int n{-1};
ToggleWakeupPipe([&](){n = epoll_wait(Assert(m_edge_trig_events)->m_fd, events, maxEvents, only_poll ? 0 : SELECT_TIMEOUT_MILLISECONDS);});
ToggleWakeupPipe([&](){n = epoll_wait(Assert(m_edge_trig_events)->GetFileDescriptor(), events, maxEvents, only_poll ? 0 : SELECT_TIMEOUT_MILLISECONDS);});
for (int i = 0; i < n; i++) {
auto& e = events[i];
if((e.events & EPOLLERR) || (e.events & EPOLLHUP)) {
Expand Down
2 changes: 0 additions & 2 deletions src/util/edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <logging.h>
#include <util/sock.h>

#include <assert.h>

#ifdef USE_EPOLL
#include <sys/epoll.h>
#endif
Expand Down
20 changes: 11 additions & 9 deletions src/util/edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <compat.h>

#include <assert.h>
#include <cstdint>
#include <string>

Expand All @@ -23,37 +24,38 @@ class EdgeTriggeredEvents
~EdgeTriggeredEvents();

bool IsValid() const { return m_valid; }
int GetFileDescriptor() const { assert(m_fd != -1); return m_fd; }

/* Add socket to interest list */
bool AddSocket(SOCKET socket) const;
/* Remove socket from interest list */
bool RemoveSocket(SOCKET socket) const;

/* Register wakeup pipe with EdgeTriggeredEvents instance */
bool RegisterPipe(int wakeup_pipe);
/* Unregister wakeup pipe with EdgeTriggeredEvents instance */
bool UnregisterPipe(int wakeup_pipe);

/* Register events for socket */
bool RegisterEvents(SOCKET socket) const;
/* Unregister events for socket */
bool UnregisterEvents(SOCKET socket) const;

private:
friend class WakeupPipe;
/* Register wakeup pipe with EdgeTriggeredEvents instance */
bool RegisterPipe(int wakeup_pipe);
/* Unregister wakeup pipe with EdgeTriggeredEvents instance */
bool UnregisterPipe(int wakeup_pipe);

private:
bool RegisterEntity(int entity, std::string entity_name) const;
bool UnregisterEntity(int entity, std::string entity_name) const;

public:
/* File descriptor used to interact with events mode */
int m_fd{-1};

private:
/* Flag set if pipe has been registered with instance */
bool m_pipe_registered{false};
/* Instance validity flag set during construction */
bool m_valid{false};
/* Flag for storing selected socket events mode */
SocketEventsMode m_mode;
/* File descriptor used to interact with events mode */
int m_fd{-1};
};

#endif /* BITCOIN_UTIL_EDGE_H */

0 comments on commit ec99294

Please sign in to comment.