Author Topic: SOCK_RECVIP - Possible?  (Read 7333 times)

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
SOCK_RECVIP - Possible?
« on: 2010-Sep-14 »
Such a function would be really useful - i.e. the IP address of the sender of the last message you used SOCK_RECV on.
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SOCK_RECVIP - Possible?
« Reply #1 on: 2010-Sep-14 »
If you got the socket from SOCK_CONNECT, you know the IP beforehand - just remember it.
If you got it from SOCK_TCPACCEPT, the 2nd parameter has the IP - also, just remember it.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Re: SOCK_RECVIP - Possible?
« Reply #2 on: 2010-Sep-15 »
These commands will work for TCP, but not UDP.  If you need your program to respond to UDP messages, then there is currently no way to get the IP address of the sender.  I've brought this up before, and I'm simply embedding a string of IPs at the start of messages, but it would be easier to have something like SOCK_RECVIP, and this would allow GLBasic programs to communicate with others that send UDP messages.  Although this isn't terribly necessary, it would be nice to have it.  I'm not sure about how hard it is to implement though, and therefore if it's worth it.
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SOCK_RECVIP - Possible?
« Reply #3 on: 2010-Sep-15 »
I think there is no way to tell where a udp message came from.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Re: SOCK_RECVIP - Possible?
« Reply #4 on: 2010-Sep-17 »
Exactly - surely the IP address of the sender must be in the header somewhere?
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SOCK_RECVIP - Possible?
« Reply #5 on: 2010-Sep-17 »
there's no header IIRC. You can browse the web to see if you find something usefull. I can implement that.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1751
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #6 on: 2010-Sep-17 »
This one has some related stuff too:

http://gafferongames.com/networking-for-game-programmers/sending-and-receiving-packets/

And yes it should be possible to see where a packet came from even with UDP.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Re: SOCK_RECVIP - Possible?
« Reply #7 on: 2010-Sep-18 »
I've looked up stuff regarding winsock.  I'm not sure what is used internally for networking in GLBasic, but Winsock seems to output a structure after receiving a UDP message which contains info (the 'recvfrom' structure is a 'sockaddr' structure, see http://msdn.microsoft.com/en-us/library/ms740496%28v=VS.85%29.aspx, which contains the IP address).

The 'recvfrom' function seems to be used in UDP (it says it is used for connectionless receiving) - see http://msdn.microsoft.com/en-us/library/ms740120%28v=VS.85%29.aspx
This function outputs a sockaddr structure:
Code: GLBasic [Select]
int recvfrom(
  __in         SOCKET s,
  __out        char *buf,
  __in         int len,
  __in         int flags,
  __out        struct sockaddr *from,
  __inout_opt  int *fromlen
);

The 'recvfrom' function, or something similar seems to be used in GLBasic.  This function takes in a previously setup socket, a pointer to a buffer, a maximum length to read - sounds like the the GL function:
Code: GLBasic [Select]
SOCK_RECV(sock%,msg$,length%)

Anyway, if GLBasic does use Winsock, then the IP address from a received UDP message should be put in the specified 'sockaddr' structure.  However, I'm assuming it's probably a lot more complicated than that - Winsock doesn't exactly sound like a multiplatform thing  :D
I think that the majority of this post will probably be useless if GLBasic doesn't use winsock, and possibly if I've misunderstood stuff about winsock, but here are some links which should be relevant in any case:

The structure of 'pseudoheaders' - the source IP is included.  http://en.wikipedia.org/wiki/User_Datagram_Protocol#IPv4_PSEUDO-HEADER

If you scroll down to the section on UDP, you can see that there is a normal header, followed by the pseudoheader described in the wikipedia page (i think at least).  http://www.protocols.com/pbook/tcpip2.htm

Another link on the normal header and then IPv4 and IPv6 pseudoheaders.  http://www.networksorcery.com/enp/protocol/udp.htm

EDIT:  The links posted by Ocean and Moru also have this, I didn't notice until now :whistle:
« Last Edit: 2010-Sep-18 by Serpent »
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1751
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #8 on: 2010-Sep-19 »
Oh oh, I have always wanted ICMP, can we have that too while we are wishing? :-)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1751
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #9 on: 2010-Sep-19 »
A support application. Istead of spelling out IPCONFIG, PING and TRACERT on the phone to confused customers that has problems with internet, I tell them to doubleclick the help_me.exe and tell me the result :-)

It's getting increasingly hard to explain to customers how to get to cmd.exe and I'm a bit tired of spending 30 minutes on this with people that just wants me to "fix internet" :-)

Would also be nice to write my own pingplotter :-)

Sadly it's not possible to harvest MAC adresses past the router but would like that too, if anyone knows how? :-)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1751
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #10 on: 2010-Sep-19 »
Yes that's what I meant. But I have access to the right router for this need so that's not a problem, just need some time and access to the right computer. But still ICMP would be nice :-)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SOCK_RECVIP - Possible?
« Reply #11 on: 2010-Sep-20 »
OK, I'll try to implement that for UDP then.

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Re: SOCK_RECVIP - Possible?
« Reply #12 on: 2010-Sep-22 »
Thanks so much  :nw:
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1751
    • View Profile
    • Homepage
Re: SOCK_RECVIP - Possible?
« Reply #13 on: 2011-Mar-30 »
Is it possible to see the originating IP-number and port of a UDP-packet yet? :-)

It is of course possible to send the IP with the packet but if the local computer does not know the external IP? I don't want to use external services to find out the IP since these don't have to be working all the time...
« Last Edit: 2011-Mar-30 by Moru »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: SOCK_RECVIP - Possible?
« Reply #14 on: 2011-Mar-31 »
I have the code here. (commented :P)

Can you provide a quick UDP test example for me to test? Maybe one that receives from more PCs?