Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e2b6ae18 authored by Krzysztof Kosiński's avatar Krzysztof Kosiński Committed by Automerger Merge Worker
Browse files

Merge "Refactor BindToDeviceSocketMutator." into udc-dev am: 50f5adc5

parents 17aea015 50f5adc5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ cc_defaults {
cc_library {
    name: "BindToDeviceSocketMutatorLib",
    vendor_available: true,
    srcs: ["src/*"],
    srcs: ["src/BindToDeviceSocketMutator.cpp"],
    export_include_dirs: ["include"],
    defaults: ["BindToDeviceSocketMutatorDefaults"],
}
+3 −12
Original line number Diff line number Diff line
@@ -16,20 +16,11 @@

#pragma once

#include <grpc++/grpc++.h>
#include <src/core/lib/iomgr/socket_mutator.h>
#include <string>
#include <grpc/grpc.h>
#include <string_view>

namespace android::hardware::automotive::remoteaccess {

class BindToDeviceSocketMutator final : public grpc_socket_mutator {
  public:
    BindToDeviceSocketMutator(const std::string_view& interface_name);

    bool mutateFd(int fd);

  private:
    std::string mIfname;
};
grpc_socket_mutator* MakeBindToDeviceSocketMutator(std::string_view interface_name);

}  // namespace android::hardware::automotive::remoteaccess
+28 −18
Original line number Diff line number Diff line
@@ -18,40 +18,50 @@

#include <android-base/logging.h>
#include <errno.h>
#include <src/core/lib/iomgr/socket_mutator.h>
#include <sys/socket.h>

#include <memory>

namespace android::hardware::automotive::remoteaccess {
namespace {

struct BindToDeviceMutator : grpc_socket_mutator {
    std::string ifname;
};

bool BindToDeviceSocketMutator::mutateFd(int fd) {
    int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, mIfname.c_str(), mIfname.size());
bool MutateFd(int fd, grpc_socket_mutator* mutator) {
    auto* bdm = static_cast<BindToDeviceMutator*>(mutator);
    int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bdm->ifname.c_str(), bdm->ifname.size());
    if (ret != 0) {
        PLOG(ERROR) << "Can't bind socket to interface " << mIfname;
        PLOG(ERROR) << "Can't bind socket to interface " << bdm->ifname;
        return false;
    }
    return true;
}

bool bind_to_device_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
    BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
    return bsm->mutateFd(fd);
}

int bind_to_device_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
int Compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
    return ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0));
}

void bind_to_device_mutator_destroy(grpc_socket_mutator* mutator) {
    BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
    delete bsm;
void Destroy(grpc_socket_mutator* mutator) {
    auto* bdm = static_cast<BindToDeviceMutator*>(mutator);
    delete bdm;
}

grpc_socket_mutator_vtable bind_to_device_mutator_vtable = {bind_to_device_mutator_mutate_fd,
                                                            bind_to_device_mutator_compare,
                                                            bind_to_device_mutator_destroy};
constexpr grpc_socket_mutator_vtable kMutatorVtable = {
        .mutate_fd = MutateFd,
        .compare = Compare,
        .destroy = Destroy,
};

}  // namespace

BindToDeviceSocketMutator::BindToDeviceSocketMutator(const std::string_view& interface_name) {
    mIfname = interface_name;
    grpc_socket_mutator_init(this, &bind_to_device_mutator_vtable);
grpc_socket_mutator* MakeBindToDeviceSocketMutator(std::string_view interface_name) {
    auto* bdm = new BindToDeviceMutator;
    grpc_socket_mutator_init(bdm, &kMutatorVtable);
    bdm->ifname = interface_name;
    return bdm;
}

}  // namespace android::hardware::automotive::remoteaccess
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ int main(int /* argc */, char* /* argv */[]) {

#ifdef GRPC_SERVICE_IFNAME
    grpcargs.SetSocketMutator(
            new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator(
            android::hardware::automotive::remoteaccess::MakeBindToDeviceSocketMutator(
                    GRPC_SERVICE_IFNAME));
    LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME;
    LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME;