add sk_interface_addr for getting an interface's IP
This commit is contained in:
		
							parent
							
								
									d6779889cc
								
							
						
					
					
						commit
						f3ac2cad65
					
				
							
								
								
									
										38
									
								
								sk.c
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								sk.c
									
									
									
									
									
								
							@ -26,6 +26,8 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <sys/ioctl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <ifaddrs.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include "print.h"
 | 
			
		||||
#include "sk.h"
 | 
			
		||||
@ -158,6 +160,42 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len)
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int sk_interface_addr(char *name, int family, uint8_t *addr, int len)
 | 
			
		||||
{
 | 
			
		||||
	struct ifaddrs *ifaddr, *i;
 | 
			
		||||
	int copy_len, result = -1;
 | 
			
		||||
	void *copy_from;
 | 
			
		||||
	if (getifaddrs(&ifaddr) == -1) {
 | 
			
		||||
		pr_err("getifaddrs failed: %m");
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
	for (i = ifaddr; i; i = i->ifa_next) {
 | 
			
		||||
		if (i->ifa_addr && family == i->ifa_addr->sa_family &&
 | 
			
		||||
			strcmp(name, i->ifa_name) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			switch (family) {
 | 
			
		||||
			case AF_INET:
 | 
			
		||||
				copy_len = 4;
 | 
			
		||||
				copy_from = &((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr;
 | 
			
		||||
				break;
 | 
			
		||||
			case AF_INET6:
 | 
			
		||||
				copy_len = 16;
 | 
			
		||||
				copy_from = &((struct sockaddr_in6 *)i->ifa_addr)->sin6_addr.s6_addr;
 | 
			
		||||
				break;
 | 
			
		||||
			default:
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			if (copy_len > len)
 | 
			
		||||
				copy_len = len;
 | 
			
		||||
			memcpy(addr, copy_from, copy_len);
 | 
			
		||||
			result = copy_len;
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	free(ifaddr);
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int sk_receive(int fd, void *buf, int buflen,
 | 
			
		||||
	       struct hw_timestamp *hwts, int flags)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								sk.h
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								sk.h
									
									
									
									
									
								
							@ -63,6 +63,16 @@ int sk_get_ts_info(char *name, struct sk_ts_info *sk_info);
 | 
			
		||||
 */
 | 
			
		||||
int sk_interface_macaddr(char *name, unsigned char *mac, int len);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Obtains the first IP address assigned to a network interface.
 | 
			
		||||
 * @param name   The name of the interface
 | 
			
		||||
 * @param family The family of the address to get: AF_INET or AF_INET6
 | 
			
		||||
 * @param addr   Buffer to hold the result
 | 
			
		||||
 * @param len    Length of 'addr'
 | 
			
		||||
 * @return       The number of bytes written to addr on success, -1 otherwise.
 | 
			
		||||
 */
 | 
			
		||||
int sk_interface_addr(char *name, int family, uint8_t *addr, int len);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Read a message from a socket.
 | 
			
		||||
 * @param fd      An open socket.
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user