Retrieving source address in a recvmmsg UDP socket - sockets

I am receiving multiple messages through a socket using:
result = recvmmsg(socket_, &messages_[0], MAX_NUM_MSG, MSG_WAITFORONE, NULL);
And I want get the source address and port, but I am getting an error in the struct assignment when I try:
msg = &messages_[0];
***struct sockaddr *src = &msg->msg_hdr.msg_name;***
srcport = ntohs(src->sin_port);
srcaddr = ntohl(src->sin_addr.s_addr);
invalid conversion from ‘void**’ to ‘sockaddr*'

The recvmmsg system call is an extension of recvmsg. As described in recvmsg: The msg_name field points to a caller-allocated buffer that is used to return the source address
That means you should preallocate memory space for msg_name by yourself, and also you should specify msg_namelen, please try:
sockaddr_in addrs[MAX_NUM_MSG];
for (int i = 0; i < MAX_NUM_MSG; ++i) {
messages_[i].msg_hdr.msg_name = &addrs[i];
messages_[i].msg_hdr.msg_namelen = sizeof(sockaddr_in);
}
So that you can access address when you have at least one message by doing (Remember to use sockaddr_in but not sockaddr):
struct sockaddr_in *src = messages_[0].msg_hdr.msg_name;

Related

Linux socket hardware timestamping

I'm working on a project researching about network synchronisation. Since I want to achieve the best performance I'm trying to compare software timestamping results with hardware timestamping ones.
I have followed this previously commented issue: Linux kernel UDP reception timestamp but after several tests I got some problems when trying to get hardware reception timestamps.
My scenario is composed of 2 devices, a PC and a Gateworks Ventana board, both devices are supposed to be waiting for packets to be broadcasted in their network and timestamping their reception times, I have tried this code (some parts have been omitted):
int rc=1;
int flags;
flags = SOF_TIMESTAMPING_RX_HARDWARE
| SOF_TIMESTAMPING_RAW_HARDWARE;
rc = setsockopt(sock, SOL_SOCKET,SO_TIMESTAMPING, &flags, sizeof(flags));
rc = bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
struct msghdr msg;
struct iovec iov;
char pktbuf[2048];
char ctrl[CMSG_SPACE(sizeof(struct timespec))];
struct cmsghdr *cmsg = (struct cmsghdr *) &ctrl;
msg.msg_control = (char *) ctrl;
msg.msg_controllen = sizeof(ctrl);
msg.msg_name = &serv_addr;
msg.msg_namelen = sizeof(serv_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = pktbuf;
iov.iov_len = sizeof(pktbuf);
//struct timeval time_kernel, time_user;
//int timediff = 0;
FILE *f = fopen("server.csv", "w");
if (f == NULL) {
error("Error opening file!\n");
exit(1);
}
fprintf(f, "Time\n");
struct timespec ts;
int level, type;
int i;
for (i = 0; i < 10; i++) {
rc = recvmsg(sock, &msg, 0);
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg))
{
level = cmsg->cmsg_level;
type = cmsg->cmsg_type;
if (SOL_SOCKET == level && SO_TIMESTAMPING == type) {
//ts = (struct timespec *) CMSG_DATA(cmsg);
memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
printf("HW TIMESTAMP %ld.%09ld\n", (long)ts.tv_sec, (long)ts.tv_nsec);
}
}
}
printf("COMPLETED\n");
fclose(f);
close(sock);
return 0;
}
In both devices the output I get after receiving a packet:
HW TIMESTAMP 0.000000000
On the other hand if with the same code my flags are:
flags = SOF_TIMESTAMPING_RX_HARDWARE
| SOF_TIMESTAMPING_RX_SOFTWARE
| SOF_TIMESTAMPING_SOFTWARE;
I get proper timestamps:
HW TIMESTAMP 1551721801.970270543
However, they seem to be software-timestamping ones. What would be the correct solution / method to handle hardware timestamping for packets received?
First of all, use ethtool -T "your NIC" to make sure your hardware supports the hardware timestamping feature.
You need to explicitly tell the Linux to enable the hardware timestamping feature of your NIC. In order to to that, you need to have a ioctl() call.
What you have to do is to call it with SIOCSHWTSTAMP, which is a device request code to indicate which device you want to handle as well as what you want to do. For example, there is a code called CDROMSTOP to stop the cdrom drive.
You also need to use a ifreq struct to configure your NIC.
You need something like this:
struct ifreq ifconfig;
strncpy(config.ifr_name, "your NIC name", sizeof(ifconfig.ifr_name));
ioctl("your file descriptor" , SIOCSHWTSTAMP, &ifconfig);
Here are some pages that you can look up to:
ioctl manual page,
ifreq manual page,
Read part 3.

Retrieve server IP address using gethostbyname()

I'm trying to retrieve the server IP address using gethostbyname(required from me)
here is the part of the client application for connecting to the server
struct hostent *SN = gethostbyname(argv[1]);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
server_addr.sin_addr.s_addr = htonl(*SN->h_addr); //I have a doubt about this line
int serverfd=0;
serverfd = connect(SocketD, (struct sockaddr*) &server_addr,
sizeof(server_addr));
where argv[1] is the name of the server, currently localhost.
I tried also this line of code
server_addr.sin_addr = *(struct in_addr*)SN->h_addr;
Could anyone please tell me what is wrong with my code?

Sending Large files using TCP

My client program needs to send a large file to the server program. After the client connects to the server and the server accepts it, the clients specifies the name of the file which it would be sending. Now, the client needs to send the file using TCP.
I know that if the size of the file is small (less than 1k bytes?), I can send it directly using a single call to the "send" function in socket programming. However, does the same work if my file size is large, say about 100 MB? I want to know does "send" by itself handle the task of breaking the large data into packets and sending them reliably or should I be the one handling this?
Thanks.
I am trying something similar & My client code looks like this
static void send_file(char *ipAddress, char *filename)
{
struct sockaddr_in serverAddr;
int skt;
uint32_t addr_size;
uint32_t sz;
int32_t sent_bytes;
FILE *fp;
if ( inet_pton(AF_INET, ipAddress, &(serverAddr.sin_addr)) ){
skt = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
}
else {
inet_pton(AF_INET6, ipAddress, &(serverAddr.sin_addr));
skt = socket(PF_INET6, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET6;
}
serverAddr.sin_port = htons(7891);
memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero));
addr_size = sizeof(serverAddr);
connect(skt, (struct sockaddr *) &serverAddr, addr_size);
/*find file size*/
fp = fopen(filename, "r");
fseek(fp,0,SEEK_END);
sz = ftell(fp);
rewind(fp);
sent_bytes = send(skt, fp, sz, 0);
printf("sent %d bytes\n", sent_bytes);
fclose(fp);
}

Get ip address of client from the server

I'm trying to get the ip address of each of my clients that connect to my server. I save this into fields of a struct which I sent to a thread. I'm noticing that sometimes I get the right ip and sometimes the wrong one. My first peer to connect usually has an incorrect ip...
The problem is that inet_ntoa() returns a pointer to static memory that is overwritten each time you call inet_ntoa(). You need to make a copy of the data before calling inet_ntoa() again:
struct peerInfo{
char ip[16];
int socket;
};
while((newsockfd = accept(sockfd,(struct sockaddr *)&clt_addr, &addrlen)) > 0)
{
struct peerInfo *p = (struct peerInfo *) malloc(sizeof(struct peerInfo));
strncpy(p->ip, inet_ntoa(clt_addr.sin_addr), 16);
p->socket = newsockfd;
printf("A peer connection was accepted from %s:%hu\n", p->ip, ntohs(clt_addr.sin_port));
if (pthread_create(&thread_id , NULL, peer_handler, (void*)p) < 0)
{
syserr("could not create thread\n");
free(p);
return 1;
}
printf("Thread created for the peer.\n");
pthread_detach(thread_id);
}
if (newsockfd < 0)
{
syserr("Accept failed.\n");
}
From http://linux.die.net/man/3/inet_ntoa:
The inet_ntoa() function converts the Internet host address in, given
in network byte order, to a string in IPv4 dotted-decimal notation.
The string is returned in a statically allocated buffer, which
subsequent calls will overwrite.
Emphasis added.

local vs remote socket connections on iphone

I want to send OSC messages from iphone to another programme (max/msp) by creating and connecting to a udp socket. this works from the iphone simulator, i.e. when both apps are running on the same computer but not when i install the app on the phone itself.
I think the problem could be with specifying the IP of the remote computer. I am using the sockaddr_in struct to specify IP and port info. when i run the code in the simulator it is fine to specify the IP as INADDR_ANY:
sin_addr.s_addr = INADDR_ANY;
when i run it on the device i'm trying to convert my IP into a hexidecimal number and specifying that instead of INADDR_ANY. This doesn't work for either the simulator or the device.
The console shows that the the socket is connecting and sending data fine but the remote programme (max/msp) doesn't receive any data at all.
I have tried importing the right frameworks so that it should work on both device and simulator.
the full code follows:
import "UDPSocketCreate.h"
#implementation UDPSocketCreate
-(id)init
{
in_addr_t myAddress = 0xC0A80145;
if(self =[super init])
{
//addr is an instance variable of type struct sockaddr_in
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = PF_INET;
addr.sin_port = htons(3333);
addr.sin_addr.s_addr = myAddress;INADDR_ANY
connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
OSC_initBuffer(&myOSCBuff, sizeof(packetBuffer), packetBuffer);
NSString *address = #"/test";
const char *utf8Address = [address UTF8String];
int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);
}
return self;
}
CFSocketRef udpSocket;
// this method is called from app delegate after init
-(void)createUDPSocketRef
{
udpSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketWriteCallBack, myCallBack, NULL);
if(udpSocket == NULL)
{
NSLog(#"socket create failed");
return;
}
CFRunLoopSourceRef runLoopSrceRef = CFSocketCreateRunLoopSource(NULL, udpSocket, 1);
CFRunLoopRef rl = CFRunLoopGetCurrent();
CFRunLoopAddSource(rl, runLoopSrceRef, kCFRunLoopCommonModes);
}
// pressing a button on the UI triggers this method
-(void)bang
{
int myInt = 1;
int writeRestult = OSC_writeIntArg(&myOSCBuff, myInt);
int buffDoneResult;
if (buffDoneResult = OSC_isBufferDone(&myOSCBuff))
{
NSLog(#"valid message in buff");
char *pack = OSC_getPacket(&myOSCBuff);
int packSize = OSC_packetSize(&myOSCBuff);
CFDataRef OSCPacketWithAddressTest = CFDataCreate(NULL, pack, packSize);
CFSocketError sendError = CFSocketSendData(udpSocket, connectAddr, OSCPacketWithAddressTest, 30);
NSLog(#"send error: %d", sendError);
}
OSC_resetBuffer(&myOSCBuff);
NSString *address = #"/test";
const char *utf8Address = [address UTF8String];
int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);
}
#end
any help would be greatly appreciated
Change;
in_addr_t myAddress = 0xC0A80145
to
in_addr_t myAddress = inet_addr("192.168.1.2");
or whatever that IP is.
S.
Unless I misunderstood you are trying to connect with INADDR_ANY as server address. INADDR_ANY is only for listening server to tell the IP stack that it wants to listen on any network interface (versus a specific interface on a multi-homed machine.) The client needs explicit server address of the server to send packets to. Look into inet_pton function for how to convert IP address from character string to network representation.