而sock则是inet地址族为上层提供的一个抽象,
我们所说的建立一个套接字其实就是建立一个sock结构,其中包含了一个连接的绝大部分信息.
socket就是通常使用的socket接口,处于用户空间
例如在intsocket(int family, int type, intprotocol)调用时候,首先会掉用sock_create,而sock_create回调用sock_alloc()分配一个具体的socket,接下来sock_create将根据protocol调用具体的sock create函数创建sock结构
int sock_create(int family, int type, int protocol, struct socket **res)
{
int i;
struct socket *sock;
/*
* Check protocol is in range
*/
if (family < 0 || family >= NPROTO)
return -EINVAL;
#if defined(CONFIG_KMOD) && defined(CONFIG_NET)
/* Attempt to load a protocol module if the find failed.
*
* 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
* requested real, full-featured networking support upon configuration.
* Otherwise module support will break!
*/
if (net_families[family] == NULL) {
char module_name[30];
sprintf(module_name,"net-pf-%d",family);
request_module(module_name);
}
#endif
if (net_families[family]==NULL)
return -EINVAL;
/*
* Check that this is a type that we know how to manipulate and
* the protocol makes sense here. The family can still reject the
* protocol later.
*/
if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
type != SOCK_SEQPACKET && type != SOCK_RAW && type != SOCK_RDM &&
#ifdef CONFIG_XTP
type != SOCK_WEB &&
#endif
type != SOCK_PACKET) || protocol < 0)
return -EINVAL;
/*
* Allocate the socket and allow the family to set things up. if
* the protocol is 0, the family is instructed to select an appropriate
* default.
*/
if (!(sock = sock_alloc())) {
printf("no more alloc sockets");
return -ENFILE; /* Not exactly a match, but its the closest posix thing */
}
sock->type = type;
if ((i = net_families[family]->create(sock, protocol)) < 0) {
sock_release(sock);
return i;
}
*res = sock;
return i;
}
链接: http://oldlinux.org/cgi-bin/LB5000XP/topic.cgi?forum=23&topic=7&show=