当connect作用于流套接字的时候,是地址指明的对端建立连接,对于TCP来讲,connect会完成与对端的三次握手建立连接的过程;当connect作用于数据报套接字的时候,用于指明发送的对端地址,并且只能向该地址发送数据,指明之后,可以使用send等发送数据,无需使用sendto的参数再次指明发送地址;

 /*
* Attempt to connect to a socket with the server address. The address
* is in user space so we verify it is OK and move it to kernel space.
*
* For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
* break bindings
*
* NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
* other SEQPACKET protocols that take time to connect() as it doesn't
* include the -EINPROGRESS status for such sockets.
*/ SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)
{
struct socket *sock;
struct sockaddr_storage address;
int err, fput_needed; /* 找到socket */
sock = sockfd_lookup_light(fd, &err, &fput_needed);
if (!sock)
goto out; /* 地址复制到内核 */
err = move_addr_to_kernel(uservaddr, addrlen, &address);
if (err < )
goto out_put; /* 安全检查 */
err =
security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
if (err)
goto out_put; /* 调用对应类型的connect */
err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
sock->file->f_flags);
out_put:
fput_light(sock->file, fput_needed);
out:
return err;
}

这里主要看流式套接字的实现;

 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
int addr_len, int flags)
{
int err; lock_sock(sock->sk);
err = __inet_stream_connect(sock, uaddr, addr_len, flags, );
release_sock(sock->sk);
return err;
}

建立连接过程中会根据socket的状态做不同的处理,连接并不一定马上完成,所以其也会有从连接中,到连接成功的状态转移;其中还包含了对阻塞socket和非阻塞socket的处理;tcp层的connect实现后续阅读过程中会进行补充;

 /*
* Connect to a remote host. There is regrettably still a little
* TCP 'magic' in here.
*/
int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
int addr_len, int flags, int is_sendmsg)
{
struct sock *sk = sock->sk;
int err;
long timeo; /*
* uaddr can be NULL and addr_len can be 0 if:
* sk is a TCP fastopen active socket and
* TCP_FASTOPEN_CONNECT sockopt is set and
* we already have a valid cookie for this socket.
* In this case, user can call write() after connect().
* write() will invoke tcp_sendmsg_fastopen() which calls
* __inet_stream_connect().
*/
/* 地址存在 */
if (uaddr) { /* 检查地址长度 */
if (addr_len < sizeof(uaddr->sa_family))
return -EINVAL; /* 对unspec的特殊处理 */
if (uaddr->sa_family == AF_UNSPEC) {
err = sk->sk_prot->disconnect(sk, flags);
sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
goto out;
}
} /* 根据socket状态对应处理 */
switch (sock->state) {
default:
err = -EINVAL;
goto out;
/* 已连接 */
case SS_CONNECTED:
err = -EISCONN;
goto out;
/* 正在连接 */
case SS_CONNECTING:
/* 对是否发消息做不同处理,fastopen*/
if (inet_sk(sk)->defer_connect)
err = is_sendmsg ? -EINPROGRESS : -EISCONN;
else
err = -EALREADY;
/* Fall out of switch with err, set for this state */
break;
/* 未连接 */
case SS_UNCONNECTED:
err = -EISCONN;
/* 需要为closed状态 */
if (sk->sk_state != TCP_CLOSE)
goto out; /* 传输层协议的connect */
err = sk->sk_prot->connect(sk, uaddr, addr_len);
if (err < )
goto out; /* 标记状态为正在连接 */
sock->state = SS_CONNECTING; /* fastopen */
if (!err && inet_sk(sk)->defer_connect)
goto out; /* Just entered SS_CONNECTING state; the only
* difference is that return value in non-blocking
* case is EINPROGRESS, rather than EALREADY.
*/
/* 非阻塞情况返回inprogress,阻塞返回already */
err = -EINPROGRESS;
break;
} /* 阻塞情况下需要获取超时时间,非阻塞为0 */
timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); /* 已发送或者已收到syn */
if (( << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
tcp_sk(sk)->fastopen_req &&
tcp_sk(sk)->fastopen_req->data ? : ; /* Error code is set above */
/* 非阻塞退出,阻塞则等待连接,等待剩余时间为0,退出 */
if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
goto out; /* 处理信号,达到最大调度时间或者被打断 */
err = sock_intr_errno(timeo);
if (signal_pending(current))
goto out;
} /* Connection was closed by RST, timeout, ICMP error
* or another process disconnected us.
*/
/* 状态为关闭 */
if (sk->sk_state == TCP_CLOSE)
goto sock_error; /* sk->sk_err may be not zero now, if RECVERR was ordered by user
* and error was received after socket entered established state.
* Hence, it is handled normally after connect() return successfully.
*/ /* 设置为连接状态 */
sock->state = SS_CONNECTED;
err = ;
out:
return err; sock_error:
err = sock_error(sk) ? : -ECONNABORTED;
/* 设置未连接状态 */
sock->state = SS_UNCONNECTED;
if (sk->sk_prot->disconnect(sk, flags))
sock->state = SS_DISCONNECTING;
goto out;
}

对于阻塞socket,需要加入到等待队列中,等待连接完成;

 /* 阻塞等待连接 */
static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
{
DEFINE_WAIT_FUNC(wait, woken_wake_function); /* 添加到等待队列 */
add_wait_queue(sk_sleep(sk), &wait);
sk->sk_write_pending += writebias; /* Basic assumption: if someone sets sk->sk_err, he _must_
* change state of the socket from TCP_SYN_*.
* Connect() does not allow to get error notifications
* without closing the socket.
*/
/* 等待连接状态改变 */
while (( << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
release_sock(sk);
timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
lock_sock(sk);
if (signal_pending(current) || !timeo)
break;
} /* 从等待队列移除 */
remove_wait_queue(sk_sleep(sk), &wait);
sk->sk_write_pending -= writebias;
return timeo;
}
05-11 17:13