sceNetInetConnect (PspPet please help!)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Noobacide
Posts: 4
Joined: Sun Jul 03, 2005 12:11 pm

sceNetInetConnect (PspPet please help!)

Post by Noobacide »

I'm having trouble with getting sceNetInetConnect() to work, either I'm calling it wrong or sony messed with sockaddr_in structure for the psp.

Code: Select all

struct sockaddr_in {
        unsigned short sin_family; // REVIEW: is this correct ?
        unsigned short sin_port; // use htons()
        unsigned char sin_addr[4];
        char    sin_zero[8];
};

int sceNetInetConnect(SOCKET s, const void *name, int namelen);

int ConnectToTcpAddr (unsigned long host, int port) {
    int sock, err;
    struct sockaddr_in addr;

    _memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    *(u32*)addr.sin_addr = htonl(host);

    sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
    if&#40;sock < 0&#41; &#123;
        printf&#40;"Socket error %i\n", sceNetInetGetErrno&#40;&#41;&#41;;
    &#125; else &#123;
        printf&#40;"Socket %i ok\n", sock&#41;;
    &#125;

    err = sceNetInetConnect&#40;sock, &addr, sizeof&#40;addr&#41;&#41;;
    if &#40;err < 0&#41; &#123;
        printf&#40;"Connect error %i\n", sceNetInetGetErrno&#40;&#41;&#41;;
    &#125; else &#123;
        printf&#40;"Connect ok\n"&#41;;
    &#125;

    return sock;
&#125;
what I get when calling ConnectToTcpAddr(0xc0a80001, 5900):
(192.168.0.1)

Code: Select all

Socket 8 ok
Connect error 106
since from what I've read, sony uses newlib, I looked up the error code in newlib's errno.h and i find 106 = EAFNOSUPPORT

which doesnt make much sense to me unless the prototype for sceNetInetConnect is wrong or the struct for sockaddr_in is wrong
pspkrazy
Posts: 49
Joined: Mon Jul 04, 2005 1:31 am

the same

Post by pspkrazy »

I've played a little with psppet's sample in a hope of making a little ftp server.

I've got the same problems with sceNetInetConnect.

So i decided to reverse the situation (PASV) and to open another socket on the psp (bind, listen, accept...).

It didn't work.

So in my mind, the problem is not connect or accept. Maybe security problems or threads... I don't know.

Why psppet can open a socket and i can't add another with the same code ? ;)

psppet pleaaaaaaaaaase !!!
PspPet
Posts: 210
Joined: Wed Mar 30, 2005 2:13 am
Contact:

Post by PspPet »

> I'm calling it wrong or sony messed with sockaddr_in structure for the psp.
You got it right - the Sony PSP structure is a little non-standard (hence the REVIEW comment). Extra byte out front (apparently unused, at least according to TwistedMetal game code)
Will be updating the sample and header later.

In the mean time, try this [also be sure the structure packs to a size of 16 bytes]. Should make sceNetInetConnect and other APIs work properly now (the old version worked somewhat accidentally)
----

Code: Select all

struct sockaddr_in &#123; 
	unsigned char sin_reserved; // PSP specific - ignored
	unsigned char sin_family; // usually AF_INET
        unsigned short sin_port; // use htons&#40;&#41; 
        unsigned char sin_addr&#91;4&#93;; 
        char    sin_zero&#91;8&#93;; 
&#125;; 
Lex
Posts: 27
Joined: Wed May 11, 2005 8:25 pm
Location: Germany

Post by Lex »

Maybe the unknown value is the socketaddr length ?
In BSD manuals I found:

The sockaddr_in structure, defined in <netinet/in.h>, is the IPv4 sockaddr
variant, and is as follows:

Code: Select all

struct sockaddr_in &#123;
  u_short      sin_family;
  u_short      sin_port;
  struct       in_addr sin_addr;
  char         sin_zero&#91;8&#93;;
&#125;;
If the compile-time option _SOCKADDR_LEN is defined before the sys/socket.h
header file is included, however, the 4.4BSD sockaddr_in structure is
defined, which is as follows:

Code: Select all

struct sockaddr_in &#123;
  u_char        sin_len;
  sa_family_t   sin_family;
  in_port_t     sin_port;
  struct        in_addr sin_addr;
  char          sin_zero&#91;8&#93;;
&#125;;
pspkrazy
Posts: 49
Joined: Mon Jul 04, 2005 1:31 am

thanks

Post by pspkrazy »

It seems that the modification of the struct changed the behaviour of the connect function.

Don't know actually if it works or not.

Thanx psppet for your comment. You're really the king of psp's net.

Will you code socket.h ?


Keep the good work.
Noobacide
Posts: 4
Joined: Sun Jul 03, 2005 12:11 pm

Post by Noobacide »

ok, I've modified my headers to match. but now instead of connect() returning and error, it never returns :(

not even throwing an exception.
pspkrazy
Posts: 49
Joined: Mon Jul 04, 2005 1:31 am

works

Post by pspkrazy »

it does work.

If your connect doesn't return it is because it is trying to connect somewhere.

Maybe the inet address is not ok.

You don't have to htonl it.

Hope it helps
PspPet
Posts: 210
Joined: Wed Mar 30, 2005 2:13 am
Contact:

Post by PspPet »

> Maybe the unknown value is the socketaddr length ?
That's my guess too -- as a place-holder. But it doesn't appear to be set or checked. [ie. you should be able to ignore it, and with the new alignment many more 'socket.h' APIs should work]
The true length of the structure (16 bytes) is passed the system APIs.

> Will you code socket.h ?
Yes there will be a socket.h work-alike

Code: Select all

#define socket sceNetInetSocket
#define bind sceNetInetBind
...
> You're really the king of psp's net.
FWIW: Actually it was embarrasingly easy. The big stumbling block was the lazy import problem
Having the SHA1 generated names for disassembling the system and programs makes it very easy!

> ok, I've modified my headers to match. but now instead of connect() returning and error, it never returns :(
As mentioned, you probably aren't connecting to a true IP address. If it doesn't find the target IP address, it doesn't return. If it does find it but can't open the specified port, it should return an error.
The error recovery is still minimal.
Noobacide
Posts: 4
Joined: Sun Jul 03, 2005 12:11 pm

Post by Noobacide »

thanks everyone, I managed to get it to work

heres a screenshot i'm sure everyone will enjoy
(Note, its running on my linuxbox, then being exported to the psp with VNC)

http://www.xboxopensource.com/upload/NH ... irefox.jpg
pspkrazy
Posts: 49
Joined: Mon Jul 04, 2005 1:31 am

great !!!

Post by pspkrazy »

A psp vnc client !!!

Great job ;)

WAR driving ? ;)
flyingtux
Posts: 1
Joined: Wed Oct 19, 2005 9:09 pm

Post by flyingtux »

hi,

I'm a newbie in pspdev and would like to port a socket based lib named ivybus on psp
I've installed pspsdk but doen't find any sceNetInet headers or lib inside

Could you explain if there is no headers yet , how to compile and link socket based program with the pspsdk ;-)


Regards,

Flyingtux
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

You can't as there is no network support in PSPSDK.
PspPet
Posts: 210
Joined: Wed Mar 30, 2005 2:13 am
Contact:

Post by PspPet »

> I've installed pspsdk but doen't find any sceNetInet headers or lib inside
There is currently a sample layered on top of the PSPSDK. Most of the WiFi programs have been created based on that.
Creating a general purpose lib may be a little tricky. See the WiFi .02 sample (it requires specialized startup):

http://www.aibohack.com/psp/wifitest.htm
[BTW: domain has been flakey if so -> http://66.70.136.74/psp/wifitest.htm ]
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

It is worth pointing out that psplink allows you to run normal user mode apps which allows the net libs to link in correctly, I am too lazy to actually write a full blown net test app so I have only tried sceNetInit which works fine ;) Something like this would at least reduce the burden on the normal applications programmer as you could just link in the libs and it _should_ work.

That said it is still not a general solution, for a start it requires you to have psplink, I will at some point get around to implementing psplink to act as a simple bootstrap for your app so you don't actually need things like a sio cable to use it. Even that doesn't necessarily help, even with various kernel patches you still cannot load the encrypted net modules from flash as the kernel falls over, you either must load decrypted copies from ms or get psplink to pre-load them before you app. And then there is vsh mode...

Nothing is ever simple.
Post Reply