Skip to content

Commit

Permalink
Added debugging outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
LongHairedHacker committed Jul 5, 2019
1 parent d33d66f commit 233c896
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
38 changes: 37 additions & 1 deletion src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* $Id: conn.c,v 1.3 2015/02/25 10:33:57 kapyar Exp $
*/


#include "conn.h"
#include "queue.h"
#include "state.h"
Expand Down Expand Up @@ -404,6 +405,24 @@ conn_loop(void)
/* break; */
}

#ifdef DEBUG
len = queue.len;
curconn = queue.beg;
min_timeout = cfg.conntimeout;
while (len--)
{
if(FD_ISSET(curconn->sd, &sdsetrd)) {
logw(2, "conn[%d]: can read.", curconn->sd);
state_conn_log(curconn);
}
if(FD_ISSET(curconn->sd, &sdsetwr)) {
logw(2, "conn[%d]: can write.", curconn->sd);
state_conn_log(curconn);
}
curconn = queue_next_elem(&queue, curconn);
}
#endif

/* calculating elapsed time */
(void)gettimeofday(&tts, NULL);
tval = 1000000ul * (tts.tv_sec - ts.tv_sec) +
Expand Down Expand Up @@ -752,11 +771,14 @@ conn_loop(void)
if (rc == 0)
continue; /* timeout caused, we will do select() again */



/* processing data on the sockets */
len = queue.len;
curconn = queue.beg;
while (len--)
{
logw(2, "conn[%d] is being serviced.", curconn->sd);
switch (curconn->state)
{
case CONN_HEADER:
Expand All @@ -773,6 +795,7 @@ conn_loop(void)
curconn = conn_close(curconn);
break;
}
logw(5, "conn[%d]: read %d bytes.", curconn->sd, rc);
curconn->ctr += rc;
if (curconn->state == CONN_HEADER)
if (curconn->ctr >= MB_UNIT_ID)
Expand Down Expand Up @@ -862,6 +885,17 @@ conn_loop(void)
case CONN_RESP:
if (FD_ISSET(curconn->sd, &sdsetwr))
{
logw(5, "conn[%d]: is ready to write.", curconn->sd);

int frame_len = MB_FRAME(curconn->buf, MB_LENGTH_L) + HDRSIZE - curconn->ctr;
if(frame_len < 0 || frame_len > HDRSIZE + BUFSIZE) {
logw(5, "conn[%d]: invalid frame length", curconn->sd);
}

logw(5, "conn[%d]: Writing %d bytes", curconn->sd, MB_FRAME(curconn->buf, MB_LENGTH_L) +
HDRSIZE - curconn->ctr);
logw(5, "conn[%d]: ctr: %d", curconn->sd, curconn->ctr);

rc = conn_write(curconn->sd,
curconn->buf + curconn->ctr,
MB_FRAME(curconn->buf, MB_LENGTH_L) +
Expand All @@ -871,6 +905,9 @@ conn_loop(void)
curconn = conn_close(curconn);
break;
}

logw(5, "conn[%d]: %d bytes written.", curconn->sd, rc);

curconn->ctr += rc;
if (curconn->ctr == (MB_FRAME(curconn->buf, MB_LENGTH_L) + HDRSIZE))
state_conn_set(curconn, CONN_HEADER);
Expand Down Expand Up @@ -902,4 +939,3 @@ conn_fix_request_header_len(conn_t *conn, unsigned char len)
MB_FRAME(conn->buf, MB_LENGTH_L) = len;
}
}

63 changes: 49 additions & 14 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,47 @@ state_conn_set(conn_t *conn, int state)
conn->ctr = 0;
conn->read_len = HDRSIZE;
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_HEADER",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_HEADER",
conn->sd);
#endif
break;
case CONN_RQST_FUNC:
conn->read_len = HDRSIZE + MB_FRAME(conn->buf, MB_LENGTH_L);
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_RQST_FUNC",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_RQST_FUNC",
conn->sd);
#endif
break;
case CONN_RQST_NVAL:
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_RQST_NVAL",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_RQST_NVAL",
conn->sd);
#endif
break;
case CONN_RQST_TAIL:
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_RQST_TAIL",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_RQST_TAIL",
conn->sd);
#endif
break;
case CONN_TTY:
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_TTY",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_TTY",
conn->sd);
#endif
break;
case CONN_RESP:
conn->ctr = 0;
#ifdef DEBUG
logw(5, "conn[%s]: state now is CONN_RESP",
inet_ntoa(conn->sockaddr.sin_addr));
logw(5, "conn[%d]: state now is CONN_RESP",
conn->sd);
#endif
break;
default:
/* unknown state, exiting */
#ifdef DEBUG
logw(5, "conn_set_state([%s]) - invalid state (%d)",
inet_ntoa(conn->sockaddr.sin_addr), state);
logw(5, "conn_set_state([%d]) - invalid state (%d)",
conn->sd, state);
#endif
exit (-1);
}
Expand All @@ -125,6 +125,41 @@ state_conn_set(conn_t *conn, int state)
conn->timeout = cfg.conntimeout;
}


void
state_conn_log(conn_t *conn)
{
switch (conn->state)
{
case CONN_HEADER:
logw(5, "conn[%d]: state is CONN_HEADER",
conn->sd);
break;
case CONN_RQST_FUNC:
logw(5, "conn[%d]: state is CONN_RQST_FUNC",
conn->sd);
break;
case CONN_RQST_NVAL:
logw(5, "conn[%d]: state is CONN_RQST_NVAL",
conn->sd);
case CONN_RQST_TAIL:
logw(5, "conn[%d]: state is CONN_RQST_TAIL",
conn->sd);
break;
case CONN_TTY:
logw(5, "conn[%d]: state is CONN_TTY",
conn->sd);
break;
case CONN_RESP:
logw(5, "conn[%d]: state is CONN_RESP",
conn->sd);
break;
default:
logw(5, "conn_set_state([%d]) - invalid state (%d)",
conn->sd, conn->state);
}
}

/*
* Set tty device to STATE
*/
Expand Down
9 changes: 5 additions & 4 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* state.h - state management procedures
*
* Copyright (c) 2002-2003, 2013, Victor Antonovich (v.antonovich@gmail.com)
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand Down Expand Up @@ -46,6 +46,7 @@
/* prototypes */
conn_t *state_conn_search(queue_t *queue, conn_t *conn, int state);
void state_conn_set(conn_t *conn, int state);
void state_conn_log(conn_t *conn);
void state_tty_set(ttydata_t *mod, int state);

#endif /* _STATE_H */

0 comments on commit 233c896

Please sign in to comment.