udp: implement connect

This commit is contained in:
Matheus
2025-12-25 20:18:12 -03:00
parent b2ccb01bd9
commit 91a5266d29
2 changed files with 22 additions and 3 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ typedef struct socketops_t {
int (*send)(socket_t *socket, sockdesc_t *desc);
int (*recv)(socket_t *socket, sockdesc_t *desc);
int (*poll)(socket_t *socket, polldata_t *data, int events);
int (*connect)(socket_t *socket, sockaddr_t *addr, uintmax_t data, cred_t *cred);
int (*connect)(socket_t *socket, sockaddr_t *addr, uintmax_t flags, cred_t *cred);
int (*listen)(socket_t *socket, int backlog);
int (*accept)(socket_t *server, socket_t *client, sockaddr_t *addr, uintmax_t flags);
int (*getname)(socket_t *socket, sockaddr_t *addr);
+21 -2
View File
@@ -188,7 +188,7 @@ static int udp_send(socket_t *socket, sockdesc_t *sockdesc) {
return 0;
}
if (sockdesc->addr == NULL) {
if (sockdesc->addr == NULL && udpsocket->peerport == 0) {
e = ENOTCONN;
goto cleanup;
}
@@ -352,6 +352,24 @@ static int udp_shutdown(socket_t *socket, int how) {
return 0;
}
static int udp_connect(socket_t *socket, sockaddr_t *addr, uintmax_t flags, cred_t *cred) {
udpsocket_t *udpsocket = (udpsocket_t *)socket;
int error = 0;
MUTEX_ACQUIRE(&socket->mutex);
if (udpsocket->peerport) {
error = EISCONN;
goto cleanup;
}
udpsocket->peerport = addr->ipv4addr.port;
udpsocket->peeraddress = addr->ipv4addr.addr;
cleanup:
MUTEX_RELEASE(&socket->mutex);
return error;
}
static socketops_t socketops = {
.bind = udp_bind,
.send = udp_send,
@@ -360,7 +378,8 @@ static socketops_t socketops = {
.poll = udp_poll,
.getname = udp_getname,
.getpeername = udp_getpeername,
.shutdown = udp_shutdown
.shutdown = udp_shutdown,
.connect = udp_connect
};
socket_t *udp_createsocket() {