分类: LINUX
2016-05-17 10:38:46
转载链接 http://www.cnblogs.com/hnrainll/archive/2011/07/18/2109375.html
最近在看网络编程部分内容,刚开始接触结构体struct sockaddr和struct sockaddr_in的时候,极其迷惑,在网上翻了不少帖子,慢慢有那么点清晰了。呵呵,现在结合一篇网友的文章,对这两个结构体进行解读。
在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:
typedef unsigned short sa_family_t;
struct sockaddr {
sa_family_t sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
}
struct ifreq {
#include
#include
int main()
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr));
if( ioctl( sock, SIOCGIFHWADDR, &ifr ) == 0 ) //获取mac
{
memcpy( arp, ifr.ifr_hwaddr.sa_data, 6 );
}
在linux环境下,结构体struct sockaddr_in在/usr/include/netinet/in.h中定义,具体如下:
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
/* 字符数组sin_zero[8]的存在是为了保证结构体struct sockaddr_in的大小和结构体struct sockaddr的大小相等 */
};
struct sockaddr是通用的套接字地址,而struct sockaddr_in则是internet环境下套接字的地址形式,二者长度一样,都是16个字节。二者是并列结构,指向sockaddr_in结构的指针也可以指向sockaddr。一般情况下,需要把sockaddr_in结构强制转换成sockaddr结构再传入系统调用函数中。
下面是struct sockaddr_in中用到两个数据类型,具体定义如下:
/* Type to represent a port. */
typedef uint16_t in_port_t;
struct in_addr其实就是32位IP地址
struct in_addr {
unsigned long s_addr;
};
BSD网络软件中包含了两个函数,用来在二进制地址格式和点分十进制字符串格式之间相互转换,但是这两个函数仅仅支持IPv4。
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
功能相似的两个函数同时支持IPv4和IPv6
const char *inet_ntop(int domain, const void *addr, char *str, socklen_t size);
int inet_pton(int domain, const char *str, void *addr);
通常的用法是:
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET; /* 主机字节序 */
my_addr.sin_port = htons(MYPORT); /* short, 网络字节序 */
my_addr.sin_addr.s_addr = inet_addr("192.168.0.1");
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
//memset(&my_addr.sin_zero, 0, 8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
============
struct ifreq 获取IP 和mac
char ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
short ifru_flags;
int ifru_metric;
caddr_t ifru_data;
} ifr_ifru;
};
#include
#include
#define ETH_NAME "eth0"
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
unsigned char arp[6] ;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) //获取ip
{
}
printf( "adapter hardware address x:x:x:x:x:x\n",
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5] );
return 0;
}