0

When I am connected to a network, I can send a struct rt_msghdr* to the kernel and receive a corresponding route. However, when I disconnect from the network and try to send the same route message header, the socket fails at send() with errno = ESRCH (3) No such process.

Generic code: (Testing on MacOS)

int sd = create_socket(AF_ROUTE, IPPROTO_RAW, AF_UNSPEC);

memset(&buffer[0], 0, buffer.size());
struct rt_msghdr* rmh = (struct rt_msghdr*)(&buffer[0]);
rmh->rtm_msglen       = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
rmh->rtm_version      = RTM_VERSION;
rmh->rtm_type         = RTM_GET;
rmh->rtm_addrs        = RTA_DST;
rmh->rtm_pid          = getpid();
rmh->rtm_seq          = 1;
struct sockaddr_in* sa_in = (struct sockaddr_in*)(rmh + 1);
sa_in->sin_len            = sizeof(struct sockaddr_in);
sa_in->sin_family         = AF_INET;

int status;
if ((status = send(sd, rmh, rmh->rtm_msglen, 0) < 0) {
    rmh->rtm_msglen =
        sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
    struct sockaddr_in6* sa_in6 = (struct sockaddr_in6*)(rmh + 1);
    memset(sa_in6, 0, sizeof(struct sockaddr_in6));
    sa_in6->sin6_len    = sizeof(struct sockaddr_in6);
    sa_in6->sin6_family = AF_INET6;
    if ((status = send(sd, rmh, rmh->rtm_msglen, 0) < 0) {
        // process errno
        // * issue happens here when not connected to a network
    }
}

// receive kernel response(s)
// process responses

close(sd);

I verified that the process ID (pid) is being set correctly in the header. netstat -nr returns the default route without issue when not connected to a network. I'm having trouble understanding why this code is dependent on network connectivity.

Results from netstat -nr:

# with a network connection (Internet)
Destination        Gateway     Flags          Refs      Use   Netif Expire
default            X.X.X.X     UGSc           58        0     en1

# without a network connection (Internet6)
Destination        Gateway            Flags         Netif Expire
default            fe80::%utun0       UGcI          utun0 
TekuConcept
  • 1,264
  • 1
  • 11
  • 21
  • I checked the send man page, is status = ENOTCONN? I dont know why search error would occur, wish i could be more helpful. Since you are using send i assume TCP and you bind the socket befor calling send/receive. Why not switch to UDP functions sendto/receivefrom? Normally I wouldnt post such an unhelpful comment but its been 2 hours and no one has commented so ehh, ill delete it if this question ever gets answered. – Bwebb Nov 30 '18 at 00:29
  • @Bwebb I’m using `IPPROTO_RAW` and communicating directly with the operating system kernel. No TCP or UDP is involved, nor is bind or connect necessary. – TekuConcept Dec 01 '18 at 01:42

0 Answers0