File: showminissdpdnotif.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 (85 lines) | stat: -rw-r--r-- 2,074 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
/* $Id: showminissdpdnotif.c,v 1.2 2016/01/19 10:25:24 nanard Exp $ */
/* vim: shiftwidth=4 tabstop=4 noexpandtab
 * MiniUPnP project
 * (c) 2016 Thomas Bernard
 * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
 * This software is subject to the conditions detailed
 * in the LICENCE file provided within the distribution */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "codelength.h"
#include "printresponse.h"

static volatile sig_atomic_t quitting = 0;

static void sighandler(int sig)
{
	(void)sig;
	quitting = 1;
}

int main(int argc, char * * argv)
{
	int i;
	int s;
	struct sockaddr_un addr;
	const char * sockpath = "/var/run/minissdpd.sock";
	unsigned char buffer[4096];
	ssize_t n;
	const char command5[] = { 0x05, 0x00 };
	struct sigaction sa;

	for(i=0; i<argc-1; i++) {
		if(0==strcmp(argv[i], "-s"))
			sockpath = argv[++i];
	}

	/* set signal handlers */
	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_handler = sighandler;
	if(sigaction(SIGINT, &sa, NULL)) {
		fprintf(stderr, "Failed to set SIGINT handler.\n");
	}
	sa.sa_handler = sighandler;
	if(sigaction(SIGTERM, &sa, NULL)) {
		fprintf(stderr, "Failed to set SIGTERM handler.\n");
	}

	s = socket(AF_UNIX, SOCK_STREAM, 0);
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
	if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
		fprintf(stderr, "connecting to %s : ", addr.sun_path);
		perror("connect");
		return 1;
	}
	printf("connected to %s\n", addr.sun_path);
	n = write(s, command5, sizeof(command5));	/* NOTIF command */
	printf("%d bytes written\n", (int)n);

	while(!quitting) {
		n = read(s, buffer, sizeof(buffer));
		if(n < 0) {
			if(errno == EINTR) continue;
			perror("read");
			break;
		} else if(n == 0) {
			printf("Socket closed\n");
			break;
		}
		printf("%d bytes read\n", (int)n);
		printresponse(buffer, (int)n);
	}
	printf("Quit...\n");
	close(s);
	return 0;
}