From: Mariano on
I have a socket-client application, on the server side I have a file
descriptor for client connection, now I've to know what is the client
name (otherwise IP address). I have tried to write a function, but ->
operator doesn't work. Someone know the solution???

void traccia_user(int fd)
{
int tmp_len;
struct sockaddr_in tmp;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
unsigned int addrlen=sizeof(tmp);
getpeername(fd,(struct sockaddr*)&tmp,&addrlen);
printf("Client IP: %s\nClient port: %d
\n",inet_ntoa(tmp.sin_addr),tmp.sin_port);
tmp_len = sizeof(tmp);
if (getnameinfo(tmp, tmp->tmp_len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
printf("host=%s, serv=%s", hbuf, sbuf);
}
From: Rainer Weikusat on
Mariano <mariano.calandra(a)gmail.com> writes:
> I have a socket-client application, on the server side I have a file
> descriptor for client connection, now I've to know what is the client
> name (otherwise IP address). I have tried to write a function, but ->
> operator doesn't work. Someone know the solution???

[...]

> int tmp_len;
> struct sockaddr_in tmp;

[...]

> tmp_len = sizeof(tmp);
> if (getnameinfo(tmp, tmp->tmp_len,

[...]

tmp_len is not a member of tmp, ie this should be tmp_len, not
tmp->tmp_len. Additionally, tmp is structure and not a pointer to a
structure an accessing its members would need to be done with the
..-operator, eg

tmp.sin_port = htons(0xdeaf);
From: Mariano on
On 30 Giu, 10:57, Rainer Weikusat <rweiku...(a)mssgmbh.com> wrote:
> Mariano <mariano.calan...(a)gmail.com> writes:
> > I have a socket-client application, on the server side I have a file
> > descriptor for client connection, now I've to know what is the client
> > name (otherwise IP address). I have tried to write a function, but ->
> > operator doesn't work. Someone know the solution???
>
> [...]
>
> > int tmp_len;
> > struct sockaddr_in tmp;
>
> [...]
>
> > tmp_len = sizeof(tmp);
> > if (getnameinfo(tmp, tmp->tmp_len,
>
> [...]
>
> tmp_len is not a member of tmp, ie this should be tmp_len, not
> tmp->tmp_len. Additionally, tmp is structure and not a pointer to a
> structure an accessing its members would need to be done with the
> .-operator, eg
>
> tmp.sin_port = htons(0xdeaf);

Using tmp_len:

s.c: In function ‘...’:
s.c:257: error: incompatible type for argument 1 of ‘getnameinfo’
s.c: In function ‘traccia_user’:
s.c:656: error: expected declaration or statement at end of input
From: Jens Thoms Toerring on
Mariano <mariano.calandra(a)gmail.com> wrote:
> On 30 Giu, 10:57, Rainer Weikusat <rweiku...(a)mssgmbh.com> wrote:
> > Mariano <mariano.calan...(a)gmail.com> writes:
> > > I have a socket-client application, on the server side I have a file
> > > descriptor for client connection, now I've to know what is the client
> > > name (otherwise IP address). I have tried to write a function, but ->
> > > operator doesn't work. Someone know the solution???
> >
> > [...]
> >
> > > int tmp_len;
> > > struct sockaddr_in tmp;
> >
> > [...]
> >
> > > tmp_len = sizeof(tmp);
> > > if (getnameinfo(tmp, tmp->tmp_len,
> >
> > [...]
> >
> > tmp_len is not a member of tmp, ie this should be tmp_len, not
> > tmp->tmp_len. Additionally, tmp is structure and not a pointer to a
> > structure an accessing its members would need to be done with the
> > .-operator, eg
> >
> > tmp.sin_port = htons(0xdeaf);

> Using tmp_len:

> s.c: In function '...':
> s.c:257: error: incompatible type for argument 1 of 'getnameinfo'
> s.c: In function 'traccia_user':
> s.c:656: error: expected declaration or statement at end of input

Then take another look at the first argument you pass to getnameinfo(),
the compiler told you that there's another problem:

struct sockaddr_in tmp;
...
if (getnameinfo(tmp, tmp->tmp_len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)

and look up what getnameinfo() expects as it's first argument

int getnameinfo( const struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen,
char *serv, size_t servlen, int flags );

getnameinfo() expects a pointer to a 'struct sockaddr' but you
try to pass it the whole structure itself instead of its address.
Try instead

if ( getnameinfo( &tmp, tmp_len,
hbuf, sizeof hbuf,
sbuf, sizeof sbuf,
NI_NUMERICHOST | NI_NUMERICSERV ) == 0 )

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de