hkb_dev
hkb_dev

Reputation: 379

How to use socket_t (socket reference) to get ip and port in Network Kernel Extension of Mac OS

I am writing the Socket filter in which most of call back functions for socket filtering(struct sflt_filter) have input parameter socket_t so as shown below:

   errno_t ( *sf_bind_func)(
   void *cookie,
   socket_t so,
   const struct sockaddr *to);

Need to get Port and IP from socket_t so. Is anyone have the idea how to do this?

Upvotes: 0

Views: 839

Answers (1)

hkb_dev
hkb_dev

Reputation: 379

Got the solution in OSx and iOS Kernel programming Book. function sock_getsockname() do the job. Find following code snippet:

    unsigned char addrStr[256];
    struct sockaddr_in addrLcl;
    sock_getsockname(so, (struct sockaddr *)&addrLcl, sizeof(addrLcl));
    inet_ntop(AF_INET, &addrLcl.sin_addr, (char *)addrStr, sizeof(addrStr));
    printf("SFWebSecBind() : <%s> Hex:<%X>", addrStr, ntohs(addrLcl.sin_port));

Upvotes: 1

Related Questions