473,507 Members | 3,112 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=recvmsg(sd, &msg, 0);

I think it should be the following:

bytes_read=recvmsg(sd, &msg.msg_control->cmsg_data,
&msg.msg_control->cmsg_len, 0, (sockaddr *)&msg.msn_name,&msg.msg_namelen);

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_control" 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 8254
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(int sd, struct msghdr *msg, int flags)
{
ssize_t bytes_read;
size_t expected_recv_size;
ssize_t left2move;
char *tmp_buf;
char *tmp;
int i;

assert(msg->msg_iov);

expected_recv_size = 0;
for(i = 0; i < msg->msg_iovlen; i++)
expected_recv_size += 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_size,
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,left2move)
);
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(int sd, struct msghdr *msg, int flags)
{
ssize_t bytes_send;
size_t expected_send_size;
size_t left2move;
char *tmp_buf;
char *tmp;
int i;

assert(msg->msg_iov);

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

for(tmp = tmp_buf, left2move = expected_send_size, 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,left2move));
left2move -= msg->msg_iov[i].iov_len;
tmp += msg->msg_iov[i].iov_len;
}

bytes_send = sendto(sd,
tmp_buf,
expected_send_size,
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
1302
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...
2
3259
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*...
2
2059
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...
9
8442
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"...
1
5990
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...
0
1138
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...
2
3136
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)...
0
1218
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...
2
5594
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...
0
7109
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...
0
7313
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,...
1
7029
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...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
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...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
411
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...

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.