为什么零填充需要SOCKADDR_IN? [英] Why is zero padding needed in sockaddr_in?

查看:129
本文介绍了为什么零填充需要SOCKADDR_IN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我GOOGLE了它,有些人说:为了保持同样的大小与结构sockaddr。但是,内核不会使用SOCKADDR直接(右?)。当使用它。内核将它转换回它是什么。那么,为什么是零填充需要的?

 结构sockaddr {
    无符号短上sa_family; //地址族,AF_xxx
    炭sa_data [14]; // 14字节协议地址的
};结构SOCKADDR_IN {
    短sin_family; //例如AF_INET,AF_INET6
    无符号短sin_port; //例如htons(3490)
    struct in_addr,这个sin_addr; //看到struct in_addr,这个,下面
    焦炭的sin_zero [8]; //零这个,如果你想
};struct in_addr,这个{
    无符号长s_addr; //使用inet_pton负荷()
};


解决方案

我能找到的信息两个相关的作品是

谈起code的片段,做的的清除字节


  

这是一个错误。我看到它occasionaly发生。此错误会导致在应用程序中未定义的行为。


随后与一些吃茶


  

大部分的净code并不SOCKADDR_IN使用,它使用套接字地址。当你使用类似的sendto功能,您必须显式转换SOCKADDR_IN,或任何地址,您正在使用,指向sockaddr。 SOCKADDR_IN是大小sockaddr的相同,但内部的大小是因为轻微的黑客一样的。


  
  

这是黑客的sin_zero。真正有用的数据在SOCKADDR_IN的长度比sockaddr的短。但不同的是在SOCKADDR_IN使用小缓冲软垫;该缓冲区的sin_zero。


和最后,即可以在不同的地方找到一个信息


  

在某些架构,它不会引起任何问题,不清除的sin_zero。但在其他架构它可能。它要求规范明确的sin_zero,所以你必须这样做,如果你想你的code是免费的错误,现在和将来。


回答这个问题:


  

为什么我们需要这8字节填充?


和答案


  

UNIX网络编程第3.2节说的POSIX规范
  需要在结构中只有三个成员:sin_family,sin_addr,属于和
  sin_port。这是可以接受的符合POSIX标准的实施来定义
  额外的结构成员,这是正常的互联网套接字地址
  结构体。几乎所有的实现添加成员的sin_zero使所有插座
  地址结构大小至少为16个字节。


  
  

这有点像结构的填充,也许保留了额外的字段
  未来。你永远不会使用它,就像评论。


这是与第一链路一致。清除字节告诉接收这些字节不习惯站在我们这一边。

I googled it and some people says "To keep the same size with struct sockaddr". But Kernel will not use sockaddr directly(right?). When using it. kernel will cast it back to what it is. So why is zero padding needed?

struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
};

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};

解决方案

The two more relevant pieces of information I could find are

Talking about a snippet of code that does not clear the bytes

This is a bug. I see it occur occasionaly. This bug can cause undefined behaviour in applications.

Followed with some explications

Most of the net code does not use sockaddr_in, it uses sockaddr. When you use a function like sendto, you must explicitly cast sockaddr_in, or whatever address you are using, to sockaddr. sockaddr_in is the same size as sockaddr, but internally the sizes are the same because of a slight hack.

That hack is sin_zero. Really the length of useful data in sockaddr_in is shorter than sockaddr. But the difference is padded in sockaddr_in using a small buffer; that buffer is sin_zero.

and finally, an information that can be found at various places

On some architectures, it wont cause any problems not clearing sin_zero. But on other architectures it might. Its required by specification to clear sin_zero, so you must do this if you intend your code to be bug free for now and in the future.

answering the question

why we need this 8 byte padding?

and the answer

Unix network programming chapter 3.2 says that, "The POSIX specification requires only three members in the structure: sin_family, sin_addr, and sin_port. It is acceptable for a POSIX-compliant implementation to define additional structure members, and this is normal for an Internet socket address structure. Almost all implementations add the sin_zero member so that all socket address structures are at least 16 bytes in size. "

It's kinda like structure padding, maybe reserved for extra fields in the future. You will never use it, just as commented.

which is consistent with the first link. Clearing the bytes tells the receiver "those bytes are not used on our side".

这篇关于为什么零填充需要SOCKADDR_IN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆