Compare commits
2 commits
7fa3b4366b
...
1662776d6b
Author | SHA1 | Date | |
---|---|---|---|
1662776d6b | |||
98c8239009 |
1 changed files with 15 additions and 16 deletions
|
@ -6,12 +6,12 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define MIN_TCP_PORT 1
|
#define MIN_TCP_PORT 1
|
||||||
#define TCP_TABLE "/proc/net/tcp"
|
#define TCP_TABLE_PATH "/proc/net/tcp"
|
||||||
#define TCP_TABLE_LINE_LENGTH 151
|
#define TCP_TABLE_LINE_LENGTH 151
|
||||||
#define LOCALHOST_HEX "0100007F"
|
#define LOCALHOSTIP_HEX "0100007F"
|
||||||
#define WILDCARD_HEX "00000000"
|
#define WILDCARDIP_HEX "00000000"
|
||||||
#define LISTENING_HEX "0A"
|
#define LISTENING_HEX "0A"
|
||||||
#define PORTS_BLOCKS_TO_ALLOW 4
|
#define BLOCKS_TO_ALLOC 4
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print a nice error message
|
* Print a nice error message
|
||||||
|
@ -23,14 +23,15 @@ static void print_error(char *name, const char *message) {
|
||||||
/**
|
/**
|
||||||
* Get all the listening TCP ports
|
* Get all the listening TCP ports
|
||||||
*/
|
*/
|
||||||
static unsigned short *get_listening_ports(unsigned short *listening_ports, FILE *tcp_table_fptr) {
|
static unsigned short *get_listening_ports(FILE *tcp_table_fptr) {
|
||||||
|
unsigned short *listening_ports = malloc(BLOCKS_TO_ALLOC * sizeof(short));
|
||||||
char line[TCP_TABLE_LINE_LENGTH];
|
char line[TCP_TABLE_LINE_LENGTH];
|
||||||
char delimiter[] = " ";
|
char delimiter[] = " ";
|
||||||
unsigned short len = 0;
|
unsigned short len = 0;
|
||||||
char *address;
|
char *address;
|
||||||
char *state;
|
char *state;
|
||||||
char *field;
|
char *field;
|
||||||
size_t allowed_for_ports = PORTS_BLOCKS_TO_ALLOW;
|
size_t blocks_allocated = BLOCKS_TO_ALLOC;
|
||||||
|
|
||||||
// Skip first line (header)
|
// Skip first line (header)
|
||||||
fgets(line, sizeof(line), tcp_table_fptr);
|
fgets(line, sizeof(line), tcp_table_fptr);
|
||||||
|
@ -48,19 +49,18 @@ static unsigned short *get_listening_ports(unsigned short *listening_ports, FILE
|
||||||
field = strtok(NULL, delimiter);
|
field = strtok(NULL, delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!strncmp(address, LOCALHOST_HEX, 8) || !strncmp(address, WILDCARD_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
|
if ((!strncmp(address, LOCALHOSTIP_HEX, 8) || !strncmp(address, WILDCARDIP_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
|
||||||
if (len == allowed_for_ports) {
|
if (len == blocks_allocated) {
|
||||||
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
blocks_allocated = blocks_allocated + BLOCKS_TO_ALLOC;
|
||||||
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
listening_ports = realloc(listening_ports, blocks_allocated * sizeof(short));
|
||||||
}
|
}
|
||||||
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
|
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == allowed_for_ports) {
|
if (len == blocks_allocated) {
|
||||||
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
listening_ports = realloc(listening_ports, blocks_allocated + 1);
|
||||||
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
|
||||||
}
|
}
|
||||||
listening_ports[len] = 0;
|
listening_ports[len] = 0;
|
||||||
|
|
||||||
|
@ -94,14 +94,13 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open TCP table
|
// Open TCP table
|
||||||
FILE *tcp_table_fptr = fopen(TCP_TABLE, "r");
|
FILE *tcp_table_fptr = fopen(TCP_TABLE_PATH, "r");
|
||||||
if (tcp_table_fptr == NULL) {
|
if (tcp_table_fptr == NULL) {
|
||||||
print_error(argv[0], "error opening the TCP table.");
|
print_error(argv[0], "error opening the TCP table.");
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short *listening_ports = malloc(PORTS_BLOCKS_TO_ALLOW * sizeof(unsigned short));
|
unsigned short *listening_ports = get_listening_ports(tcp_table_fptr);
|
||||||
listening_ports = get_listening_ports(listening_ports, tcp_table_fptr);
|
|
||||||
|
|
||||||
if (fclose(tcp_table_fptr) != 0) {
|
if (fclose(tcp_table_fptr) != 0) {
|
||||||
print_error(argv[0], "can't close the TCP table.");
|
print_error(argv[0], "can't close the TCP table.");
|
||||||
|
|
Loading…
Reference in a new issue