@@ -45,25 +45,32 @@ void Telem_UDP::handle_select_fds(fd_set &fds_read,
45
45
}
46
46
47
47
48
- void Telem_UDP::create_and_bind ()
48
+ void Telem_UDP::create_and_bind (INIReader *config )
49
49
{
50
- int _fd;
51
- struct sockaddr_in sa;
50
+ _is_server = is_server (config);
52
51
53
- _fd = socket (AF_INET, SOCK_DGRAM, 0 );
52
+ int _fd = socket (AF_INET, SOCK_DGRAM, 0 );
54
53
if (_fd < 0 ) {
55
54
perror (" socket" );
56
55
abort ();
57
56
}
58
57
59
- memset (&sa, 0 , sizeof (sa)) ;
58
+ struct sockaddr_in sa = {} ;
60
59
sa.sin_family = AF_INET;
61
60
sa.sin_addr .s_addr = htonl (INADDR_ANY);
62
- sa.sin_port = 0 ; // we don't care what our port is
63
61
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
67
74
}
68
75
69
76
fd = _fd;
@@ -80,21 +87,29 @@ uint16_t Telem_UDP::udp_port(INIReader *config) const
80
87
return config->GetInteger (" dflogger" , " udp_port" , 14550 );
81
88
}
82
89
90
+ bool Telem_UDP::is_server (INIReader *config) const
91
+ {
92
+ return config->GetBoolean (" dflogger" , " server" , false );
93
+ }
94
+
83
95
void Telem_UDP::pack_sockaddr (INIReader *config)
84
96
{
85
- // uint16_t tf_port = config->GetInteger("solo", "telem_forward_port", 14560);
86
97
const uint16_t tf_port = udp_port (config);
87
98
const std::string ip = udp_ip (config);
88
99
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;
92
106
#ifdef _WIN32
93
- sa_tf .sin_addr .s_addr = htonl (INADDR_LOOPBACK);
107
+ sa_destination .sin_addr .s_addr = htonl (INADDR_LOOPBACK);
94
108
#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
96
110
#endif
97
- sa_tf.sin_port = htons (tf_port);
111
+ sa_destination.sin_port = htons (tf_port);
112
+ }
98
113
}
99
114
100
115
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()
117
132
return 0 ;
118
133
}
119
134
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
+
120
140
return res;
121
141
}
122
142
@@ -142,12 +162,17 @@ void Telem_UDP::do_writer_sends()
142
162
mavlink_message_t &msg = _send_buf[_send_buf_start];
143
163
uint16_t messageLen = mavlink_msg_to_send_buffer ((uint8_t *)buf,&msg);
144
164
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
+ }
151
176
}
152
177
_send_buf_start++;
153
178
if (_send_buf_start >= send_buf_size ()) {
@@ -161,9 +186,8 @@ void Telem_UDP::do_writer_sends()
161
186
void Telem_UDP::configure (INIReader *config)
162
187
{
163
188
pack_sockaddr (config);
164
-
189
+ create_and_bind (config);
165
190
}
166
191
167
192
void Telem_UDP::init () {
168
- create_and_bind ();
169
193
}
0 commit comments