Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
FdUtils.cxx
Go to the documentation of this file.
1
35#include "FdUtils.hxx"
36
37#ifdef __linux__
38#include <netinet/in.h>
39#include <netinet/tcp.h>
40#include <sys/stat.h>
41#include <termios.h> /* tc* functions */
42#endif
43
44#include "nmranet_config.h"
45
52#define PCALL_LOGERR(where, callfn, fd, args...) \
53 do \
54 { \
55 int ret = callfn(fd, args); \
56 if (ret < 0) \
57 { \
58 char buf[256]; \
59 strerror_r(errno, buf, sizeof(buf)); \
60 LOG_ERROR("fd %d %s: %s", fd, where, buf); \
61 } \
62 } while (0)
63
68{
69#ifdef __linux__
70 const int rcvbuf = config_gridconnect_tcp_rcv_buffer_size();
71 if (rcvbuf > 1)
72 {
73 PCALL_LOGERR("setsockopt SO_RCVBUF", ::setsockopt, fd, SOL_SOCKET,
74 SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
75 }
76 const int sndbuf = config_gridconnect_tcp_snd_buffer_size();
77 if (sndbuf > 1)
78 {
79 PCALL_LOGERR("setsockopt SO_SNDBUF", ::setsockopt, fd, SOL_SOCKET,
80 SO_SNDBUF, &sndbuf, sizeof(sndbuf));
81 int ret = 0;
82 socklen_t retsize = sizeof(ret);
83 ::getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &ret, &retsize);
84 LOG(ALWAYS, "fd %d sndbuf %d", fd, ret);
85 }
86 const int lowat = config_gridconnect_tcp_notsent_lowat_buffer_size();
87 if (lowat > 1)
88 {
89 PCALL_LOGERR("setsockopt tcp_notsent_lowat", ::setsockopt, fd,
90 IPPROTO_TCP, TCP_NOTSENT_LOWAT, &lowat, sizeof(lowat));
91 int ret = 0;
92 socklen_t retsize = sizeof(ret);
93 ::getsockopt(fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT, &ret, &retsize);
94 LOG(ALWAYS, "fd %d lowat %d", fd, ret);
95 }
96#endif
97}
98
103{
104#ifdef __linux__
105 // Sets up the terminal in raw mode. Otherwise linux might echo
106 // characters coming in from the device and that will make
107 // packets go back to where they came from.
108 HASSERT(!tcflush(fd, TCIOFLUSH));
109 struct termios settings;
110 HASSERT(!tcgetattr(fd, &settings));
111 cfmakeraw(&settings);
112 cfsetspeed(&settings, B115200);
113 HASSERT(!tcsetattr(fd, TCSANOW, &settings));
114#endif
115}
116
120{
121#ifdef __linux__
122 struct stat statbuf;
123 fstat(fd, &statbuf);
124
125 if (S_ISSOCK(statbuf.st_mode))
126 {
128 }
129 else if (isatty(fd))
130 {
131 optimize_tty_fd(fd);
132 }
133#endif
134}
#define PCALL_LOGERR(where, callfn, fd, args...)
Performs a system call on an fd.
Definition FdUtils.cxx:52
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
Set the socket options.
Definition Socket.cxx:255
int getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len)
Get the socket options.
Definition Socket.cxx:272
#define IPPROTO_TCP
TCP Raw Socket.
Definition in.h:70
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int ALWAYS
Loglevel that is always printed.
Definition logging.h:49
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define SO_RCVBUF
socket option for receive buffer size
Definition socket.h:76
uint32_t socklen_t
type of sockaddr lenth
Definition socket.h:89
#define SOL_SOCKET
socket option category
Definition socket.h:64
static void optimize_socket_fd(int fd)
Optimizes the kernel settings like socket and TCP options for an fd that is an outgoing TCP socket.
Definition FdUtils.cxx:67
static void optimize_tty_fd(int fd)
Sets the kernel settings like queuing and terminal settings for an fd that is an outgoing tty.
Definition FdUtils.cxx:102
static void optimize_fd(int fd)
For an fd that is an outgoing link, detects what kind of file descriptor this is and calls the approp...
Definition FdUtils.cxx:119