SDLnet: get my own IP

Hi!

Maybe I did not find the function that I’m looking for, but I don’t think
so…

I want to get my local IP as a String…

I did the following:

char myIP[50];
char *localname;
IPaddress localIP;

 // get localhost-IP (127.0.0.1) in localIP

SDLNet_ResolveHost(&localIP, “localhost”, 0);
// get name of localhost
localname = SDLNet_ResolveIP(&localIP);
// get real IP of local host
SDLNet_ResolveHost(&localIP, localname, 0);
IP2String(&localIP, myIP);

Code of
IP2String(&localIP, myIP)
is at the end of text.
I’d like to have such one included in SDLnet!

I expected to find my IP in
char * myIP
at the end.
Using ping on the console (win98) worked
ping localhost
answer “myhostname” and 127.0.0.1
ping "hostname"
answer “myhostname” and 192.168.7.1

But in my code i get “NULL” in localname, maybe this is because i have a
computer name, but not a domain?

Why am I doing all this?
Well, i want to show the his ip to player with the server, so that he can
tell his “opponent” where he has to connect to.
But showing him 127.0.0.1 is a little bit senseless… :wink:

Thanx in Advance
Stiefel

BTW:
I really don’t understand the advantage of swapped bytes or nibbles…
Is the following function correct for all systems?

void IP2String(IPaddress *ipaddress, char *ipString)
{
Uint32 IP = ipaddress->host;
int hnibble, lnibble;
char part[5];

lnibble = IP % 16;
IP/=16;
hnibble = IP % 16;
IP/=16;
itoa(hnibble * 16 + lnibble, ipString, 10);
strcat(ipString,".");

lnibble = IP % 16;
IP/=16;
hnibble = IP % 16;
IP/=16;
itoa(hnibble * 16 + lnibble, part, 10);
strcat(ipString,part);
strcat(ipString,".");

lnibble = IP % 16;
IP/=16;
hnibble = IP % 16;
IP/=16;
itoa(hnibble * 16 + lnibble, part, 10);
strcat(ipString,part);
strcat(ipString,".");

lnibble = IP % 16;
IP/=16;
hnibble = IP % 16;
IP/=16;
itoa(hnibble * 16 + lnibble, part, 10);
strcat(ipString,part);
}

| But in my code i get “NULL” in localname, maybe this is because i have a
| computer name, but not a domain?

That method relies on you having your computer’s hostname in your
/etc/hosts file (or in a DNS), and that it maps to your network card. On
my machine (and most Linux machines), the hostname maps to 127.0.0.1, if
it exists at all. You can’t reliably work out your IP address from the
computer’s hostname.

I don’t know how you’d do it with SDL net, but what you need to do is
find out what their network card is (eth0, eth3, ppp0, sl0, etc) and
then work out what the IP address is.

Don’t forget you can have multiple network cards in your machine.
Imagine you have a PC with a network card and a modem. When you’re
online you have the local loopback - ‘lo’, your network card - 'eth0’
and your ppp connection - ‘ppp0’. Which one is the correct IP to show?
Is the user trying to play an Internet-wide game, or just one on their
local network, but they happen to be online checking their mail at
the same time?. So you need some way to work out which one is the
correct card to show the IP address of. You could maybe have a config
option to tell the game what network interface to listen on, then show
the IP address of that one.

Can you use the ‘normal’ network functions like gethostbyname() and
htons() with SDL_net?

An easier way is to just tell the user of the server to work it out for
themselves (using ‘ifconfig’ in Linux, ‘ipconfig’ in NT/2000, and
’winipcfg’ in win9x/me).

| I really don’t understand the advantage of swapped bytes or nibbles…

Because any computer can attach itself to a network, and different CPUs
order their bytes differently (Motorola and Intel are different. Intel
is little-endian, motorola is big-endian), there needs to be a standard
when they want to talk. This is called ‘network byte order’ and just
happens to be big-endian, and therefore the opposite to all Intel
machines. This is a slight pain, but there are C functions to swap the
bytes properly.On Thu, Mar 21, 2002 at 12:07:11AM +0100, Daniel Stiefelmaier wrote:


Quoting one is plagiarism. Quoting many is research.
6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

Hi!

So it looks like there is no way to get the users IP by SDLnet…
I’d really like to see this feature in a future version!
You could give a parameter to it which says you are intersested in eth0,
modem or s.t. else…

| I really don’t understand the advantage of swapped bytes or nibbles…

Because any computer can attach itself to a network, and different CPUs
order their bytes differently (Motorola and Intel are different. Intel
is little-endian, motorola is big-endian), there needs to be a standard
when they want to talk. This is called ‘network byte order’ and just
happens to be big-endian, and therefore the opposite to all Intel
machines. This is a slight pain, but there are C functions to swap the
bytes properly.

That was not my question…
Why don’t ALL CPUs use the “correct” order, where the MSB is on the left?
(This is “correct” in my eyes because you always read numbers from left to
right…)
If you read a Wave file for example, you may have to swap all bytes, and
before you can write it back you have do it again!
This is nonsense in my eyes…

That was not my question…
Why don’t ALL CPUs use the “correct” order, where the MSB is on the left?
(This is “correct” in my eyes because you always read numbers from left to
right…)
If you read a Wave file for example, you may have to swap all bytes, and
before you can write it back you have do it again!
This is nonsense in my eyes…

not everyone think MSB to the left is the correct order…

Hi!

So it looks like there is no way to get the users IP by SDLnet…
I’d really like to see this feature in a future version!
You could give a parameter to it which says you are intersested in eth0,
modem or s.t. else…

Which IP are you intereted in by default then? Are you interested in a
device IP by default (such as eth0, mode, etc) or a network by default (such
as the Internet, or one of the local internets). I don’t know that there is
a way you can always know what IP ties to the Internet though. Network
routers just don’t really even care about such things. They only care about
how to get packets to the destination that you want. Since the Internet is
likely to cover the most possible destination IP addresses, you could use
the default gateway, but if the computer isn’t even connected to the
Internet, the default gateway isn’t going to be to the Internet. This
probably doesn’t really matter for the case you are talking about. Maybe
what you really want is just the IP address for the default gateway, and
then a way to get all the other IP addresses available as well (in case they
want to play a lan game).

| I really don’t understand the advantage of swapped bytes or nibbles…

Because any computer can attach itself to a network, and different CPUs
order their bytes differently (Motorola and Intel are different. Intel
is little-endian, motorola is big-endian), there needs to be a standard
when they want to talk. This is called ‘network byte order’ and just
happens to be big-endian, and therefore the opposite to all Intel
machines. This is a slight pain, but there are C functions to swap the
bytes properly.

That was not my question…
Why don’t ALL CPUs use the “correct” order, where the MSB is on the left?
(This is “correct” in my eyes because you always read numbers from left to
right…)
If you read a Wave file for example, you may have to swap all bytes, and
before you can write it back you have do it again!
This is nonsense in my eyes…

Because electronic engineers, as usually, just decided to do things ‘their
own way’ and not standardize things. So you have some CPUs doing it one
way, and others doing it another. And now it’s far too late to change
things around anymore because everything relies on things being the way they
are.

As for what is more correct, it’s silly to say one way is more correct than
the other. It may seem more intuitive to you do use MSB first, however LSB
first has it’s advantages too. If you want to convert a 32 bit integer to
an 8 char byte, then you just take the byte at the memory location, instead
of having to look 3 bytes ahead in memory. From this perspective, MSB first
seems backwards.

As far as local data (reading and writing wave files in your example), you
can convert it all to the format of the machine ahead of time so you don’t
have to swap byte order later when your program runs if you want. After
all, your machine isn’t going to suddenly switch edianness.

-Jason

----- Original Message -----
From: mail@stiefelweb.de (Daniel Stiefelmaier)
To:
Sent: Wednesday, March 20, 2002 8:21 PM
Subject: Re: [SDL] SDLnet: get my own IP

personally, I favour MSB order, since humans use MSD also (Most Significant Decimal): If I want to write onehundred-eighty-three, I write the
digits in that order: 1, 8, 3, which makes 183. So, in our decimal digits standard, the most-significant-decimal is first too.

Just my, perhaps simple, thoughts on this issue :)>----- Original Message -----

From: “Daniel Stiefelmaier”
To:
Sent: Wednesday, March 20, 2002 8:21 PM
Subject: Re: [SDL] SDLnet: get my own IP

Hi!

So it looks like there is no way to get the users IP by SDLnet…
I’d really like to see this feature in a future version!
You could give a parameter to it which says you are intersested in eth0,
modem or s.t. else…

Which IP are you intereted in by default then? Are you interested in a
device IP by default (such as eth0, mode, etc) or a network by default (such
as the Internet, or one of the local internets). I don’t know that there is
a way you can always know what IP ties to the Internet though. Network
routers just don’t really even care about such things. They only care about
how to get packets to the destination that you want. Since the Internet is
likely to cover the most possible destination IP addresses, you could use
the default gateway, but if the computer isn’t even connected to the
Internet, the default gateway isn’t going to be to the Internet. This
probably doesn’t really matter for the case you are talking about. Maybe
what you really want is just the IP address for the default gateway, and
then a way to get all the other IP addresses available as well (in case they
want to play a lan game).

| I really don’t understand the advantage of swapped bytes or nibbles…

Because any computer can attach itself to a network, and different CPUs
order their bytes differently (Motorola and Intel are different. Intel
is little-endian, motorola is big-endian), there needs to be a standard
when they want to talk. This is called ‘network byte order’ and just
happens to be big-endian, and therefore the opposite to all Intel
machines. This is a slight pain, but there are C functions to swap the
bytes properly.

That was not my question…
Why don’t ALL CPUs use the “correct” order, where the MSB is on the left?
(This is “correct” in my eyes because you always read numbers from left to
right…)
If you read a Wave file for example, you may have to swap all bytes, and
before you can write it back you have do it again!
This is nonsense in my eyes…

Because electronic engineers, as usually, just decided to do things ‘their
own way’ and not standardize things. So you have some CPUs doing it one
way, and others doing it another. And now it’s far too late to change
things around anymore because everything relies on things being the way they
are.

As for what is more correct, it’s silly to say one way is more correct than
the other. It may seem more intuitive to you do use MSB first, however LSB
first has it’s advantages too. If you want to convert a 32 bit integer to
an 8 char byte, then you just take the byte at the memory location, instead
of having to look 3 bytes ahead in memory. From this perspective, MSB first
seems backwards.

As far as local data (reading and writing wave files in your example), you
can convert it all to the format of the machine ahead of time so you don’t
have to swap byte order later when your program runs if you want. After
all, your machine isn’t going to suddenly switch edianness.

-Jason


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Martijn Melenhorst wrote:

personally, I favour MSB order, since humans use MSD also (Most Significant Decimal): If I want to write onehundred-eighty-three, I write the
digits in that order: 1, 8, 3, which makes 183. So, in our decimal digits standard, the most-significant-decimal is first too.

Now think of people who write right-to-left, mostly arabic countries.

./lxnt

| Martijn Melenhorst wrote:
| >personally, I favour MSB order, since humans use MSD also (Most
| >Significant Decimal): If I want to write onehundred-eighty-three, I write
| >the digits in that order: 1, 8, 3, which makes 183. So, in our decimal
| >digits standard, the most-significant-decimal is first too.
|
| Now think of people who write right-to-left, mostly arabic countries.

And add to that the three ways of representing dates:

Europe: DD/MM/YYYY
US : MM/DD/YYYY
China : YYYY/MM/DD

I’m not that bothered about the byte sex of my machine (I’m sure
there’s perfectly logical reasons for either way), but it does get
confusing when you need to talk to something of the opposite ordering.On Thu, Mar 21, 2002 at 01:53:53PM +0300, Alexander Sabourenkov wrote:


I will stop talking about the twelve inch pianist
6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

James wrote:

I’m not that bothered about the byte sex of my machine (I’m sure
there’s perfectly logical reasons for either way), but it does get
confusing when you need to talk to something of the opposite ordering.

Yes it is.

But we’ve went way offtopic.

More or less original question was:

Why am I doing all this?
Well, i want to show the his ip to player with the server, so that he can
tell his “opponent” where he has to connect to.
But showing him 127.0.0.1 is a little bit senseless… :wink:

In the above i assume ‘his’ means server.

The only question remains is - how you are going to tell anything to player if
he is not connected to you.

If he had connected to you, then he already knows your IP address.

So where’s the point?

./lxnt

At 12:02 3/21/2002 +0000, you wrote:

And add to that the three ways of representing dates:

Europe: DD/MM/YYYY
US : MM/DD/YYYY
China : YYYY/MM/DD

Actually I always learned that Europe uses DD-MM-YYYY. (And that there’s no
such thing as DD/MM/YYYY) It’s a shame I see the confusing DD/MM/YYYY so
often nowadays :frowning:

Regards,
Dimitri

But china format is the right :

with YYYY/MM/DD, you can compare date with a simple string comparaison so
sorting date is only a matter of alphabetical sort …

LvR

-----Message d’origine-----De: Dimitri [mailto:dimitri at shortcut.nl]
Date: jeudi 21 mars 2002 13:45
?: sdl at libsdl.org
Objet: [OT] Re: [SDL] SDLnet: get my own IP

At 12:02 3/21/2002 +0000, you wrote:

And add to that the three ways of representing dates:

Europe: DD/MM/YYYY
US : MM/DD/YYYY
China : YYYY/MM/DD

Actually I always learned that Europe uses DD-MM-YYYY. (And that there’s no
such thing as DD/MM/YYYY) It’s a shame I see the confusing DD/MM/YYYY so
often nowadays :frowning:

Regards,
Dimitri


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

More or less original question was:

Why am I doing all this?
Well, i want to show the his ip to player with the server, so that he
can

tell his “opponent” where he has to connect to.
But showing him 127.0.0.1 is a little bit senseless… :wink:

In the above i assume ‘his’ means server.

Maybe it’s a server, maybe not. Could be a peer to peer system. By ‘his’,
I think he means exactly that, a person he wants to play against (or with?
Don’t know anything about the game so can’t really say). So he’s refering
to another human being, and not really a computer.

The only question remains is - how you are going to tell anything to
player if
he is not connected to you.

If he had connected to you, then he already knows your IP address.

So where’s the point?

You are assuming that ‘tell’ refers only to his game code communicating with
his opponent’s computer. However, that’s completely wrong I’m sure.
There’s many ways he could ‘tell’ his opponent where he has to connect to,
including IRC, ICQ, email, a telephone call, etc. Watch out for those
assumptions! :slight_smile:

-Jason

----- Original Message -----
From: lxnt@caravan.ru (Alexander Sabourenkov)
To:
Sent: Thursday, March 21, 2002 7:37 AM
Subject: Re: [SDL] SDLnet: get my own IP

At 14:08 3/21/2002 +0100, you wrote:

But china format is the right :

with YYYY/MM/DD, you can compare date with a simple string comparaison so
sorting date is only a matter of alphabetical sort …

Oow I didn’t say that the European format is the ‘right’ format,
only that DD/MM/YYYY is bullocks. I actually prefer DD ABC YYYY
personally :wink:

Regards,
Dimitri

You are assuming that ‘tell’ refers only to his game code communicating with
his opponent’s computer. However, that’s completely wrong I’m sure.
There’s many ways he could ‘tell’ his opponent where he has to connect to,
including IRC, ICQ, email, a telephone call, etc. Watch out for those
assumptions! :slight_smile:

Well, he states pretty clear that he wants “to show the his ip to player with the server”.

Can’t really imagine his server telling anything anyone w/o IP connectivity :).

I may have misread the post and he means client software telling its own IP to the
server, but that’s also utterly pointless, as server always can figure out the peer’s
address - that’s what src field is for in the IP packets after all.

And use of word ‘server’ excludes peer-to-peer stuff, for me at least.

Anyway this issue needs further clarification.

Daniel, if you’re still interested, please tell us more of what you are trying to do.

./lxnt

| >You are assuming that ‘tell’ refers only to his game code communicating
| >with
| >his opponent’s computer. However, that’s completely wrong I’m sure.
| >There’s many ways he could ‘tell’ his opponent where he has to connect to,
| >including IRC, ICQ, email, a telephone call, etc. Watch out for those
| >assumptions! :slight_smile:
|
|
| Well, he states pretty clear that he wants “to show the his ip to player
| with the server”.
|
| Can’t really imagine his server telling anything anyone w/o IP connectivity
| :).

The way I read it is the following:

The game runs in two modes : “client” and “server”.
You designate one PC to be a “server”.
All other PCs that want to take part in the game are “clients”.

I.e exactly the same way webserver/browsers work and Quake III works.

On the server PC you tell the game to run as a server. It then, to be
helpful to you, prints out “My IP address is x.x.x.x” so you can then
tell this to people who want to connect to you - just like there are
lists of Quake III servers announced on IRC channels, etc.On Thu, Mar 21, 2002 at 05:12:09PM +0300, Alexander Sabourenkov wrote:


I will remember to take my medication
6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

At 14:08 3/21/2002 +0100, you wrote:

But china format is the right :

with YYYY/MM/DD, you can compare date with a simple string comparaison so
sorting date is only a matter of alphabetical sort …

Oow I didn’t say that the European format is the ‘right’ format,
only that DD/MM/YYYY is bullocks. I actually prefer DD ABC YYYY
personally :wink:

Well, in Portugal we use DD/MM/YYYY and YYYY/MM/DD.
Portugal is an European country and has two official date formats.
So which one is right ? :slight_smile:


Paulo Pinto (aka Moondevil in demoscene)
pjmlp_pt at yahoo.comhttp://www.progtools.org
"If you think education is expensive, try ignorance" - Derek Bok


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

----- Original Message -----
From: dimitri@shortcut.nl (Dimitri)
To:
Sent: Thursday, March 21, 2002 1:43 PM
Subject: RE: [OT] Re: [SDL] SDLnet: get my own IP

[…endian…]

As far as local data (reading and writing wave files in your example),
you can convert it all to the format of the machine ahead of time so
you don’t have to swap byte order later when your program runs if you
want. After all, your machine isn’t going to suddenly switch
edianness.

AFAIK, it could, at least with certain non-intel CPUs. But that would
of course be a result of the OS doing something really stupid. :slight_smile:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 21 March 2002 07:45, Jason Hoffoss wrote:

BTW, we seem to use the chinese variant most of the time in Sweden…

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 21 March 2002 13:02, James wrote:

On Thu, Mar 21, 2002 at 01:53:53PM +0300, Alexander Sabourenkov wrote:
| Martijn Melenhorst wrote:
| >personally, I favour MSB order, since humans use MSD also (Most
| >Significant Decimal): If I want to write onehundred-eighty-three, I
| > write the digits in that order: 1, 8, 3, which makes 183. So, in
| > our decimal digits standard, the most-significant-decimal is first
| > too.
|
| Now think of people who write right-to-left, mostly arabic countries.

And add to that the three ways of representing dates:

Europe: DD/MM/YYYY
US : MM/DD/YYYY
China : YYYY/MM/DD

[…]

And use of word ‘server’ excludes peer-to-peer stuff, for me at least.

ICQ would be one example of a protocol where you can have both p2p and
client/server interaction going on…

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 21 March 2002 15:12, Alexander Sabourenkov wrote:

I’ve just wanted to say that the YYYY/MM/DD format is in my opinion the
right format from a point of view of developpers, just because it’s easier
to handle than any other format, but I don’t care if it’s not the official
format in Europe or elsewhere :wink:

-----Message d’origine-----De: Paulo Pinto [mailto:pjmlp_pt at yahoo.com]
Date: jeudi 21 mars 2002 16:23
?: sdl at libsdl.org
Objet: Re: [OT] Re: [SDL] SDLnet: get my own IP

----- Original Message -----
From: dimitri@shortcut.nl (Dimitri)
To:
Sent: Thursday, March 21, 2002 1:43 PM
Subject: RE: [OT] Re: [SDL] SDLnet: get my own IP

At 14:08 3/21/2002 +0100, you wrote:

But china format is the right :

with YYYY/MM/DD, you can compare date with a simple string comparaison so
sorting date is only a matter of alphabetical sort …

Oow I didn’t say that the European format is the ‘right’ format,
only that DD/MM/YYYY is bullocks. I actually prefer DD ABC YYYY
personally :wink:

Well, in Portugal we use DD/MM/YYYY and YYYY/MM/DD.
Portugal is an European country and has two official date formats.
So which one is right ? :slight_smile:


Paulo Pinto (aka Moondevil in demoscene)
pjmlp_pt at yahoo.comhttp://www.progtools.org
"If you think education is expensive, try ignorance" - Derek Bok


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl