--- embedaddon/mtr/packet/command.c 2019/10/21 14:25:31 1.1 +++ embedaddon/mtr/packet/command.c 2021/03/17 00:07:30 1.1.1.2 @@ -11,20 +11,27 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "command.h" #include #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif +#include #include #include #include #include #include +#include #include "cmdparse.h" #include "platform.h" @@ -321,6 +328,7 @@ void send_probe_command( param.ttl = 255; param.packet_size = 64; param.timeout = 10; + param.is_probing_byte_order = false; for (i = 0; i < command->argument_count; i++) { name = command->argument_name[i]; @@ -422,4 +430,64 @@ void dispatch_buffer_commands( printf("0 command-buffer-overflow\n"); buffer->incoming_read_position = 0; } +} + +/* + Initialize the command buffer and put the command stream in + non-blocking mode. +*/ +void init_command_buffer( + struct command_buffer_t *command_buffer, + int command_stream) +{ + int flags; + + memset(command_buffer, 0, sizeof(struct command_buffer_t)); + command_buffer->command_stream = command_stream; + + /* Get the current command stream flags */ + flags = fcntl(command_stream, F_GETFL, 0); + if (flags == -1) { + error(EXIT_FAILURE, errno, "Unexpected command stream error"); + } + + /* Set the O_NONBLOCK bit */ + if (fcntl(command_stream, F_SETFL, flags | O_NONBLOCK)) { + error(EXIT_FAILURE, errno, "Unexpected command stream error"); + } +} + +/* Read currently available data from the command stream */ +int read_commands( + struct command_buffer_t *buffer) +{ + int space_remaining = + COMMAND_BUFFER_SIZE - buffer->incoming_read_position - 1; + char *read_position = + &buffer->incoming_buffer[buffer->incoming_read_position]; + int read_count; + int command_stream = buffer->command_stream; + + read_count = read(command_stream, read_position, space_remaining); + + /* If the command stream has been closed, read will return zero. */ + if (read_count == 0) { + errno = EPIPE; + return -1; + } + + if (read_count > 0) { + /* Account for the newly read data */ + buffer->incoming_read_position += read_count; + } + + if (read_count < 0) { + /* EAGAIN simply means there is no available data to read */ + /* EINTR indicates we received a signal during read */ + if (errno != EINTR && errno != EAGAIN) { + error(EXIT_FAILURE, errno, "Unexpected command buffer read error"); + } + } + + return 0; }