lkml.org 
[lkml]   [2001]   [Jul]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
Subjectrecvfrom and sockaddr_in.sin_port

hello,
I'm using recvfrom, and upon return it should fill in
from argument with sockaddr_in data structure. from
ip(7) man page I get that it has format of :

struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
u_int16_t sin_port; /* port in network byte order*/
struct in_addr sin_addr; /* internet address */
};

for PF_INET type of socket. Now if I run the program the, data in
the from field comes out as:

2 0 0 0 192 168 1 4 61 63 140 200 85 214 2

| | |

AF_INET :2
IP :192.168.1.4

and as show in above example and other repeated test, the port
part is always set to 0. Shouldn't that be set to port number?

on similar token the "padding" part after the IP is always set
to the same pattern. Shouldn't it rather be zeroed, or be
some random data?

I have attached a simple program I used for generating those
results.

--
Adam
http://www.eax.com The Supreme Headquarters of the 32 bit registers

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, char **argv) {

size_t buflen = 1024;
char buf[buflen];
struct sockaddr_in from;
socklen_t fromlen = sizeof(struct sockaddr_in);
int flags;

int rawsock;
int bytesread;

int i;
unsigned char * ptr;


rawsock = socket(PF_INET,SOCK_RAW,IPPROTO_TCP);

if (rawsock == -1)
perror("socket():");

flags = MSG_WAITALL;
memset(&from,0,fromlen);

bytesread = recvfrom(rawsock,&buf,buflen,flags,
(struct sockaddr*)(&from),&fromlen);

ptr = (unsigned char*)&from;
for(i=0;i<fromlen;i++) {
printf("%i ",*ptr);
ptr++;
}
printf("\n");

if (bytesread == -1) {
printf("err # : %i \n",errno);
perror("recvfrom():");
return -1;
}


if (argc ==2) {
struct hostent *host;

printf("msg len: %i \n",bytesread);
printf("frm len: %i \n",fromlen);
printf("IPv4 : type:%i port:%X addr:%X \n",
from.sin_family,
ntohs(from.sin_port),
from.sin_addr.s_addr);

host=gethostbyaddr(&(from.sin_addr.s_addr),
fromlen,from.sin_family);

if (host == NULL ) {
printf("err # : %i \n",h_errno);
herror("gethostbyaddr():");
}

printf("host: (");

ptr = (unsigned char*)(&(from.sin_addr.s_addr));
for(i=0;i<4;i++) {
printf("%i",*(ptr++));
if (i<3)
printf(".");
}

if (host == NULL)
printf(") \n");
else
printf(") %s \n",host->h_name);


}


return 0;

}
\
 
 \ /
  Last update: 2005-03-22 12:56    [W:0.048 / U:0.264 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site