File: printresponse.c

package info (click to toggle)
minissdpd 1.5.20190210-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 484 kB
  • sloc: ansic: 4,944; sh: 296; makefile: 99
file content (90 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* $Id: printresponse.c,v 1.2 2016/01/19 10:24:59 nanard Exp $ */
/* vim: tabstop=4 shiftwidth=4 noexpandtab
 * Project : miniupnp
 * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
 * Author : Thomas BERNARD
 * copyright (c) 2005-2016 Thomas Bernard
 * This software is subjet to the conditions detailed in the
 * provided LICENCE file. */

#include <stdio.h>
#include "codelength.h"

#define NOTIF_NEW    1
#define NOTIF_UPDATE 2
#define NOTIF_REMOVE 3

void printresponse(const unsigned char * resp, int n)
{
	int i, l;
	int notif_type;
	unsigned int nresp;
	const unsigned char * p;

	if(n == 0)
		return;
	/* first, hexdump the response : */
	for(i = 0; i < n; i += 16) {
		printf("%06x | ", i);
		for(l = i; l < n && l < (i + 16); l++)
			printf("%02x ", resp[l]);
		while(l < (i + 16)) {
			printf("   ");
			l++;
		}
		printf("| ");
		for(l = i; l < n && l < (i + 16); l++)
			putchar((resp[l] >= ' ' && resp[l] < 128) ? resp[l] : '.');
		putchar('\n');
	}
	for(p = resp; p < resp + n; ) {
		/* now parse and display all devices of response */
		nresp = p[0]; /* 1st byte : number of devices in response */
		if(nresp == 0xff) {
			/* notification */
			notif_type = p[1];
			nresp = p[2];
			printf("Notification : ");
			switch(notif_type) {
			case NOTIF_NEW:	printf("new\n"); break;
			case NOTIF_UPDATE:	printf("update\n"); break;
			case NOTIF_REMOVE:	printf("remove\n"); break;
			default:	printf("**UNKNOWN**\n");
			}
			p += 3;
		} else {
			p++;
		}
		for(i = 0; i < (int)nresp; i++) {
			if(p >= resp + n)
				goto error;
			/*l = *(p++);*/
			DECODELENGTH(l, p);
			if(p + l > resp + n)
				goto error;
			printf("%d - %.*s\n", i, l, p); /* URL */
			p += l;
			if(p >= resp + n)
				goto error;
			/*l = *(p++);*/
			DECODELENGTH(l, p);
			if(p + l > resp + n)
				goto error;
			printf("    %.*s\n", l, p);	/* ST */
			p += l;
			if(p >= resp + n)
				goto error;
			/*l = *(p++);*/
			DECODELENGTH(l, p);
			if(p + l > resp + n)
				goto error;
			printf("    %.*s\n", l, p); /* USN */
			p += l;
		}
	}
	return;
error:
	printf("*** WARNING : TRUNCATED RESPONSE ***\n");
}