473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

migrating recvmsg() to recvfrom()

Hi group,

I am trying to port a reliable multicast framework for UNIX to Win32 and so
far so good, but I stumbled on the next problem:

in the original code, the programmers use the recvmsg() function and as far
as I know, there is no Windows-version of this function. So I guess I will
have to translate it to a recvfrom(), but that gives some problems.

First of all, in the man pages I read the following:

<man recvfrom>
int recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);

int recvmsg(int s, struct msghdr *msg, int flags);

DESCRIPTION

[...]

The recvmsg call uses a msghdr structure to minimize the
number of directly supplied parameters. This structure
has the following form, as defined in <sys/socket.h>:

struct msghdr {
void * msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec * msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void * msg_control; /* ancillary data, see below
*/
socklen_t msg_controllen; /* ancillary data buffer len
*/
int msg_flags; /* flags on received message
*/
};

[...]

The messages are of the form:

struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including hdr
*/
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by
u_char cmsg_data[]; */
};

Ancillary data should only be accessed by the macros
defined in cmsg(3).

[...]

</man recvfrom>
So how do I translate it then?

if the original code is

bytes_read=recv msg(sd, &msg, 0);

I think it should be the following:

bytes_read=recv msg(sd, &msg.msg_contro l->cmsg_data,
&msg.msg_contro l->cmsg_len, 0, (sockaddr *)&msg.msn_name ,&msg.msg_namel en);

anyone has got an idea if my way of thinking is ok here? imho, the framework
is poorly organized, so I don't find a direct way to test it for only this
purpose.

but in for example Cygwin, there is no "msg_contro l" in the msghdr-struct.
(/usr/include/cygwin/socket.h)

Due to absence of experience with msghdr, I have no idea if my translation
can work and I am looking for a second opinion or other ideas.

Many kind greetings and thanks in advance,

--wim

Jul 22 '05 #1
1 8268
Hi Group,

guess I found a solution, I'll post it right here just for the
threads-sake:

if you want to port a *nix-program that uses the recvmsg() and
sendmsg()-functions, you can migrate them by using the following code:

ssize_t fake_recvmsg(in t sd, struct msghdr *msg, int flags)
{
ssize_t bytes_read;
size_t expected_recv_s ize;
ssize_t left2move;
char *tmp_buf;
char *tmp;
int i;

assert(msg->msg_iov);

expected_recv_s ize = 0;
for(i = 0; i < msg->msg_iovlen; i++)
expected_recv_s ize += msg->msg_iov[i].iov_len;
tmp_buf = malloc(expected _recv_size);
if(!tmp_buf)
return -1;

left2move = bytes_read = recvfrom(sd,
tmp_buf,
expected_recv_s ize,
flags,
(struct sockaddr *)msg->msg_name,
&msg->msg_namelen
);

for(tmp = tmp_buf, i = 0; i < msg->msg_iovlen; i++)
{
if(left2move <= 0) break;
assert(msg->msg_iov[i].iov_base);
memcpy(
msg->msg_iov[i].iov_base,
tmp,
MIN(msg->msg_iov[i].iov_len,left2m ove)
);
left2move -= msg->msg_iov[i].iov_len;
tmp += msg->msg_iov[i].iov_len;
}

free(tmp_buf);

return bytes_read;
}

and


ssize_t fake_sendmsg(in t sd, struct msghdr *msg, int flags)
{
ssize_t bytes_send;
size_t expected_send_s ize;
size_t left2move;
char *tmp_buf;
char *tmp;
int i;

assert(msg->msg_iov);

expected_send_s ize = 0;
for(i = 0; i < msg->msg_iovlen; i++)
expected_send_s ize += msg->msg_iov[i].iov_len;
tmp_buf = malloc(expected _send_size);
if(!tmp_buf)
return -1;

for(tmp = tmp_buf, left2move = expected_send_s ize, i = 0; i <
msg->msg_iovlen; i++)
{
if(left2move <= 0) break;
assert(msg->msg_iov[i].iov_base);
memcpy(
tmp,
msg->msg_iov[i].iov_base,
MIN(msg->msg_iov[i].iov_len,left2m ove));
left2move -= msg->msg_iov[i].iov_len;
tmp += msg->msg_iov[i].iov_len;
}

bytes_send = sendto(sd,
tmp_buf,
expected_send_s ize,
flags,
(struct sockaddr *)msg->msg_name,
msg->msg_namelen
);

free(tmp_buf);

return bytes_send;
}
greetings and have a nice Sunday-evening,

--wim
-------------------------
Many thanks to Lev Walkin
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1314
by: Heiko Wundram | last post by:
Hi all! I've read up on unix domain sockets, and I've seen that you can send out of bound messages such as sending the processes credentials over the socket. Receiving this requires to call recvmsg, which seems to be unavailable on the standard Python socket type. How would you go about implementing this functionality? I don't think that a standard recv() does what I want...
2
3266
by: William | last post by:
I have declared the following struct: struct PropagateInfo { string type; // "registration", "gossip", "termination" int initiatePeerID; }; which I used as follows: PropagateInfo* initiatePeerInfo = new PropagateInfo; initiatePeerInfo->type = type; // type is set to "registration"
2
2068
by: D.Frangiskatos | last post by:
Hi, I have been working for a few months in project that deals raw sockets. However recently, and while trying to examine the contents of the buffer used in recvfrom i was a bit confused. The buffer was allocated using malloc as it can be seen next: do { ..............
9
8463
by: Omega | last post by:
I have a problem with recvfrom(). I get a message and no error occurs, but the struct sockaddr* from is not filled with sender's data. The client's source code: #include "reversi.h" ------------------------------------------------------------------- int main() { int socket_id = socket(PF_INET,SOCK_DGRAM,0); struct sockaddr_in adres;
1
6005
by: Jack | last post by:
Hi guys, I can't figure this out. rec = recvfrom(sdUDP, buf, BUFSIZE, 0, (struct sockaddr *)&connectChannel, &chanSizeUDP ); while(1){ if (rec 0){ snt = sendto(sdUDP, buf, rec, 0, (struct sockaddr *)&connectChannel, chanSizeUDP ); rec = recvfrom(sdUDP, buf, BUFSIZE, 0, (struct sockaddr
0
1142
by: Michael Bacarella | last post by:
Hi list, I'm 99% sure httplib is sporadically hanging when I call read() on the HTTPResponse object (r.status == 200). Evidence: straceing the process shows it's blocked in recvfrom() ls -la /proc/pid/fd shows the socket-id for recvfrom() file descriptor (3) lsof -n | grep socket-id shows the connection to the HTTP server (on
2
3153
by: lgwe | last post by:
I want to receive 200 udp datagrams. Each into a new data string. But I dont know how to do that, this is wrong: import socket s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(("",port)) i = 0 while i<200: data,addr = s.recvfrom(1024) i = +1
0
1227
by: Jeff | last post by:
Hi, Fairly new to python, messing with some socket and pcap sniffing and have come across the following issue while trying to do a pcap_loop (via pcapy http://oss.coresecurity.com/projects/pcapy.html). I believe i recall seeing similar stuff using other pcap libs with python in the past (such as pylibpcap) in a nutshell, blocking on a read using socket.recvfrom(buf), which strace shows me is sitting in recvfrom(2) allows ctrl-c to be...
2
5607
by: kardon33 | last post by:
Is there a difference between the way the function recvfrom and sendto use the address structure. When i use sento it works fine, but when I try and use recvfrom with the same variables it errors out with (bad address). Or is there a better way to receive packets from a socket that i just sent to. Theres some of my source code. struct sockaddr_in a;
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.