📄 http_active.c
字号:
case SYN: case END: break; case ACT_REQ: case ACT_RSP: client_state = ACTIVE; break; case REQ: client_state = ACTIVE; log_REQ(); break; case RSP: log_RSP(); break; } break; /* ends case PENDING_IDLE */ } else /* it has crossed the idle threshold */ client_state = IDLE; /* NOTE: drop through to IDLE to handle the current event */ case IDLE: switch (event_type) { case SYN: case END: break; case ACT_REQ: case ACT_RSP: client_state = ACTIVE; log_IDLE(ts); break; case REQ: client_state = ACTIVE; log_IDLE(ts); log_REQ(); break; case RSP: log_RSP(); break; break; /* ends case PENDING_IDLE */ } break; default: break; } /* end switch */ strcpy(last_client_ts, ts); } /* end while (!feof ....) */ close (dumpFP); close (outFP);}/* updates the status of connections for each interesting event */void set_connection(char *sp, char *dh, char *dp, enum event_types type){ int cx; /* A connection is identified by the host/port 3-tuple (the source IP address is not needed because only one client is handled at a time). The connection's status depends on the type of the event that caused the update. The following event type are defined with their effect on the connection's status: SYN, ACT-REQ, - The connection has begun (is an "active" ACT-RSP connection. Add it to the table as idle (activity == 0) if a SYN or with request/ response activity (activity == 1) for ACT-REQ or ACT-RSP. REQ - Find the connection in the table and mark it with an outstanding request (activity == 1). RSP - Find the connection in the table and mark it with a completed request (activity == 0). END - The connection has ended (is no longer an "active" connection). Remove it from the table. */ switch (type) { case SYN: case ACT_REQ: case ACT_RSP: { cx = AddConnection(sp, dh, dp); if (cx < 0) /* already there */ { error_state("Add for existing connection"); return; } if (cx > MAX_CONNECTIONS) /* table overflow */ { error_state("Active connections exceeds maximum"); exit (-1); } connections[cx].state = type; if (type == SYN) connections[cx].activity = 0; else connections[cx].activity = 1; break; } case REQ: { cx = FindConnection(sp, dh, dp); if (cx < 0) /* not there */ { error_state("REQ for non-existent connection"); return; } if ((connections[cx].state == RSP) || (connections[cx].state == ACT_REQ) || (connections[cx].state == SYN)) { connections[cx].activity = 1; connections[cx].state = REQ; } else error_state("REQ in invalid connection state"); break; } case RSP: { cx = FindConnection(sp, dh, dp); if (cx < 0) /* not there */ { error_state("RSP for non-existent connection"); return; } if ((connections[cx].state == REQ) || (connections[cx].state == ACT_RSP) || (connections[cx].state == SYN)) { connections[cx].activity = 0; connections[cx].state = RSP; } else error_state("RSP in invalid connection state"); break; } case END: { cx = FindConnection(sp, dh, dp); if (cx < 0) /* not there */ { error_state("End for non-existent connection"); return; } connections[cx].activity = 0; connections[cx].state = END; cx = RemoveConnection(sp, dh, dp); break; } default: break; }}/* A set of functions to maintain the table of "active" connections for the current client (IP address). All of these use simple linear scans of the table because we expect the number of concurrently active connections from a client to be small (< 100) *//* Clears the active connections from the connection table and resets the count of active connections to zero */void ClearConnections(void){ int i; for (i = 0; i < active_connections; i++) { strcpy(connections[i].id, ""); connections[i].activity = 0; connections[i].state = END; } active_connections = 0;}/* Count the number of active connections that have an outstanding request (activity == 1) */int ConnectionsActive(void){ int count = 0; int i; for (i = 0; i < active_connections; i++) count = count + connections[i].activity; return (count);}/* Find a connection in the table by its identifying host/port 3-tuple and return its index (or -1 if not found). Note that the source IP address is not necessary since the table is used for one client at a time.*/int FindConnection(char *sp, char *dh, char *dp){ char connection[50]; int i; strcpy(connection, sp); strcat(connection, dh); strcat(connection, dp); /* find the connection in the table */ for (i = 0; i < active_connections; i++) { if (strcmp(connections[i].id, connection) == 0) break; } if (i == active_connections) /* not there */ return(-1); else return (i);}/* Add a new connection to the table identified by its host/port 3-tuple (source IP is not needed because the table is for one client (IP address) only). Return the index of the added connection or -1 if it is already in the table. Increase the count of active connections by 1 if added. Initialize the state of the added connection to the reset state.*/ int AddConnection(char *sp, char *dh, char *dp){ char connection[50]; int i; strcpy(connection, sp); strcat(connection, dh); strcat(connection, dp); /* check to see if connection already in the table; if not there, add it */ for (i = 0; i < active_connections; i++) { if (strcmp(connections[i].id, connection) == 0) break; } if (i < active_connections) return(-1); /* already there */ else { active_connections += 1; if (active_connections > MAX_CONNECTIONS) return (active_connections); /* table overflow */ strcpy(connections[i].id, connection); connections[i].activity = 0; connections[i].state = END; return (i); }}/* Remove a connection from the table and compact the table by shifting all connections above the "hole" down by one index value. Return 0 if all is OK or -1 if the connection was not there. Decrease the count of active connections by one if one was removed. Reset the vacated table entry to the reset state*/int RemoveConnection(char *sp, char *dh, char *dp){ char connection[50]; int i, j; strcpy(connection, sp); strcat(connection, dh); strcat(connection, dp); /* find the connection in the table; if not there, error */ for (i = 0; i < active_connections; i++) { if (strcmp(connections[i].id, connection) == 0) break; } if (i == active_connections) return (-1); /* move all active connections above this down by one with overwriting */ for (j = i + 1; j < active_connections; j++) { strcpy(connections[j-1].id, connections[j].id); connections[j-1].activity = connections[j].activity; connections[j-1].state = connections[j].state; } /* clearing the top vacated slot is not strictly necessary but it may help with debugging */ strcpy(connections[active_connections - 1].id, ""); connections[active_connections - 1].activity = 0; connections[active_connections - 1].state = END; active_connections -= 1; return (0);}/* Copy the input line to the output file */void log_REQ(void){ fprintf(outFP, "%s", new_line);}/* Copy the input line to the output file */void log_RSP(void){ fprintf(outFP, "%s", new_line);}/* create a line in the output for the idle period */void log_IDLE(char *ts){ int elapsed; elapsed = (int) elapsed_ms(ts, idle_begin); fprintf(outFP, "%s %-15s %5s > %-15s %5s IDLE%12d %s\n", ts, current_src, "*", "*", "*", elapsed, idle_begin); }void error_line(char * s){ fprintf(outFP, "%s", s);}void error_state(char * s){ fprintf(outFP, "%s %-15s %5s > %-15s %5s ERROR %s\n", ts, current_src, "*", "*", "*", s);}/*--------------------------------------------------------------*/ /* subtract two timevals (t1 - t0) with result in tdiff *//* tdiff, t1 and t0 are all pointers to struct timeval *//*--------------------------------------------------------------*/ static voidtvsub(tdiff, t1, t0)struct timeval *tdiff, *t1, *t0;{ tdiff->tv_sec = t1->tv_sec - t0->tv_sec; tdiff->tv_usec = t1->tv_usec - t0->tv_usec; if (tdiff->tv_usec < 0) { tdiff->tv_sec--; tdiff->tv_usec += 1000000; }}/*--------------------------------------------------------------*/ /* compute the elapsed time in milliseconds to end_time *//* from some past time given by start_time (both formatted timevals) *//*--------------------------------------------------------------*/ long elapsed_ms(char *end, char *start){ struct timeval delta, end_time, start_time; long elapsed_time; char end_tmp[20]; char start_tmp[20]; char *cursor; char *cp; strcpy(end_tmp, end); cursor = end_tmp; cp = (char *)strsep(&cursor, "." ); end_time.tv_sec = atoi(end_tmp); end_time.tv_usec = atoi(cursor); strcpy(start_tmp, start); cursor = start_tmp; cp = (char *)strsep(&cursor, "." ); start_time.tv_sec = atoi(start_tmp); start_time.tv_usec = atoi(cursor); tvsub(&delta, &end_time, &start_time); /* express as milliseconds */ elapsed_time = (delta.tv_sec * 1000) + (delta.tv_usec/1000); return (elapsed_time);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -