File: testgetifaddr.c

package info (click to toggle)
miniupnpd 2.3.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,040 kB
  • sloc: ansic: 28,571; sh: 2,024; makefile: 164
file content (58 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (2)
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
/* $Id: testgetifaddr.c,v 1.10 2025/04/21 21:43:37 nanard Exp $ */
/* MiniUPnP project
 * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
 * (c) 2006-2025 Thomas Bernard
 * This software is subject to the conditions detailed
 * in the LICENCE file provided within the distribution */
#include <stdio.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#include "getifaddr.h"
#include "getconnstatus.h"

#if defined(__sun)
/* solaris 10 does not define LOG_PERROR */
#define LOG_PERROR 0
#endif

int main(int argc, char * * argv) {
	char str_addr[64];
	struct in_addr addr;
	struct in_addr mask;
	int r;
#ifdef ENABLE_IPV6
	char str_addr6[64];
#endif
	if(argc < 2) {
		fprintf(stderr, "Usage:\t%s interface_name\n", argv[0]);
		return 1;
	}

	openlog("testgetifaddr", LOG_CONS|LOG_PERROR, LOG_USER);
	printf("Interface %s status : %s\n", argv[1], get_wan_connection_status_str(argv[1]));

	r = getifaddr(argv[1], str_addr, sizeof(str_addr), &addr, &mask);
	if(r < 0) {
		fprintf(stderr, "getifaddr(\"%s\") returned %d\n", argv[1], r);
		return 1;
	}
	printf("Interface %s has IP address %s.\n", argv[1], str_addr);
	printf("addr=%s ", inet_ntoa(addr));
	printf("mask=%s\n", inet_ntoa(mask));
#ifdef ENABLE_IPV6
	r = find_ipv6_addr(argv[1], str_addr6, sizeof(str_addr6));
	if(r < 0) {
		fprintf(stderr, "find_ipv6_addr() failed\n");
		return 1;
	} else if(r == 0) {
		printf("Interface %s has no IPv6 address.\n", argv[1]);
	} else {
		printf("Interface %s has IPv6 address %s.\n", argv[1], str_addr6);
	}
#endif
	return 0;
}