Skip to content

Commit dea516b

Browse files
committed
telem_udp: expand to also act as server
1 parent f96a9fb commit dea516b

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

telem_udp.cpp

+48-24
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,32 @@ void Telem_UDP::handle_select_fds(fd_set &fds_read,
4545
}
4646

4747

48-
void Telem_UDP::create_and_bind()
48+
void Telem_UDP::create_and_bind(INIReader *config)
4949
{
50-
int _fd;
51-
struct sockaddr_in sa;
50+
_is_server = is_server(config);
5251

53-
_fd = socket(AF_INET, SOCK_DGRAM, 0);
52+
int _fd = socket(AF_INET, SOCK_DGRAM, 0);
5453
if (_fd < 0) {
5554
perror("socket");
5655
abort();
5756
}
5857

59-
memset(&sa, 0, sizeof(sa));
58+
struct sockaddr_in sa = {};
6059
sa.sin_family = AF_INET;
6160
sa.sin_addr.s_addr = htonl(INADDR_ANY);
62-
sa.sin_port = 0; // we don't care what our port is
6361

64-
if (bind(_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
65-
perror("bind");
66-
abort();
62+
if (_is_server) {
63+
sa.sin_port = htons(udp_port(config));
64+
int one = 1;
65+
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
66+
if (bind(_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
67+
perror("bind");
68+
abort();
69+
}
70+
::fprintf(stderr, "bound port %u\n", sa.sin_port);
71+
} else {
72+
::fprintf(stderr, "client connection %u\n", sa.sin_port);
73+
sa.sin_port = 0; // we don't care what our port is
6774
}
6875

6976
fd = _fd;
@@ -80,21 +87,29 @@ uint16_t Telem_UDP::udp_port(INIReader *config) const
8087
return config->GetInteger("dflogger", "udp_port", 14550);
8188
}
8289

90+
bool Telem_UDP::is_server(INIReader *config) const
91+
{
92+
return config->GetBoolean("dflogger", "server", false);
93+
}
94+
8395
void Telem_UDP::pack_sockaddr(INIReader *config)
8496
{
85-
// uint16_t tf_port = config->GetInteger("solo", "telem_forward_port", 14560);
8697
const uint16_t tf_port = udp_port(config);
8798
const std::string ip = udp_ip(config);
8899

89-
la_log(LOG_INFO, "df-udp: connecting to %s:%u", ip.c_str(), tf_port);
90-
memset(&sa_tf, 0, sizeof(sa_tf));
91-
sa_tf.sin_family = AF_INET;
100+
if (is_server(config)) {
101+
la_log(LOG_INFO, "df-udp: listening on %s:%u", ip.c_str(), tf_port);
102+
} else {
103+
la_log(LOG_INFO, "df-udp: sending to %s:%u", ip.c_str(), tf_port);
104+
memset(&sa_destination, 0, sizeof(sa_destination));
105+
sa_destination.sin_family = AF_INET;
92106
#ifdef _WIN32
93-
sa_tf.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
107+
sa_destination.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
94108
#else
95-
inet_aton(ip.c_str(), &sa_tf.sin_addr); // useful for debugging
109+
inet_aton(ip.c_str(), &sa_destination.sin_addr); // useful for debugging
96110
#endif
97-
sa_tf.sin_port = htons(tf_port);
111+
sa_destination.sin_port = htons(tf_port);
112+
}
98113
}
99114

100115
bool Telem_UDP::sane_recv_buf(uint8_t *pkt UNUSED, uint16_t pktlen UNUSED) const
@@ -117,6 +132,11 @@ uint32_t Telem_UDP::handle_recv()
117132
return 0;
118133
}
119134

135+
if (_is_server && sa_destination.sin_port == 0) {
136+
sa_destination.sin_port = sa.sin_port;
137+
sa_destination.sin_addr.s_addr = sa.sin_addr.s_addr;
138+
}
139+
120140
return res;
121141
}
122142

@@ -142,12 +162,17 @@ void Telem_UDP::do_writer_sends()
142162
mavlink_message_t &msg = _send_buf[_send_buf_start];
143163
uint16_t messageLen = mavlink_msg_to_send_buffer((uint8_t*)buf,&msg);
144164

145-
int32_t bytes_sent =
146-
sendto(fd, buf, messageLen, 0,
147-
(struct sockaddr *)&sa_tf, sizeof(struct sockaddr));
148-
if (bytes_sent == -1) {
149-
la_log(LOG_INFO, "Failed sendto: %s", strerror(errno));
150-
// we drop the message anyway!
165+
if (_is_server &&
166+
sa_destination.sin_port == 0) {
167+
::fprintf(stderr, "telem_udp: server - nowhere to send\n");
168+
} else {
169+
const int32_t bytes_sent = sendto(fd, buf, messageLen, 0,
170+
(struct sockaddr *)&sa_destination,
171+
sizeof(struct sockaddr));
172+
if (bytes_sent == -1) {
173+
la_log(LOG_INFO, "Failed sendto: %s", strerror(errno));
174+
// we drop the message anyway!
175+
}
151176
}
152177
_send_buf_start++;
153178
if (_send_buf_start >= send_buf_size()) {
@@ -161,9 +186,8 @@ void Telem_UDP::do_writer_sends()
161186
void Telem_UDP::configure(INIReader *config)
162187
{
163188
pack_sockaddr(config);
164-
189+
create_and_bind(config);
165190
}
166191

167192
void Telem_UDP::init() {
168-
create_and_bind();
169193
}

telem_udp.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class Telem_UDP : public Telem_Client {
3333
private:
3434
int fd = -1;
3535

36-
struct sockaddr_in sa; // our send-from address
37-
struct sockaddr_in sa_tf; /* udp address to connect to */
36+
struct sockaddr_in sa = {}; // our send-from address
37+
struct sockaddr_in sa_destination = {}; /* udp address to connect to */
3838

39-
void create_and_bind();
39+
void create_and_bind(INIReader *config);
4040
void pack_sockaddr(INIReader *config);
4141
bool sane_packet(uint8_t *pkt, uint16_t bpktlen);
4242
virtual bool sane_recv_buf(uint8_t *pkt, uint16_t pktlen) const;
@@ -49,4 +49,7 @@ class Telem_UDP : public Telem_Client {
4949

5050
std::string udp_ip(INIReader *config) const;
5151
uint16_t udp_port(INIReader *config) const;
52+
bool is_server(INIReader *config) const;
53+
54+
bool _is_server = false;
5255
};

0 commit comments

Comments
 (0)