Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:56:38+00:00 2026-06-05T10:56:38+00:00

I have project code which creates a UDP socket to receive multicast packets. The

  • 0

I have project code which creates a UDP socket to receive multicast packets. The code is portable for Linux and Solaris operating systems. An extension of my project is looking to retrieve the source IP address of the UDP packet when using the recvmsg() function. I had asked a fellow expert on the matter, and she mentioned that Linux appears to be able to provide the source IP address, but Solaris may not when using the recvmsg() function. So I pose the question here, am I able to retrieve the source IP address using recvmsg() on Solaris 10?

OS: Solaris 10, Sunstudio 12 cc (no U1 or U2).
Code base: C/C++

//Socket initially opened with the following options from a different function.
// This connects the socket to receive multicast:
setsockopt(data->fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
    (char *)&mregs, sizeof(mregs) ) < 0)

//A different function performs an infinite loop reading from the socket:

struct iovec vector;
vector.iov_base = buf;     //passed in param
vector.iov_len = len;      //passed in param
struct msghdr msg;
char    caddr[100] ;
msg.msg_name =  caddr;
msg.msg_namelen = 100;
msg.msg_iov = &vector;
msg.msg_iovlen = 1;
int flags = 0;

char controlBuffer[1024];
msg.msg_control = controlBuffer;
msg.msg_controllen = 1024;

bytes = ::recvmsg(data->fd, &msg, flags);

//removed error checking    

struct cmsghdr *cmsg;
struct in_pktinfo *dest_ip_ptr;
rrcp_u32_t dest_ip = 0;
cmsg = CMSG_FIRSTHDR(&msg);
for ( cmsg = CMSG_FIRSTHDR(&msg);
    cmsg != NULL;
    cmsg = CMSG_NXTHDR( &msg, cmsg ) )
{
    //if ( cmsg->cmsg_type == IPPROTO_IP && cmsg->cmsg_level == IP_PKTINFO )
    {
#ifdef Linux
        struct in_pktinfo *dest_ip_ptr = (struct in_pktinfo*)CMSG_DATA(cmsg);
        dest_ip = dest_ip_ptr->ipi_addr.s_addr;
#else
        //in_addr only has 1 address
        struct in_addr * dest_ip_ptr = (struct in_addr *)CMSG_DATA(cmsg);
        dest_ip = dest_ip_ptr->_S_un._S_addr;
#endif
    }
}
if( ipaddr ) 
    ipaddr->IP = dest_ip;

//according to the Linux article mentioned below, the caddr should have the source
//socket address.  In my case, the caddr field is not filled with any coherent data,
//so this does not seem to be the source address.  Then again, "source socket" could
//be the interface IP on the local machine, which isn't what I need.

I also saw the following articles, but they didn’t seem to answer my questions:
Get destination address of a received UDP packet, solution at: Get destination address of a received UDP packet

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:56:39+00:00Added an answer on June 5, 2026 at 10:56 am

    There are two problems: One is that you don’t use caddr anywhere and you don’t say what it is, so it’s hard to help you; The other (and likely the problem you’re having) is that the address you get from recvmsg is not a string.

    msg.msg_name should point to a struct sockaddr_in and msg.msg_namelen should sizeof(struct sockaddr_in). You then get the address from that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my project, I have created a code snippet which can be copied and
So in Code::Blocks in Ubuntu (latest). I have a project in which I load
I have a WPF project which compiles just fine. However, when I enable code
I have a class, which is part of a code library project that was
I have a master page in my asp.net MVC project, which has code like
I have my Fluent NHibernate setup code in a DataAccess project which is accessed
I have done a small project, which consists of 5 excel sheet in, code
For a project, I'm trying to send UDP packets from Linux kernel-space. I'm currently
I have a project with a huge amount of auto-generated code, which we build
I have simple test project which creates a frame and adds one JTextPane to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.