2020-01-26 10:44:24 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-26 10:44:24 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-03-14 19:41:10 -03:00
|
|
|
#include <LibCore/UDPSocket.h>
|
2020-01-26 10:44:24 -03:00
|
|
|
#include <errno.h>
|
2020-02-02 08:34:39 -03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
2020-05-23 10:31:30 -03:00
|
|
|
#ifndef SOCK_NONBLOCK
|
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
2020-01-26 10:44:24 -03:00
|
|
|
|
2020-03-14 19:41:10 -03:00
|
|
|
UDPSocket::UDPSocket(Object* parent)
|
2020-02-02 08:34:39 -03:00
|
|
|
: Socket(Socket::Type::UDP, parent)
|
2020-01-26 10:44:24 -03:00
|
|
|
{
|
2020-05-23 10:31:30 -03:00
|
|
|
#ifdef SOCK_NONBLOCK
|
2020-01-26 10:44:24 -03:00
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
|
2020-05-23 10:31:30 -03:00
|
|
|
#else
|
|
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
int option = 1;
|
|
|
|
|
ioctl(fd, FIONBIO, &option);
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-01-26 10:44:24 -03:00
|
|
|
if (fd < 0) {
|
|
|
|
|
set_error(errno);
|
|
|
|
|
} else {
|
|
|
|
|
set_fd(fd);
|
2021-05-12 06:26:43 -03:00
|
|
|
set_mode(OpenMode::ReadWrite);
|
2020-01-26 10:44:24 -03:00
|
|
|
set_error(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 19:41:10 -03:00
|
|
|
UDPSocket::~UDPSocket()
|
2020-01-26 10:44:24 -03:00
|
|
|
{
|
|
|
|
|
}
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|