LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Retrieve client ip address and port number in kernel udp socket (https://www.linuxquestions.org/questions/linux-newbie-8/retrieve-client-ip-address-and-port-number-in-kernel-udp-socket-898872/)

yethish 08-23-2011 12:06 AM

Retrieve client ip address and port number in kernel udp socket
 
I am writing a simple client server udp program.

The server runs at the kernel level and client runs at
application level.

The client sends the request packet to server through
"sendto(server_sock, buf, BUFLEN, 0,
(struct sockaddr *)&server_addr, addr_len);"
function and the server at the kernel level receives the
message through "sock_recvmsg(server_socket, &msg, size, flags);".

my doubt is how to retrieve the client IP address and port number
at server side.

tbrand 09-06-2011 09:38 PM

Hi Yethish,

I'm sorry I didn't notice your question has not been answered yet. If you still need it, here is a suggestion:

Code:

#include <sys/socket.h>
#include <netdb.h>

...

  struct sockaddr_storage peer_addr;
  socklen_t peer_addr_len = sizeof(struct sockaddr_storage);
  char host[NI_MAXHOST], service[NI_MAXSERV];

...
  nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);

...

  rc = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);

...

  printf("Received %d bytes from %s:%s\n", nread, host, service);

I hope that helps.

yethish 09-08-2011 09:23 AM

Retrieve client ip address and port number in kernel udp socket
 
Hi tBrand,

Thanks for the response.

The code suggested by you is for kernel level or
application level ?

Because at kernel level we use sock_recvmsg() call
to receive the data, but in your code there is
recvfrom() call to receive the data.

I am looking for the solution at kernel level.

Regards
Yethish

chrism01 09-08-2011 08:11 PM

For a qn like this, I'd recommend asking the Mods via the Report button to move this to the programming forum.
You may also find this useful http://beej.us/guide/bgnet/. Not sure if it does the kernel level version.

tbrand 09-08-2011 09:59 PM

Sorry, Yethish, I didn't read your post carefully enough. I'll do a little experimenting and see if I can find something. I would like to know the answer myself.


All times are GMT -5. The time now is 02:51 AM.