Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions servers/upnp_pit.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,48 @@ char* ssdpResponse() {
return responseBuffer;
}

// Logging the headers and their values
void extraction(char *pkt_buffer,char *log_msg){
char *current_pos=pkt_buffer;
char *nl = strchr(current_pos, '\n');
if (!nl) return;
current_pos = nl + 1;

while ((nl = strchr(current_pos, '\n')) != NULL){
if (*current_pos == '\r' || *current_pos == '\n') break;
if(*current_pos!=' '&& *current_pos != '\t'){
char *col = strchr(current_pos,':');
int current_line_len = nl - current_pos;
if (col != NULL && col < nl) {
int key_len = col - current_pos;
char key_name[64] = {0};
if (key_len > 0 && key_len < sizeof(key_name)) {
strncpy(key_name, current_pos, key_len);
key_name[key_len] = '\0';
}

char *val_ptr = col + 1;
while (*val_ptr == ' ') val_ptr++;

int val_len = 0;
while (val_ptr + val_len < nl &&
val_ptr[val_len] != '\r' &&
val_ptr[val_len] != '\n') {
val_len++;
}
if (val_len > 0) {
char fragment[512];
snprintf(fragment, sizeof(fragment), " | %s:%.*s", key_name, val_len, val_ptr);
if (strlen(log_msg) + strlen(fragment) < 1023) {
strcat(log_msg, fragment);
}
}
}
}
current_pos = nl + 1;
}
}

// Handles SSDP discovery requests and sends fake responses
void *ssdpListener(void *arg) {
(void)arg;
Expand Down Expand Up @@ -172,14 +214,16 @@ void *ssdpListener(void *arg) {
sendto(sockFd, response, strlen(response), 0,
(struct sockaddr *)&client_addr, sizeof(client_addr));

snprintf(msg, sizeof(msg), "%s M-SEARCH %s\n",
snprintf(msg, sizeof(msg), "%s M-SEARCH %s",
SERVER_ID, client_ip);
extraction(buffer,msg);
} else {
snprintf(msg, sizeof(msg), "%s non-M-SEARCH %s\n",
snprintf(msg, sizeof(msg), "%s non-M-SEARCH %s",
SERVER_ID, client_ip);
extraction(buffer,msg);
}

printf("%s", msg);
printf("%s\n", msg);
sendMetric(msg);
}

Expand Down