Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add c++ wrapper for tcplistener #48140

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/core.pri
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ HEADERS += \
$$PWD/defercall.h \
$$PWD/socketnotifier.h \
$$PWD/eventloop.h \
$$PWD/tcplistener.h \
$$PWD/tcpstream.h \
$$PWD/logutil.h \
$$PWD/uuidutil.h \
$$PWD/zutil.h \
Expand All @@ -83,6 +85,8 @@ SOURCES += \
$$PWD/defercall.cpp \
$$PWD/socketnotifier.cpp \
$$PWD/eventloop.cpp \
$$PWD/tcplistener.cpp \
$$PWD/tcpstream.cpp \
$$PWD/logutil.cpp \
$$PWD/uuidutil.cpp \
$$PWD/zutil.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/core/coretests.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
int httpheaders_test(int argc, char **argv);
int jwt_test(int argc, char **argv);
int timer_test(int argc, char **argv);
int tcpstream_test(int argc, char **argv);
int eventloop_test(int argc, char **argv);
int defercall_test(int argc, char **argv);

Expand Down
10 changes: 10 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ mod tests {
unsafe { call_c_main(ffi::defercall_test, args) as u8 }
}

fn tcpstream_test(args: &[&OsStr]) -> u8 {
// SAFETY: safe to call
unsafe { call_c_main(ffi::tcpstream_test, args) as u8 }
}

fn eventloop_test(args: &[&OsStr]) -> u8 {
// SAFETY: safe to call
unsafe { call_c_main(ffi::eventloop_test, args) as u8 }
Expand All @@ -150,6 +155,11 @@ mod tests {
assert!(qtest::run(defercall_test));
}

#[test]
fn tcpstream() {
assert!(qtest::run(tcpstream_test));
}

#[test]
fn eventloop() {
assert!(qtest::run(eventloop_test));
Expand Down
50 changes: 47 additions & 3 deletions src/core/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,21 +764,32 @@ mod ffi {

#[no_mangle]
pub extern "C" fn tcp_listener_bind(
addr: *const c_char,
ip: *const c_char,
port: u16,
out_errno: *mut c_int,
) -> *mut TcpListener {
assert!(!out_errno.is_null());

let addr = unsafe { CStr::from_ptr(addr) };
let ip = unsafe { CStr::from_ptr(ip) };

let addr = match addr.to_str() {
let ip = match ip.to_str() {
Ok(s) => s,
Err(_) => {
unsafe { out_errno.write(libc::EINVAL) };
return ptr::null_mut();
}
};

let ip: std::net::IpAddr = match ip.parse() {
Ok(ip) => ip,
Err(_) => {
unsafe { out_errno.write(libc::EINVAL) };
return ptr::null_mut();
}
};

let addr = std::net::SocketAddr::new(ip, port);

let l = match std::net::TcpListener::bind(addr) {
Ok(l) => l,
Err(e) => {
Expand All @@ -805,6 +816,39 @@ mod ffi {
}
}

#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn tcp_listener_local_addr(
l: *const TcpListener,
out_ip: *mut c_char,
out_ip_size: *mut libc::size_t,
out_port: *mut u16,
) -> c_int {
let l = l.as_ref().unwrap();
let out_ip_size = out_ip_size.as_mut().unwrap();
assert!(!out_port.is_null());

let addr = match l.0.local_addr() {
Ok(addr) => addr,
Err(_) => return -1,
};

let ip = addr.ip().to_string();

if ip.len() > *out_ip_size {
// if value doesn't fit, return success with empty value
*out_ip_size = 0;
return 0;
}

ptr::copy(ip.as_bytes().as_ptr() as *const c_char, out_ip, ip.len());
*out_ip_size = ip.len();

out_port.write(addr.port());

0
}

#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn tcp_listener_as_raw_fd(l: *const TcpListener) -> c_int {
Expand Down
89 changes: 89 additions & 0 deletions src/core/tcplistener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "tcplistener.h"

#include "socketnotifier.h"

TcpListener::TcpListener() :
inner_(nullptr)
{
}

TcpListener::~TcpListener()
{
reset();
}

bool TcpListener::bind(const QHostAddress &addr, quint16 port)
{
reset();

QByteArray ip = addr.toString().toUtf8();

int e;
inner_ = ffi::tcp_listener_bind(ip.data(), port, &e);
if(!inner_)
return false;

int fd = ffi::tcp_listener_as_raw_fd(inner_);

sn_ = std::make_unique<SocketNotifier>(fd, SocketNotifier::Read);
sn_->activated.connect(boost::bind(&TcpListener::sn_activated, this));
sn_->setEnabled(true);

return true;
}

std::tuple<QHostAddress, quint16> TcpListener::localAddress() const
{
QByteArray ip(256, 0);
size_t ip_size = ip.size();
quint16 port;
if(ffi::tcp_listener_local_addr(inner_, ip.data(), &ip_size, &port) != 0)
return {QHostAddress(), 0};

ip.resize(ip_size);
QHostAddress addr(QString::fromUtf8(ip));

return {addr, port};
}

std::unique_ptr<TcpStream> TcpListener::accept()
{
int e;
ffi::TcpStream *s_inner = ffi::tcp_listener_accept(inner_, &e);
if(!s_inner)
return std::unique_ptr<TcpStream>(); // null

TcpStream *s = new TcpStream;
s->inner_ = s_inner;

return std::unique_ptr<TcpStream>(s);
}

void TcpListener::reset()
{
sn_.reset();

ffi::tcp_listener_destroy(inner_);
inner_ = nullptr;
}

void TcpListener::sn_activated()
{
streamsReady();
}
49 changes: 49 additions & 0 deletions src/core/tcplistener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TCPLISTENER_H
#define TCPLISTENER_H

#include <memory>
#include <QtGlobal>
#include <QHostAddress>
#include <boost/signals2.hpp>
#include "rust/bindings.h"
#include "tcpstream.h"

class SocketNotifier;

class TcpListener
{
public:
TcpListener();
~TcpListener();

bool bind(const QHostAddress &addr, quint16 port);
std::tuple<QHostAddress, quint16> localAddress() const;
std::unique_ptr<TcpStream> accept();

boost::signals2::signal<void()> streamsReady;

private:
void reset();
void sn_activated();

ffi::TcpListener *inner_;
std::unique_ptr<SocketNotifier> sn_;
};

#endif
27 changes: 27 additions & 0 deletions src/core/tcpstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "tcpstream.h"

TcpStream::TcpStream() :
inner_(nullptr)
{
}

TcpStream::~TcpStream()
{
ffi::tcp_stream_destroy(inner_);
}
35 changes: 35 additions & 0 deletions src/core/tcpstream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2025 Fastly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TCPSTREAM_H
#define TCPSTREAM_H

#include "rust/bindings.h"

class TcpStream
{
public:
~TcpStream();

private:
friend class TcpListener;

ffi::TcpStream *inner_;

TcpStream();
};

#endif
Loading
Loading