Basilisk II PSP Port (Mac Emulator)

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

I hav done research on the forum about raw sockets but none of theme show concrete statements of why sockets dont work or even any example of codes that people have tried.

Mind posting what you have tried?
<a><img></a>
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Wally wrote:Well I think its time I started making a 5MB Shareware / Freeware pack for Basilisk PSP
That would be cool. :)
Gaby_64 wrote:I hav done research on the forum about raw sockets but none of theme show concrete statements of why sockets dont work or even any example of codes that people have tried.

Mind posting what you have tried?
No problem.

main.c

Code: Select all

#include <pspsdk.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <psppower.h>
#include <pspdisplay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pspsdk.h>
#include <psputility_netmodules.h>
#include <psputility_netparam.h>
#include <pspwlan.h>
#include <pspnet.h>
#include <pspnet_apctl.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>

#define printf pspDebugScreenPrintf


#define VERS    1
#define REVS    0


PSP_MODULE_INFO&#40;"RawSockets", 0, VERS, REVS&#41;;
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_USER&#41;;


int psp_net_error = 0;
int psp_net_available = 0;

char szMyIPAddr&#91;32&#93;;
char *psp_local_ip_addr = NULL;                     // String&#58; the local IP address for the PSP

int my_socket;
int kill_thread = 0;
int got_packet = 0;


/*
 * Network support code
 */

static int connect_to_apctl&#40;int config&#41; &#123;
    int err, i;
    int stateLast = -1;

	pspDebugScreenClear&#40;&#41;;

    if &#40;sceWlanGetSwitchState&#40;&#41; != 1&#41;
        pspDebugScreenPrintf&#40;"Please enable WLAN or press a button to procede without networking.\n"&#41;;
    for &#40;i=0; i<10; i++&#41;
    &#123;
        SceCtrlData pad;
        if &#40;sceWlanGetSwitchState&#40;&#41; == 1&#41;
            break;
        sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
        if &#40;pad.Buttons&#41;
            return 0;
	    pspDebugScreenPrintf&#40;"%d... ", 10-i&#41;;
        sceKernelDelayThread&#40;1000*1000&#41;;
    &#125;
    printf&#40;"\n"&#41;;
    if &#40;i == 10&#41;
        return 0;

    sceNetApctlDisconnect&#40;&#41;;
    err = sceNetApctlConnect&#40;config&#41;;
    if &#40;err != 0&#41; &#123;
        pspDebugScreenPrintf&#40;"sceNetApctlConnect returns %08X\n", err&#41;;
        return 0;
    &#125;

    pspDebugScreenPrintf&#40;"Connecting...\n"&#41;;
    // loop end&#58; 600 = 30 sec, 900 = 45 sec
    for &#40;i=0; i<900; i++&#41; &#123;
        int state;
        err = sceNetApctlGetState&#40;&state&#41;;
        if &#40;err != 0&#41; &#123;
            pspDebugScreenPrintf&#40;"sceNetApctlGetState returns $%x\n", err&#41;;
            break;
        &#125;
        if &#40;state != stateLast&#41; &#123;
            pspDebugScreenPrintf&#40;"  Connection state %d of 4.\n", state&#41;;
            stateLast = state;
        &#125;
        if &#40;state == 4&#41; &#123;
            break;
        &#125;
        sceKernelDelayThread&#40;50 * 1000&#41;;
    &#125;
    if &#40;i == 900 || err !=0&#41;
        return 0;

    pspDebugScreenPrintf&#40;"Connected!\n"&#41;;
    sceKernelDelayThread&#40;3*1000*1000&#41;;

    return 1;
&#125;

static int net_thread&#40;SceSize args, void *argp&#41;
&#123;
    int selComponent = 1; // access point selector

    pspDebugScreenPrintf&#40;"Using connection %d to connect...\n", selComponent&#41;;

    while &#40;psp_net_available != 1&#41;
    &#123;
        if &#40;connect_to_apctl&#40;selComponent&#41;&#41;
        &#123;
            if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41;
                strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
            pspDebugScreenPrintf&#40;"IP&#58; %s\n", szMyIPAddr&#41;;
            psp_net_available = 1;
        &#125;
        else
        &#123;
            int i;
            pspDebugScreenPrintf&#40;"\n\n Couldn't connect to access point.\n Press any key to try again.\n"&#41;;
            for &#40;i=0; i<10; i++&#41;
            &#123;
                SceCtrlData pad;
                sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
                if &#40;pad.Buttons&#41;
                    break;
                pspDebugScreenPrintf&#40;"%d... ", 10-i&#41;;
                sceKernelDelayThread&#40;1000*1000&#41;;
            &#125;
            pspDebugScreenPrintf&#40;"\n"&#41;;
            if &#40;i == 10&#41;
            &#123;
                psp_net_available = -1;
                return 0;
            &#125;
        &#125;
    &#125;

    return 0;
&#125;

static void psp_net_connect &#40;void&#41;
&#123;
	SceUID thid;

	thid = sceKernelCreateThread&#40;"net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL&#41;;
	if &#40;thid < 0&#41; &#123;
		pspDebugScreenPrintf&#40;"Could not create network thread!\n"&#41;;
        pspDebugScreenPrintf&#40;"\n"&#41;;
		sceKernelDelayThread&#40;4*1000*1000&#41;;
		return;
	&#125;
	sceKernelStartThread&#40;thid, 0, NULL&#41;;
	while &#40;1&#41;
	&#123;
		if &#40;psp_net_available&#41;
            break;
		sceKernelDelayThread&#40;1000*1000&#41;;
	&#125;
	if &#40;psp_net_available != 1&#41;
	&#123;
		pspDebugScreenPrintf&#40;"Could not connect to access point!\n"&#41;;
        pspDebugScreenPrintf&#40;"\n"&#41;;
		sceKernelDelayThread&#40;4*1000*1000&#41;;
		return;
	&#125;
	else
        psp_local_ip_addr = strdup&#40;szMyIPAddr&#41;;

	pspDebugScreenPrintf&#40;"Networking successfully started.\n"&#41;;
	sceKernelDelayThread&#40;4*1000*1000&#41;;
&#125;

static int InitialiseNetwork&#40;void&#41;
&#123;
  int err;

  pspDebugScreenPrintf&#40;"load network modules..."&#41;;
  err = sceUtilityLoadNetModule&#40;PSP_NET_MODULE_COMMON&#41;;
  if &#40;err != 0&#41;
  &#123;
    pspDebugScreenPrintf&#40;"Error, could not load PSP_NET_MODULE_COMMON %08X\n", err&#41;;
    return 1;
  &#125;
  err = sceUtilityLoadNetModule&#40;PSP_NET_MODULE_INET&#41;;
  if &#40;err != 0&#41;
  &#123;
    pspDebugScreenPrintf&#40;"Error, could not load PSP_NET_MODULE_INET %08X\n", err&#41;;
    return 1;
  &#125;
  printf&#40;"done\n"&#41;;

  err = pspSdkInetInit&#40;&#41;;
  if &#40;err != 0&#41;
  &#123;
    pspDebugScreenPrintf&#40;"Error, could not initialise the network %08X\n", err&#41;;
    return 1;
  &#125;
  return 0;
&#125;

static void psp_net_init&#40;void&#41;
&#123;
	if &#40;InitialiseNetwork&#40;&#41; != 0&#41;
	&#123;
		psp_net_error = 1;
		pspDebugScreenPrintf&#40;"Networking not available.\n"&#41;;
		sceKernelDelayThread&#40;4*1000*1000&#41;;
		return;
	&#125;
&#125;

/*
 *  Packet reception thread
 */

static int receive_proc&#40;SceSize args, void *argp&#41;
&#123;
	int fd = my_socket;

	while &#40;!kill_thread&#41;
	&#123;
		fd_set readfds;
		FD_ZERO&#40;&readfds&#41;;
		FD_SET&#40;fd, &readfds&#41;;
		struct timeval timeout;
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		if &#40;select&#40;fd+1, &readfds, NULL, NULL, &timeout&#41; > 0&#41;
			if &#40;FD_ISSET&#40;fd, &readfds&#41;&#41;
			&#123;
				printf&#40;" Packet received\n"&#41;;
				got_packet = 1;
				while &#40;got_packet&#41;
					sceKernelDelayThread&#40;100*1000&#41;;
			&#125;
	&#125;

	sceKernelExitDeleteThread&#40;0&#41;;
	return 0;
&#125;


/*
 *  Start packet reception thread
 */

int ether_start_pkt_thread&#40;void&#41;
&#123;
	SceUID read_thread = sceKernelCreateThread&#40;"Packet Receiver", receive_proc, 0x14, 0x1800, PSP_THREAD_ATTR_USER, NULL&#41;;
	if&#40;read_thread < 0&#41;
        return 0;
	sceKernelStartThread&#40;read_thread, 0, NULL&#41;;
	return 1;
&#125;

int main&#40;void&#41;
&#123;
	int on = 1;
	ssize_t length;
	struct sockaddr_in from;
	socklen_t from_len = sizeof&#40;from&#41;;
	char packet&#91;8192&#93;;
	SceCtrlData pad;

    pspDebugScreenInit&#40;&#41;;
    pspDebugScreenSetBackColor&#40;0xff000000&#41;;
    pspDebugScreenSetTextColor&#40;0xffffffff&#41;;
    pspDebugScreenClear&#40;&#41;;

    sceCtrlSetSamplingCycle&#40;0&#41;;
    sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_DIGITAL&#41;;

	psp_net_init&#40;&#41;;
	if &#40;!psp_net_error&#41;
	&#123;
		psp_net_connect&#40;&#41;;
		if &#40;psp_net_available&#41;
		&#123;
			int fd = socket &#40;PF_INET, SOCK_RAW, IPPROTO_RAW&#41;;
			if &#40;fd < 0&#41;
				printf &#40;"Couldn't open socket!\n"&#41;;
			else
			&#123;
				my_socket = fd;
				setsockopt&#40;fd, SOL_SOCKET, SO_NONBLOCK, &on, sizeof&#40;on&#41;&#41;;
				if &#40;ether_start_pkt_thread&#40;&#41;&#41;
				&#123;
					while &#40;1&#41;
					&#123;
						while &#40;!got_packet&#41;
						&#123;
							sceKernelDelayThread&#40;100*1000&#41;;
							sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
							if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;
								break;
						&#125;

						length = recvfrom&#40;fd, packet, 1514, 0, &#40;struct sockaddr *&#41;&from, &from_len&#41;;
						if &#40;length >= 14&#41;
						&#123;
							printf&#40;"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
							packet&#91;0&#93;, packet&#91;1&#93;, packet&#91;2&#93;, packet&#91;3&#93;, packet&#91;4&#93;, packet&#91;5&#93;,
							packet&#91;6&#93;, packet&#91;7&#93;, packet&#91;8&#93;, packet&#91;9&#93;, packet&#91;10&#93;, packet&#91;11&#93;,
							packet&#91;12&#93;, packet&#91;13&#93;&#41;;
						&#125;
						else
							printf&#40;"Packet length &#40;%d&#41; too small.\n", length&#41;;
						got_packet = 0;
						sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
						if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;
							break;
					&#125;
					kill_thread = 1;
					sceKernelDelayThread&#40;100*1000&#41;;
				&#125;
				else
					printf&#40;"Couldn't start packet receiver!\n"&#41;;
			&#125;
		&#125;
	&#125;

    sceKernelDelayThread&#40;3*1000*1000&#41;;
    sceKernelExitGame&#40;&#41;;

    return 0;   /* never reaches here, again, just to suppress warning */
&#125;
makefile

Code: Select all

TARGET = RawSockets
OBJS = main.o

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

BUILD_PRX = 1
PSP_FW_VERSION = 371

LIBDIR =
LIBS = -lpspwlan
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Raw Sockets Test

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
It doesn't crash or fail anywhere, it just never receives anything. If you can build of this and get anything, I'd surely appreciate it - I need rawsockets in B2 to do general networking (I can emulate an ethernet card with raw sockets).

I'd run the above code and try to ping the PSP from my computer. Using normal packets would allow me to see the packets (not raw). Using raw sockets, I'd get absolutely nothing.
User avatar
dennis96411
Posts: 70
Joined: Sun Jul 06, 2008 4:59 am

Post by dennis96411 »

Doesn't PSPKVM use sockets for internet connectivity?
My PSP's Firmware:
5.00 M33-6 w/ LEDA 0.2

My PSP's Motherboard:
TA-088

My PSP's Model:
PSP-2001 (Slim)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

dennis96411 wrote:Doesn't PSPKVM use sockets for internet connectivity?
Yeah, plain sockets with regular connectivity. If you want that in B2, you could write a special version of FireFox (or whatever) that used a special command to use the PSP sockets to communicate. That's UPPER LEVEL networking. OSes provide the LOWER LEVEL. In the case of the PSP, that's Sony's socket code and libs. In the Mac, that's MacOS. MacOS deals with packets at a level lower than regular sockets. So does Windows, or Amiga TCP, or any OS. The OS handles the low level while the applications use the high level. If you're emulating an OS, you're stuck trying to figure out how to handle that low level when all you have is the higher levels. That's where raw sockets come in - it gives applications the ability to dip down into those low levels and do things they otherwise couldn't.
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

when pinging the psp with the pc should I be seing anything on the psp happen?

On the comp it says it recved them but theres nothing that prints on the psp


and what does it recv from?

length = recvfrom(sock, RecvBuf, 1514, 0, (struct sockaddr *)&from, &from_len);
Last edited by Gaby_64 on Thu Mar 19, 2009 6:32 am, edited 1 time in total.
<a><img></a>
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Gaby_64 wrote:when pinging the psp with the pc should I be seing anything on the psp happen?

On the comp it says it recved them but theres nothing that prints on the psp
Exactly. If it were working, you'd see prints about the packets. Raw sockets just doesn't seem available... at least not to user-mode apps. I haven't tried that same thing in kernel-mode yet. The computer saying the PSP received them shows the AP is connected and the networking properly initialized - so the PSP is getting the packets... but the user app isn't. The example is a pretty standard rawsockets example from the PC for simply dumping info about all the packets that come in.
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

Why is it that when I try to set it to nonblock in my app it says 109(ENOPROTOOPT)

EDIT1:
I dont think it works in kernel mode, it fails to initialize the network and freezes when trying to create a socket

EDIT2:
and the psp doesnt display anything because recvfrom fails
Error = ffffffff, -1

and from is not defined? Should that be the routers IP?
recvfrom(sock, RecvBuf, 1514, 0, (struct sockaddr *)&from, &from_len);

EDIT3:
found my answer here and some interesting info
http://msdn.microsoft.com/en-us/library ... S.85).aspx

also why do you put +1
select(sock+1, &readfd, NULL, NULL, &timeout)

EDIT4:
the sceNetInetRecvfrom function freezeson psp (I wanted to use it to be able to get the error number, is there a way to get error number from the non sce functions?)


sorry for all the questions butI still believe it is possible


EDIT5:

here is a screenshot of what I got so far:

Image

as you can see recvfrom fails, EBADF, /* Bad file number */
but what does that mean?

EDIT6:

tested with sock_stream and sock_dgram and recvfrom returns the same results

so this could only be a problem with recvfrom and not that raw_sockets are not posible on psp
<a><img></a>
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

What do you think J.F.?
<a><img></a>
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Gaby_64 wrote:Why is it that when I try to set it to nonblock in my app it says 109(ENOPROTOOPT)

EDIT1:
I dont think it works in kernel mode, it fails to initialize the network and freezes when trying to create a socket
Like I said, I haven't tried kernel mode. You may have to change the init around as the current init is using user functions, not kernel functions.
EDIT2:
and the psp doesnt display anything because recvfrom fails
Error = ffffffff, -1

and from is not defined? Should that be the routers IP?
recvfrom(sock, RecvBuf, 1514, 0, (struct sockaddr *)&from, &from_len);
Most calls to recvfrom will fail... because there's no packet waiting. When a packet has been received, the function won't fail and will fill in the from field to let you know who it was from.
EDIT3:
found my answer here and some interesting info
http://msdn.microsoft.com/en-us/library ... S.85).aspx

also why do you put +1
select(sock+1, &readfd, NULL, NULL, &timeout)
You don't seem to know how to use sockets... I suggest you read a few pages and try a few examples on the PC before trying to move to the PSP.
EDIT4:
the sceNetInetRecvfrom function freezeson psp (I wanted to use it to be able to get the error number, is there a way to get error number from the non sce functions?)
No idea. I just use regular stuff others already have done in other PSP apps. I don't/haven't done any research into PSP networking.
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

You havnt answered EDIT5 and 6?
I am learning some socket programming, just havnt gotten to read it all yet
I have looked at many source codes that use sockets, the first time I see select being used and also recvfrom
<a><img></a>
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Take this out of the B2 thread. You're cluttering it with unrelated posts.

The bad file error is probably because you're not handling the select right and passing a bad fd to the recvfrom. I probably won't answer anymore posts on this. You need to do more work yourself. We all have stuff we're working on and can't hold your hand on every single step. If I were going to do EVERYTHING, I'd already be doing it.

As to errors, perhaps things have changed slightly in the firmware since I last ran the code... I was on something like 3.90 or something. The behavior may have changed, and no, I'm not going to look into it.
User avatar
Gaby_64
Posts: 33
Joined: Fri Dec 19, 2008 4:04 am

Post by Gaby_64 »

I only needed advice, I thought you had already tried almost everything with raw sockets. I am currently using the same method you are, I will check if I get the same results from your app (Its missing error handlers, you might not have seen that it was returning an error)
<a><img></a>
daniela
Posts: 1
Joined: Tue Aug 11, 2009 3:50 am

Post by daniela »

What is the differrence between a router and an ethernet hub? I thought they were the same thing- I have an ethernet hub-can i successfully plug in two computers and have internet access with both? Or do I need a router or what?
___________
affiliateelite ~ affiliateelite.com ~ adgooroo ~ adgooroo.com
Last edited by daniela on Thu Aug 20, 2009 5:46 pm, edited 1 time in total.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

daniela wrote:What is the differrence between a router and an ethernet hub? I thought they were the same thing- I have an ethernet hub-can i successfully plug in two computers and have internet access with both? Or do I need a router or what?
A hub and a router are not the same thing - a hub is a simple box that merely connects multiple ethernet sources. A router is a smart device, usually a full computer running linux or some other OS, whose task is to connect to modem, provide NAT, and often a firewall, among other things. A hub can connect multiple computers together as long as the computers do all the work themselves. A router can connect multiple computers to a single modem so that all can get on the net without having to do anything special.
Visigotico
Posts: 11
Joined: Wed Apr 23, 2008 11:44 am

Add network support...

Post by Visigotico »

Add network support...

maybe using udp tunneling like CIPE, OpenVPN or other protocol that suport tunneling and bridging.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Re: Add network support...

Post by J.F. »

Visigotico wrote:Add network support...

maybe using udp tunneling like CIPE, OpenVPN or other protocol that suport tunneling and bridging.
It's got UDP tunneling... for AppleTalk networking. You'd need a special server app on a PC to provide internet via UDP tunneling, and I haven't the gumption to put that much work into it.
Post Reply