2020-01-26 14:44:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-15 11:41:10 +13:00
|
|
|
#include <LibCore/UDPSocket.h>
|
2020-01-26 14:44:24 +01:00
|
|
|
#include <errno.h>
|
2020-02-02 12:34:39 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
namespace Core {
|
2020-01-26 14:44:24 +01:00
|
|
|
|
2020-03-15 11:41:10 +13:00
|
|
|
UDPSocket::UDPSocket(int fd, Object* parent)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Socket(Socket::Type::UDP, parent)
|
2020-01-26 14:44:24 +01:00
|
|
|
{
|
2020-03-15 11:41:10 +13:00
|
|
|
// NOTE: This constructor is used by UDPServer::accept(), so the socket is already connected.
|
2020-01-26 14:44:24 +01:00
|
|
|
m_connected = true;
|
|
|
|
set_fd(fd);
|
2020-02-02 12:34:39 +01:00
|
|
|
set_mode(IODevice::ReadWrite);
|
2020-01-26 14:44:24 +01:00
|
|
|
set_error(0);
|
|
|
|
}
|
|
|
|
|
2020-03-15 11:41:10 +13:00
|
|
|
UDPSocket::UDPSocket(Object* parent)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Socket(Socket::Type::UDP, parent)
|
2020-01-26 14:44:24 +01:00
|
|
|
{
|
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
set_error(errno);
|
|
|
|
} else {
|
|
|
|
set_fd(fd);
|
2020-02-02 12:34:39 +01:00
|
|
|
set_mode(IODevice::ReadWrite);
|
2020-01-26 14:44:24 +01:00
|
|
|
set_error(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 11:41:10 +13:00
|
|
|
UDPSocket::~UDPSocket()
|
2020-01-26 14:44:24 +01:00
|
|
|
{
|
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
}
|