getaddrinfo: wrong usage?...

Jan 26, 2010 at 11:08am
Hey...


i still have this two applications (a host and an client) using udp ports to send data between each other...

to resolve the static adress is got i did use getaddrinfo() to let it fill out my addrinfo-structure for me... but somehow it does not work the right way... if i go reverse the lookup-process usign getnameinfo() it does not resolve into the original hostname...

now that i tried this the old-fashion way by filling a sockaddr_in-struct by myself it works...

do i use the getaddrinfo()-func the wrong way or missunderstood something...
or does the lookupprocess by the api fail?...
may it be the internet-provider-reseller which interferes with the api?...

following the two ways i tried doing this from my client:

The_Working_One wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
HOSTENT* he;
	in_addr addr;

	he = gethostbyname("myAddress");

	addr.s_addr = *(u_long*)he->h_addr_list[0];

	cout << inet_ntoa(addr);

	SOCKET s = socket(AF_INET, SOCK_DGRAM,0);

	char buffer[200];

	strcpy_s(buffer, "TEST");

	sockaddr_in sa;
	ZeroMemory(&sa,sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(12345);

	memcpy(&(sa.sin_addr),he->h_addr_list[0],4);

	int rc = sendto(s,buffer,200,0,(SOCKADDR*)&sa,sizeof(sa));

	cout << "\n" << rc << " Error: " << WSAGetLastError();


The_Not_Working_One wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Get AddressInfo
	addrinfo hints, *p;
	ZeroMemory(&hints,sizeof(hints));

	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;

	rc = getaddrinfo(szHost,szPort,&hints,&g_pAddr);
	if(rc)
		cout << "Error3\n";

	char nodebuffer[100];
	char servicebuffer[100];
	rc = getnameinfo(g_pAddr->ai_addr,sizeof(sockaddr),nodebuffer,100,servicebuffer,100,0);

	cout << nodebuffer << ":" << servicebuffer << endl;

	rc = WSAGetLastError();
	cout << "Last WSA Error: " << rc << endl;

	p = g_pAddr;

	char buffer[220];
	strcpy_s(buffer,"Test Message");
	rc = sendto(g_Socket,buffer,200,0,p->ai_addr, p->ai_addrlen);

	cout << "Bytes sent: " << rc << endl;

	rc = WSAGetLastError();

	cout << "Last WSA Error: " << rc << endl;


btw: both calls to sendto() return 200 even tho the host does not recv anything while using the second solution... i guess its due to the un-realiability of the UDP-protocol.
Last edited on Jan 26, 2010 at 4:08pm
Jan 26, 2010 at 12:14pm
Are these addresses/names set up in your DNS?
Jan 26, 2010 at 2:44pm
I do not fully understand your question... but maybe u are asking for the following:

The domain name i am trying to forward lookup to an ip-adress is a static domain name i got from an DNS-service provider... my router logs in everytime my ip changes and tells them to update this... (no-ip.com is the DNS service provider)...

when i reverse thhis( lookup domain name from the ipaddress given by the getaddrinfo) the domain name is somethin like
[MYCITY]-5XXXXXX.pool.mediaWays.net
Last edited on Jan 26, 2010 at 4:42pm
Jan 26, 2010 at 4:10pm
the problem seems to come from the sockaddr- within the addrinfo-structure...

may anyone show me an example of how to convert the ip contained in it to an human readable format?... (the beej.us-example from page 17 does not work for me)...

thanks
Jan 26, 2010 at 7:49pm
These functions do DNS lookups via the redirector. They don't magically work out these details themselves.

Maybe this will help. http://en.wikipedia.org/wiki/Getaddrinfo
Last edited on Jan 26, 2010 at 7:53pm
Jan 26, 2010 at 8:17pm
I never did assume them to do some black magic^^...

sendto´s prototype:
1
2
 int sendto(int sockfd, const void *msg, int len, unsigned int flags,
           const struct sockaddr *to, socklen_t tolen); 



So I should be able to do the following, after I did call getaddrinfo to fill out my addrinfo (and yes, it did get me the right ip):

sendto(socket, text, textlen, 0, addrinfo->ai_addr,sizeof(ai_addr));

But it does not work...
Maybe I did not express myself the right way in post before... but this is the problem I experience.

edit: ok it does in some way work, beacause it does send the data... but not to me/my server...
Last edited on Jan 26, 2010 at 8:25pm
Jan 26, 2010 at 9:28pm
Where is g_Socket initialised?
You don't appear to have filled in hints.
Jan 26, 2010 at 9:39pm
g_Socket is initialized before that... as : g_Socket = socket(AF_INET, SOCK_DGRAM, 0);
hints are filled with zeroes and
1
2
hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;
Topic archived. No new replies allowed.