forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Basilisk II PSP Port (Mac Emulator)
Goto page Previous  1, 2, 3 ... 22, 23, 24
 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Wed Mar 18, 2009 2:13 am    Post subject: Reply with quote

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>
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Wed Mar 18, 2009 9:19 am    Post subject: Reply with quote

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:
#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("RawSockets", 0, VERS, REVS);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);


int psp_net_error = 0;
int psp_net_available = 0;

char szMyIPAddr[32];
char *psp_local_ip_addr = NULL;                     // String: 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(int config) {
    int err, i;
    int stateLast = -1;

   pspDebugScreenClear();

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

    sceNetApctlDisconnect();
    err = sceNetApctlConnect(config);
    if (err != 0) {
        pspDebugScreenPrintf("sceNetApctlConnect returns %08X\n", err);
        return 0;
    }

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

    pspDebugScreenPrintf("Connected!\n");
    sceKernelDelayThread(3*1000*1000);

    return 1;
}

static int net_thread(SceSize args, void *argp)
{
    int selComponent = 1; // access point selector

    pspDebugScreenPrintf("Using connection %d to connect...\n", selComponent);

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

    return 0;
}

static void psp_net_connect (void)
{
   SceUID thid;

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

   pspDebugScreenPrintf("Networking successfully started.\n");
   sceKernelDelayThread(4*1000*1000);
}

static int InitialiseNetwork(void)
{
  int err;

  pspDebugScreenPrintf("load network modules...");
  err = sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
  if (err != 0)
  {
    pspDebugScreenPrintf("Error, could not load PSP_NET_MODULE_COMMON %08X\n", err);
    return 1;
  }
  err = sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
  if (err != 0)
  {
    pspDebugScreenPrintf("Error, could not load PSP_NET_MODULE_INET %08X\n", err);
    return 1;
  }
  printf("done\n");

  err = pspSdkInetInit();
  if (err != 0)
  {
    pspDebugScreenPrintf("Error, could not initialise the network %08X\n", err);
    return 1;
  }
  return 0;
}

static void psp_net_init(void)
{
   if (InitialiseNetwork() != 0)
   {
      psp_net_error = 1;
      pspDebugScreenPrintf("Networking not available.\n");
      sceKernelDelayThread(4*1000*1000);
      return;
   }
}

/*
 *  Packet reception thread
 */

static int receive_proc(SceSize args, void *argp)
{
   int fd = my_socket;

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

   sceKernelExitDeleteThread(0);
   return 0;
}


/*
 *  Start packet reception thread
 */

int ether_start_pkt_thread(void)
{
   SceUID read_thread = sceKernelCreateThread("Packet Receiver", receive_proc, 0x14, 0x1800, PSP_THREAD_ATTR_USER, NULL);
   if(read_thread < 0)
        return 0;
   sceKernelStartThread(read_thread, 0, NULL);
   return 1;
}

int main(void)
{
   int on = 1;
   ssize_t length;
   struct sockaddr_in from;
   socklen_t from_len = sizeof(from);
   char packet[8192];
   SceCtrlData pad;

    pspDebugScreenInit();
    pspDebugScreenSetBackColor(0xff000000);
    pspDebugScreenSetTextColor(0xffffffff);
    pspDebugScreenClear();

    sceCtrlSetSamplingCycle(0);
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);

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

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

    sceKernelDelayThread(3*1000*1000);
    sceKernelExitGame();

    return 0;   /* never reaches here, again, just to suppress warning */
}


makefile
Code:
TARGET = RawSockets
OBJS = main.o

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

BUILD_PRX = 1
PSP_FW_VERSION = 371

LIBDIR =
LIBS = -lpspwlan
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Raw Sockets Test

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/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.
Back to top
View user's profile Send private message AIM Address
dennis96411



Joined: 06 Jul 2008
Posts: 70

PostPosted: Wed Mar 18, 2009 9:37 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Wed Mar 18, 2009 11:03 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Thu Mar 19, 2009 6:11 am    Post subject: Reply with quote

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);
_________________
<a><img></a>


Last edited by Gaby_64 on Thu Mar 19, 2009 6:32 am; edited 1 time in total
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Mar 19, 2009 6:20 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Thu Mar 19, 2009 6:41 am    Post subject: Reply with quote

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/ms740120(VS.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:



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>
Back to top
View user's profile Send private message
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Fri Mar 20, 2009 8:58 am    Post subject: Reply with quote

What do you think J.F.?
_________________
<a><img></a>
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Mar 20, 2009 10:15 am    Post subject: Reply with quote

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.

Quote:
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.

Quote:
EDIT3:
found my answer here and some interesting info
http://msdn.microsoft.com/en-us/library/ms740120(VS.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.

Quote:
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.
Back to top
View user's profile Send private message AIM Address
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Fri Mar 20, 2009 10:55 am    Post subject: Reply with quote

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>
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Mar 20, 2009 11:44 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Gaby_64



Joined: 19 Dec 2008
Posts: 33

PostPosted: Fri Mar 20, 2009 11:56 am    Post subject: Reply with quote

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>
Back to top
View user's profile Send private message
daniela



Joined: 11 Aug 2009
Posts: 1

PostPosted: Wed Aug 12, 2009 7:30 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Aug 13, 2009 1:19 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Visigotico



Joined: 23 Apr 2008
Posts: 11

PostPosted: Thu Sep 03, 2009 2:35 pm    Post subject: Add network support... Reply with quote

Add network support...

maybe using udp tunneling like CIPE, OpenVPN or other protocol that suport tunneling and bridging.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Sep 03, 2009 3:23 pm    Post subject: Re: Add network support... Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Goto page Previous  1, 2, 3 ... 22, 23, 24
Page 24 of 24

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group