Skip to content

Commit

Permalink
Merge branch 'addsocketfds-potentialfix' of https://github.com/nutech…
Browse files Browse the repository at this point in the history
…software/ser2sock into binary_mode
  • Loading branch information
f34rdotcom committed Mar 29, 2014
2 parents ed0661c + a33e379 commit 47d0696
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions ser2sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

#define SERIAL_CONNECTED_MSG "!SER2SOCK SERIAL_CONNECTED\r\n"
#define SERIAL_DISCONNECTED_MSG "!SER2SOCK SERIAL_DISCONNECTED\r\n"
#define SOCKET_CONNECTED_MSG "!SER2SOCK Connected\r\n"

#define CONFIG_PATH "/etc/ser2sock/ser2sock.conf"

Expand Down Expand Up @@ -140,7 +141,6 @@ typedef struct
typedef struct
{
/* flags */
int new;
int inuse;
int fd_type;

Expand Down Expand Up @@ -171,7 +171,7 @@ typedef char byte_t;
int init_system();
int init_listen_socket_fd();
int init_serial_fd(char *path);
void add_to_all_socket_fds(char * message);
void add_to_all_socket_fds(char *message);
void add_to_serial_fd(char * message);
int cleanup_fd(int n);
void set_non_blocking(int fd);
Expand Down Expand Up @@ -236,7 +236,6 @@ BOOL option_send_terminal_init = FALSE;
int option_debug_level = 0;
BOOL option_keep_connection = FALSE;
int option_open_serial_delay = 5000;
int line_ended = 0;
int serial_connected = 0;
struct timeval tv_serial_start, tv_last_serial_check;

Expand Down Expand Up @@ -423,7 +422,6 @@ int init_system()
for (x = 0; x < MAXCONNECTIONS; x++)
{
my_fds[x].inuse = FALSE;
my_fds[x].new = TRUE;
my_fds[x].fd = -1;
my_fds[x].fd_type = NA;
#ifdef HAVE_LIBSSL
Expand Down Expand Up @@ -755,7 +753,6 @@ int add_fd(int fd, int fd_type)
my_fds[x].inuse = TRUE;
my_fds[x].fd_type = fd_type;
my_fds[x].fd = fd;
my_fds[x].new = TRUE;
results = x;
break;
}
Expand Down Expand Up @@ -866,7 +863,6 @@ void poll_serial_port()
serial_connected = 1;
tv_last_serial_check.tv_sec = 0;
tv_last_serial_check.tv_usec = 0;
line_ended=0;
add_to_all_socket_fds(SERIAL_CONNECTED_MSG);
}
else
Expand Down Expand Up @@ -1065,8 +1061,7 @@ BOOL poll_read_fdset(fd_set *read_fdset)
tempbuffer);
}

tempbuffer = strdup(
"!SER2SOCK Connected\r\n");
tempbuffer = strdup(SOCKET_CONNECTED_MSG);
fifo_add(&my_fds[added_slot].send_buffer,
tempbuffer);
if (serial_connected)
Expand Down Expand Up @@ -1362,7 +1357,6 @@ void listen_loop()

/* reset state if asked */
if (reset_state) {
line_ended=0;
tv_serial_start.tv_sec = 0;
tv_serial_start.tv_usec = 0;
tv_last_serial_check.tv_sec = 0;
Expand Down Expand Up @@ -1416,10 +1410,9 @@ void listen_loop()
*/
void add_to_all_socket_fds(char * message)
{

char * tempbuffer;
char * location;
int n;

/*
Adding anything to the fifo must be allocated so it can be free'd later
Not very efficient but we have plenty of mem with as few connections as we
Expand All @@ -1432,40 +1425,11 @@ void add_to_all_socket_fds(char * message)
if (my_fds[n].fd_type == CLIENT_SOCKET)
{
/* caller of fifo_get must free this */
if (line_ended || !my_fds[n].new)
{
if (my_fds[n].new)
my_fds[n].new = FALSE;
tempbuffer = strdup(message);
fifo_add(&my_fds[n].send_buffer, tempbuffer);
}
else
{
/*
wait for our first \n to start on a clean line. We are skipping
our first message so we dont send a partial message
*/
location = strchr(message, '\n');
if (location != NULL)
{
tempbuffer = strdup(location);
fifo_add(&my_fds[n].send_buffer, tempbuffer);
my_fds[n].new = FALSE;
}
}
tempbuffer = strdup(message);
fifo_add(&my_fds[n].send_buffer, tempbuffer);
}
}
}
line_ended = 0;
if (message)
{
location = message + strlen(message) - 1;
if (location)
{
if ((*location) == '\n')
line_ended = 1;
}
}
}

/*
Expand Down

0 comments on commit 47d0696

Please sign in to comment.