1 /**
2  * @file sa/printaddr.c  Socket Address printing
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 #ifdef HAVE_GETIFADDRS
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #define __USE_MISC 1   /**< Use MISC code */
10 #include <net/if.h>
11 #endif
12 #include <re_types.h>
13 #include <re_fmt.h>
14 #include <re_sa.h>
15 
16 
17 /**
18  * Print a Socket Address including IPv6 scope identifier
19  *
20  * @param pf Print function
21  * @param sa Socket Address
22  *
23  * @return 0 if success, otherwise errorcode
24  */
25 int sa_print_addr(struct re_printf *pf, const struct sa *sa)
26 {
27 	int err;
28 
29 	if (!sa)
30 		return 0;
31 
32 	err = re_hprintf(pf, "%j", sa);
33 
34 #if defined (HAVE_GETIFADDRS) && defined (HAVE_INET6)
35 	if (sa_af(sa) == AF_INET6 && sa_is_linklocal(sa)) {
36 
37 		char ifname[IF_NAMESIZE];
38 
39 		if (!if_indextoname(sa->u.in6.sin6_scope_id, ifname))
40 			return errno;
41 
42 		err |= re_hprintf(pf, "%%%s", ifname);
43 	}
44 #endif
45 
46 	return err;
47 }
48