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 

Retrieve files via http
Goto page 1, 2  Next
 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Tue Dec 06, 2005 3:07 pm    Post subject: Retrieve files via http Reply with quote

Can someone provide an example of source as to the best way to retrieve files via http (port 80, and http GET) in the same basic operation as wget?

Many thanks,
Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Tue Dec 06, 2005 3:30 pm    Post subject: Reply with quote

Do not use ads in your signature.
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Tue Dec 06, 2005 3:40 pm    Post subject: Reply with quote

mrbrown wrote:
Do not use ads in your signature.


Sorry bout that, I've just been copying and pasting it in lately.

As to my question, based on wifitest, I know how to open a socket for listening, and send data, but I suppose there's a cleaner more efficient way of doing it.

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Tue Dec 06, 2005 9:09 pm    Post subject: Re: Retrieve files via http Reply with quote

See this topic http://forums.ps2dev.org/viewtopic.php?t=4216 and my sample program. You have to strip the HTTP-headers and perhaps find a cleaner way for detecting the file end, or you have to wait until mrbrown adds the code to the PSPSDK wlan sample and cleans it up :-)
Back to top
View user's profile Send private message
PspPet



Joined: 30 Mar 2005
Posts: 210

PostPosted: Wed Dec 07, 2005 2:39 am    Post subject: Reply with quote

FWIW: there is a more elaborate/robust library built into the system that does all of this. However it hasn't been completely reverse engineered (yet/ever?)
If interested, look for the "sceHttp" APIs.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Zenurb



Joined: 30 Sep 2005
Posts: 106
Location: United Kingdom

PostPosted: Wed Dec 07, 2005 2:58 am    Post subject: Reply with quote

PspPet wrote:
FWIW: there is a more elaborate/robust library built into the system that does all of this. However it hasn't been completely reverse engineered (yet/ever?)
If interested, look for the "sceHttp" APIs.


A full documentation of all the sce* API functions (i.e. parameter lists/usages nstuff) would be nice. Not that I know C or anything.
_________________
Proud Dvorak User
US 1.5 PSP (Original)
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Wed Dec 07, 2005 10:16 am    Post subject: Reply with quote

I hear tale using those same API's, there is a DNS resolve function....anybody know how to use it? Can you post in a function to return the hosts IP address?

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Wed Dec 07, 2005 10:28 am    Post subject: Reply with quote

planetusa wrote:
I hear tale using those same API's, there is a DNS resolve function....anybody know how to use it? Can you post in a function to return the hosts IP address?


It's used in PSPRadio. I've included it in Lua Player, see luawlan.cpp and the WLAN Lua example how to use it (should be no problem to see how the Lua script calls the C functions).
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Wed Dec 07, 2005 2:23 pm    Post subject: Reply with quote

I looked at that, and despite all my efforts, couldn't get it to work. Are you planning to update your wget code to use dns?

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
liquid8d



Joined: 30 Jun 2005
Posts: 66

PostPosted: Fri Dec 09, 2005 7:53 am    Post subject: Reply with quote

I can't get the DNS to work at all, it always hangs on ResolverStartNtoA...


liquid8d
Back to top
View user's profile Send private message AIM Address
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Fri Dec 09, 2005 11:39 am    Post subject: Reply with quote

I've updated it again, now it works with the latest SVN and the headers from the HTTP result are ignored. Now it is nearly ready for a new sample in the PSPSDK sample folder.

main.c:

Code:

#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <psputility.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

// TODO: should be moved to PSPSDK
int sceNetResolverCreate(int *rid, void *buf, SceSize buflen);
int sceNetResolverStartNtoA(int rid, const char* hostname, struct in_addr* addr, unsigned int timeout, int retry);
int sceNetResolverStartAtoN(int rid, const struct in_addr* addr, char *hostname, SceSize hostname_len, unsigned int timeout, int retry);
int sceNetResolverStop(int rid);
int sceNetInetInetAton(const char* host, struct in_addr* addr);

static char resolverBuffer[1024];
static int resolverId;

/* Define the module info section */
PSP_MODULE_INFO("WGET", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);
PSP_MAIN_THREAD_STACK_SIZE_KB(32); /* smaller stack for kernel thread */

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   sceKernelExitGame();
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

void wget()
{
   ///////////// PSP specific part ///////////////

   // init wlan
   int err = pspSdkInetInit();
   if (err != 0) {
      pspDebugScreenPrintf("pspSdkInetInit failed: %i", err);
      return;
   }

   // print available connections
   pspDebugScreenPrintf("available connections:\n");
   int i;
   for (i = 1; i < 100; i++) // skip the 0th connection
   {
      if (sceUtilityCheckNetParam(i) != 0) break;  // no more
      char name[64];
      sceUtilityGetNetParam(i, 0, (netData*) name);
      pspDebugScreenPrintf("%i: %s\n", i, name);
   }
   
   // use connection 1
   pspDebugScreenPrintf("using first connection\n");
   err = sceNetApctlConnect(1);
   if (err != 0) {
      pspDebugScreenPrintf("sceNetApctlConnect failed: %i", err);
      return;
   }

   // WLAN is initialized when PSP IP address is available
   pspDebugScreenPrintf("init WLAN and getting IP address");
   char pspIPAddr[32];
   while (1) {
      if (sceNetApctlGetInfo(8, pspIPAddr) == 0) break;
      pspDebugScreenPrintf(".");
      sceKernelDelayThread(1000 * 1000);  // wait a second
   }
   pspDebugScreenPrintf("\nPSP IP address: %s\n", pspIPAddr);

   // start DNS resolver
   err = sceNetResolverCreate(&resolverId, resolverBuffer, sizeof(resolverBuffer));
   if (err != 0) {
      pspDebugScreenPrintf("sceNetResolverCreate failed: %i", err);
      return;
   }

   // resolve host
   pspDebugScreenPrintf("resolving host...\n");
   const char *host = "www.luaplayer.org";
   int port = 80;
   struct sockaddr_in addrTo;
   addrTo.sin_family = AF_INET;
   addrTo.sin_port = htons(port);
   err = sceNetInetInetAton(host, &addrTo.sin_addr);
   if (err == 0) {
      err = sceNetResolverStartNtoA(resolverId, host, &addrTo.sin_addr, 2, 3);
      if (err != 0) {
         pspDebugScreenPrintf("sceNetResolverStartNtoA failed: %i", err);
         return;
      }
   }


   ///////////// standard socket part ///////////////

   // create socket (in blocking mode)
   pspDebugScreenPrintf("creating socket...\n");
   int sock = socket(AF_INET, SOCK_STREAM, 0);
   if (sock < 0) {
      pspDebugScreenPrintf("socket failed, errno: %i", errno);
      return;
   }

   // connect (this may block some time)
   pspDebugScreenPrintf("connecting...\n");
   err = connect(sock, (struct sockaddr*) &addrTo, sizeof(addrTo));
   if (err != 0) {
      pspDebugScreenPrintf("connect failed");
      return;
   }

   // send HTTP request
   pspDebugScreenPrintf("loading page");
   const char* request =
      "GET /wlan-pspsdk-test.txt HTTP/1.0\r\n"
      "host: www.luaplayer.org\r\n\r\n";
   err = send(sock, request, strlen(request), 0);
   if (err < 0) {
      pspDebugScreenPrintf("send failed, errno: %i", errno);
      return;
   }
   
   // read all
   int capacity = 256;
   int len = 0;
   u8* buffer = (u8*) malloc(capacity);
   while (1) {
      pspDebugScreenPrintf(".");

      // read data
      int count = recv(sock, (u8*) &buffer[len], capacity - len, 0);

      // in blocking mode it has to return something, otherwise it is closed
      // (which is the default for HTTP/1.0 connections)
      if (count == 0) break;
      
      if (count < 0) {
         pspDebugScreenPrintf("read error: %i\n", errno);
         break;
      }

      // adjust buffer, if needed
      len += count;
      if (len + 256 > capacity) {
         capacity *= 2;
         buffer = realloc(buffer, capacity);
         if (!buffer) break;
      }
   }
   
   // now "len" bytes data are in buffer, add a 0 at end for printing and search for header end
   if (buffer) {
      buffer[len] = 0;
      char* page = strstr((char*) buffer, "\r\n\r\n");
      if (page) {
         page += 4;
         pspDebugScreenPrintf("\n\n%s", page);
      }
      free(buffer);
   }
   

   ///////////// PSP specific part ///////////////

   // term, TODO: this doesn't work
   sceNetApctlDisconnect();
   pspSdkInetTerm();
}
   

int user_main(SceSize argc, void* argv)
{
   SetupCallbacks();
   
   // test wlan
   wget();
   pspDebugScreenPrintf("\ntest finished, wlan should be deactivated now");
   
   // wait until user ends the program
   sceKernelSleepThread();

   return 0;
}

__attribute__((constructor)) void wlanInit()
{
   // TODO: wlan initialization should work in a kernel mode constructor
//   pspSdkLoadInetModules();
}

int main(void)
{
   pspDebugScreenInit();
   int err = pspSdkLoadInetModules();

   if (err != 0) {
      pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
      sceKernelDelayThread(5*1000000); // 5 sec to read error
      return 1;
   }

   // create user thread, tweek stack size here if necessary
   SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
      0x11, // default priority
      256 * 1024, // stack size (256KB is regular default)
      PSP_THREAD_ATTR_USER, NULL);
   
   // start user thread, then wait for it to do everything else
   sceKernelStartThread(thid, 0, NULL);
   sceKernelWaitThreadEnd(thid, NULL);
   
   sceKernelExitGame();

   return 0;

}


Makefile:

Code:

TARGET = wget
OBJS = main.o

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

LIBDIR =
LDFLAGS =
LIBS = -lpspdebug -lpspsdk -lpspwlan -lpspnet_apctl -lpspnet_resolver -lc -lpspnet_inet -lpspnet
# TODO: without -lpspsdk the linker reports many undefined references in pspSdkInetInit

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = wget

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


Last edited by Shine on Sun Dec 11, 2005 8:59 am; edited 1 time in total
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Dec 11, 2005 4:18 am    Post subject: Reply with quote

Is anyone working on the socket support for the PSPSDK? With the latest changes in the socket files in SVN from today the program I posted doesn't even compile anymore, the linker says:

Quote:

/usr/local/pspdev/lib/gcc/psp/4.0.2/../../../../psp/lib/libc.a(connect.o): In function `connect':
../../../../../../newlib/libc/sys/psp/socket.c:178: undefined reference to `sceNetInetGetErrno'
/usr/local/pspdev/lib/gcc/psp/4.0.2/../../../../psp/lib/libc.a(recv.o): In function `recv':
../../../../../../newlib/libc/sys/psp/socket.c:268: undefined reference to `sceNetInetRecv'
../../../../../../newlib/libc/sys/psp/socket.c:271: undefined reference to `sceNetInetGetErrno'
/usr/local/pspdev/lib/gcc/psp/4.0.2/../../../../psp/lib/libc.a(send.o): In function `send':
../../../../../../newlib/libc/sys/psp/socket.c:330: undefined reference to `sceNetInetSend'
../../../../../../newlib/libc/sys/psp/socket.c:333: undefined reference to `sceNetInetGetErrno'
/usr/local/pspdev/lib/gcc/psp/4.0.2/../../../../psp/lib/libc.a(socket.o): In function `socket':
../../../../../../newlib/libc/sys/psp/socket.c:43: undefined reference to `sceNetInetClose'
../../../../../../newlib/libc/sys/psp/socket.c:33: undefined reference to `sceNetInetGetErrno'
/usr/local/pspdev/lib/gcc/psp/4.0.2/../../../../psp/lib/libc.a(socket.o): In function `__psp_socket_close':
../../../../../../newlib/libc/sys/psp/socket.c:66: undefined reference to `sceNetInetClose'
../../../../../../newlib/libc/sys/psp/socket.c:70: undefined reference to `sceNetInetGetErrno'
collect2: ld returned 1 exit status
make: *** [wget.elf] Error 1
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Dec 11, 2005 4:26 am    Post subject: Reply with quote

Are you linking with libpspnet_inet.a?
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Dec 11, 2005 4:32 am    Post subject: Reply with quote

mrbrown wrote:
Are you linking with libpspnet_inet.a?


yes, see above for my LIBS definition. The compiler is called like this:

Code:

psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -L. -L/usr/local/pspdev/psp/sdk/lib   main.o -lpspdebug -lpspsdk -lpspwlan -lpspnet_apctl -lpspnet_resolver -lpspnet_inet -lpspnet -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lc -lpsputility -lpspuser -lpspkernel -o wget.elf


But perhaps it is easier if you would just copy and past my example and fix it :-)
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Dec 11, 2005 4:45 am    Post subject: Reply with quote

-lpspnet_inet needs to be specified after -lc because the socket code in -lc depends on it.
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Dec 11, 2005 4:59 am    Post subject: Reply with quote

mrbrown wrote:
-lpspnet_inet needs to be specified after -lc because the socket code in -lc depends on it.


thanks, now the LIBS line looks like this and it works:

Code:

LIBS = -lpspdebug -lpspsdk -lpspwlan -lpspnet_apctl -lpspnet_resolver -lc -lpspnet_inet -lpspnet


The last thing missing for easier porting existing socket programs would be to add the in_addr and sockaddr_in structure definitions in the psp include directory and providing the resolver stuff with the gethostbyname function (perhaps with lazy initialization of the resolver lib to avoid too many memory usage on pspSdkInetInit, if not needed and unloading it on pspSdkInetTerm, if initialized).
Back to top
View user's profile Send private message
pspfan



Joined: 30 Dec 2005
Posts: 1

PostPosted: Fri Dec 30, 2005 5:08 am    Post subject: missing socket.h Reply with quote

Hello,
I installed the latest pspdev using both cygwin (+ pspchain) and devkitPro but I got this error when I try to compile this example :

psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -c -o main.o mai
n.c
main.c:10:25: error: netinet/in.h: No such file or directory
main.c:11:25: error: sys/socket.h: No such file or directory
main.c:15: warning: 'struct in_addr' declared inside parameter list
main.c:15: warning: its scope is only this definition or declaration, which is p
robably not what you want
main.c:16: warning: 'struct in_addr' declared inside parameter list
main.c:18: warning: 'struct in_addr' declared inside parameter list
main.c: In function 'wget':
main.c:113: error: storage size of 'addrTo' isn't known
main.c:114: error: 'AF_INET' undeclared (first use in this function)
main.c:114: error: (Each undeclared identifier is reported only once
main.c:114: error: for each function it appears in.)
main.c:115: warning: implicit declaration of function 'htons'
main.c:130: warning: implicit declaration of function 'socket'
main.c:130: error: 'SOCK_STREAM' undeclared (first use in this function)
main.c:138: warning: implicit declaration of function 'connect'
main.c:149: warning: implicit declaration of function 'send'
main.c:163: warning: implicit declaration of function 'recv'
main.c:113: warning: unused variable 'addrTo'
make: *** [main.o] Error 1

I suppose that the problem lies in an incorrect library path when compiling the example (it doesn't found sys/socket.h and other include files)

the only file I found (under cygwin installation that seems the missing ones are under the /home/local/include but when I include those files I got another error).

Thanks in advance for the help.

Francesco.
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Wed Jan 11, 2006 1:55 am    Post subject: Reply with quote

I have modified Shine's example heavily (sent him a PM), it now supports URI parsing, and both the GET and POST methods, and I will be happy to share the code, as soon as I can get binary files to transfer correctly. Currently, plaintext works flawlessly, if you can help, please see thread

http://forums.ps2dev.org/viewtopic.php?t=4615&highlight=

This code can be used for many many things, the possibilities are endless.


Thanks,
Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Wed Jan 11, 2006 8:05 am    Post subject: Reply with quote

Code:

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <psputility.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <util.h>

// TODO: should be moved to PSPSDK
int sceNetResolverCreate(int *rid, void *buf, SceSize buflen);
int sceNetResolverStartNtoA(int rid, const char* hostname, struct in_addr* addr, unsigned int timeout, int retry);
int sceNetResolverStartAtoN(int rid, const struct in_addr* addr, char *hostname, SceSize hostname_len, unsigned int timeout, int retry);
int sceNetResolverStop(int rid);
int sceNetInetInetAton(const char* host, struct in_addr* addr);
#define SOCKET int
#define MAX_URL_LENGTH 750
#define MAX_JPEG   30000
typedef struct {
   char uri[256];
    char sheme[256];
    char host[256];
    char abs_path[256];
    int port;
    char query[256];
} URI;

static char resolverBuffer[1024];
static int resolverId;

/* Define the module info section */
PSP_MODULE_INFO("WGET", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);
PSP_MAIN_THREAD_STACK_SIZE_KB(32); /* smaller stack for kernel thread */

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   sceKernelExitGame();
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

unsigned short htons(unsigned short wIn)
{
    u8 bHi = (wIn >> 8) & 0xFF;
    u8 bLo = wIn & 0xFF;
    return ((unsigned short)bLo << 8) | bHi;
}


void ParseURI(URI *uri)
{
 /* parsing address

      http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

    */

    *uri->sheme=0;
    *uri->host=0;
    strcpy(uri->abs_path,"/");
    *uri->query=0;
    uri->port=0;

   char *start;
    char *end;
    char portstr[256];


    start=uri->uri;
   //end=strstr(start,"://");
    end=strchr(start,':');

    if ( end != NULL) {
       char *end1=strstr(end,"//");
       if (end1 != NULL) {
           if (end < end1 ) {
             strncpy(uri->sheme,start,end-start+1);
              uri->sheme[end-start+1]=0;
              strlwr(uri->sheme);
            }
            start=end1+2;
        }
        else {

           int num=atoi(end+1);
            if ( num <= 0 ) {
               strncpy(uri->sheme,start,end-start+1);
               uri->sheme[end-start+1]=0;
               strlwr(uri->sheme);
               start=end+1;
            }
        }
    }

    end=uri->uri+strlen(uri->uri);
    char *slash=strchr(start,'/');
    char *colon=strchr(start,':');
    char *qmark=strchr(start,'\?');

    if (qmark != NULL ) { // query
       strcpy(uri->query,qmark);
        end=qmark;
    }


    if (slash != NULL ) { // abs_path
       if (slash < end) {
          strncpy(uri->abs_path,slash,end-slash);
            uri->abs_path[end-slash]=0;
            end=slash;
        }
    }


    if ( colon != NULL && colon < end ) {
       strncpy(portstr,colon+1,end-colon-1);
        portstr[end-colon-1]=0;
        end=colon;
        uri->port=atoi(portstr);
    }

    if (end > start) {
       strncpy(uri->host,start,end-start);
        uri->host[end-start]=0;
        strlwr(uri->host);

    }
}




void wget(URI *uri, char* method, char* formdata)
{
   ///////////// PSP specific part ///////////////
   pspDebugScreenPrintf("downloading %s now\n", uri);
   // init wlan
   int err = pspSdkInetInit();
   if (err != 0) {
      pspDebugScreenPrintf("pspSdkInetInit failed: %i", err);
      return;
   }
   //init_file("ms0:/custom.html");
   // print available connections
   //pspDebugScreenPrintf("available connections:\n"); //removed
   ParseURI(uri);
   
   int i;
   for (i = 1; i < 100; i++) // skip the 0th connection
   {
      if (sceUtilityCheckNetParam(i) != 0) break;  // no more
      char name[64];
      sceUtilityGetNetParam(i, 0, (netData*) name);
      pspDebugScreenPrintf("%i: %s\n", i, name);
   }
   
   // use connection 1
   //pspDebugScreenPrintf("using first connection\n"); //removed
   err = sceNetApctlConnect(1);
   if (err != 0) {
      pspDebugScreenPrintf("sceNetApctlConnect failed: %i", err);
      return;
   }

   // WLAN is initialized when PSP IP address is available
   //pspDebugScreenPrintf("init WLAN and getting IP address"); //removed
   char pspIPAddr[32];
   while (1) {
      if (sceNetApctlGetInfo(8, pspIPAddr) == 0) break;
      pspDebugScreenPrintf(".");
      sceKernelDelayThread(1000 * 1000);  // wait a second
   }
   //pspDebugScreenPrintf("\nPSP IP address: %s\n", pspIPAddr); //removed

   // start DNS resolver
   err = sceNetResolverCreate(&resolverId, resolverBuffer, sizeof(resolverBuffer));
   if (err != 0) {
      pspDebugScreenPrintf("sceNetResolverCreate failed: %i", err);
      return;
   }

   // resolve host
   //pspDebugScreenPrintf("resolving host...\n"); //removed
   const char *host = uri->host;
   int port = 80;
   struct sockaddr_in addrTo;
   addrTo.sin_family = AF_INET;
   addrTo.sin_port = htons(port);
   err = sceNetInetInetAton(host, &addrTo.sin_addr);
   if (err == 0) {
      err = sceNetResolverStartNtoA(resolverId, host, &addrTo.sin_addr, 2, 3);
      if (err != 0) {
         pspDebugScreenPrintf("sceNetResolverStartNtoA failed: %i", err);
         return;
      }
   }


   ///////////// standard socket part ///////////////

   // create socket (in blocking mode)
   //pspDebugScreenPrintf("creating socket...\n");  //removed
   int sock = socket(AF_INET, SOCK_STREAM, 0);
   if (sock < 0) {
      pspDebugScreenPrintf("socket failed, errno: %i", errno);
      return;
   }

   // connect (this may block some time)
   //pspDebugScreenPrintf("connecting...\n");  //removed
   err = connect(sock, (struct sockaddr*) &addrTo, sizeof(addrTo));
   if (err != 0) {
      pspDebugScreenPrintf("connect failed");
      return;
   }

   // send HTTP request
   //pspDebugScreenPrintf("loading page");  //removed
   
   //const char* request =
   //"GET / HTTP/1.0\r\n"
   //"host: www.google.com\r\n\r\n";
   const char request[MAX_URL_LENGTH + 1] = { '\0' };
   if (method == "GET") {
      sprintf(request,
      "GET %s HTTP/1.0\r\n"
      "host: %s\r\n\r\n",
      uri->abs_path, uri->host);
   }   
   if (method == "POST") {
      sprintf(request,
      "POST %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: PSP\r\nContent-type: application/x-www-form-urlencoded\r\nContent-length: %u\r\n\r\n%s",
      uri->abs_path, uri->host, strlen(formdata), formdata);
   }
   pspDebugScreenPrintf(request); //for testing
   err = send(sock, request, strlen(request), 0);
   if (err < 0) {
      pspDebugScreenPrintf("send failed, errno: %i", errno);
      return;
   }
   int capacity = 256;
   int len = 0;
   u8* buffer = (u8*) malloc(capacity);
   
   while (1) {
      pspDebugScreenPrintf(".");

      // read data
      int count = recv(sock, (u8*) &buffer[len], capacity - len, 0);

      // in blocking mode it has to return something, otherwise it is closed
      // (which is the default for HTTP/1.0 connections)
      if (count == 0) break;
       
      if (count < 0) {
         pspDebugScreenPrintf("read error: %i\n", errno);
         break;
      }

      // adjust buffer, if needed
      len += count;
      if (len + 256 > capacity) {
         capacity *= 2;
         buffer = realloc(buffer, capacity);
         if (!buffer) break;
      }
   }
    // now "len" bytes data are in buffer, add a 0 at end for printing and search for header end
   //pspDebugScreenPrintf(buffer); //used for debugging
   if (buffer) {
      buffer[len] = 0;
      char* page = strstr((char*) buffer, "\r\n\r\n");
      if (page) {
         page += 4;
         //pspDebugScreenPrintf(page); //only for testing output
       //parse_html(page);
       //write_to_file("ms0:/custom.html", page);
       pspDebugScreenPrintf("\n\nPage saved to ms0:/custom.html");
      }
      free(buffer);
   }
   

   ///////////// PSP specific part ///////////////

   // term, TODO: this doesn't work
   sceNetApctlDisconnect();
   pspSdkInetTerm();
}
   

int user_main(SceSize argc, void* argv)
{
   SetupCallbacks();
   
   // test wlan
   // wget(URL, HTTP METHOD, FORM DATA) POST for POST GET for GET
   wget("www.google.com","GET","field1=this&field2=is&field3=nota&field4=test"); // Any plaintext url should work
   //pspDebugScreenPrintf("\ntest finished, wlan should be deactivated now"); //removed
   
   // wait until user ends the program
   sceKernelSleepThread();

   return 0;
}

__attribute__((constructor)) void wlanInit()
{
   // TODO: wlan initialization should work in a kernel mode constructor
//   pspSdkLoadInetModules();
}

int main(void)
{
   pspDebugScreenInit();
   int err = pspSdkLoadInetModules();

   if (err != 0) {
      pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
      sceKernelDelayThread(5*1000000); // 5 sec to read error
      return 1;
   }

   // create user thread, tweek stack size here if necessary
   SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
      0x11, // default priority
      256 * 1024, // stack size (256KB is regular default)
      PSP_THREAD_ATTR_USER, NULL);
   
   // start user thread, then wait for it to do everything else
   sceKernelStartThread(thid, 0, NULL);
   sceKernelWaitThreadEnd(thid, NULL);
   
   sceKernelExitGame();

   return 0;

}



It should compile without issue, I commented out functions that are specific to what I'm doing. If anyone gets binary files to work, jpg, etc, please let me know.

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
dbeyer3069



Joined: 19 Dec 2005
Posts: 81

PostPosted: Wed Jan 11, 2006 1:10 pm    Post subject: Reply with quote

Can you drop your makefile also just for giggles?

Thanks,

David Beyer
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Wed Jan 11, 2006 2:15 pm    Post subject: Reply with quote

Code:

TARGET = wget
OBJS = main.o

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

LIBDIR =
LDFLAGS =
LIBS = -lpspdebug -lpspsdk -lpspwlan -lpspnet_apctl -lpspnet_resolver -lc -lpspnet_inet -lpspnet
#LIBS = -lpspdebug -lpspwlan -lpspsdk -lpspnet_apctl -lpspnet_resolver -lpspnet_inet -lpspnet
# TODO: without -lpspsdk it says: libpspsdk.a(inethelper.o): In function `pspSdkInetInit': inethelper.c:67: undefined reference to `sceNetInit'

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = wget

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
digihoe



Joined: 14 May 2005
Posts: 108

PostPosted: Wed Jan 11, 2006 11:27 pm    Post subject: Reply with quote

Great to see you getting closer to file support, will be nice to see this work...

Good luck with the web browser!

Best regards!
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Thu Jan 12, 2006 8:46 pm    Post subject: Reply with quote

Wouldn't it be much nicer to use libcurl ;)

Here, I've just finished porting it.

http://localhost.geek.nz/crap/curl-psp.tar.bz2
Edit: Woops, its actually a .bz2 file *renamed*

Instructions:
Download the latest curl source code (I used 7.15.1).

Apply the patch and build curl: (change ../patch-curl to the location of the patch)
Code:

tar -jxf curl-7.15.1.tar.bz2
cd curl-7.15.1
cat ../curl-psp-patch | patch -p1
chmod +x buildit
./buildit

(buildit is a short script to create some empty folders needed and also contains the huge configure line)

now you should have libcurl.a inside curl-7.15.1/lib/.libs/

To use libcurl you will need to link against libpsp_req.a from inside the psp_req folder and libcurl.a
(psp_req is a quick implementation of gethostbyname)

Copy both of these to the example directory and compile it with make.
(You will probally need to change the -I../curl-7.15.1 line in the Makefile)
The included example downloads my website and throws all the html on the screen :)

I haven't tryed to build an ssl library to get https support, but it will likely work, I also haven't got zlib installed so I dont know if that works ;)

Oh, The example seems to hang when you try to quit, not sure why... might be the resolver created in gethostbyname.


Last edited by danzel on Fri Jan 20, 2006 7:47 am; edited 1 time in total
Back to top
View user's profile Send private message
dbeyer3069



Joined: 19 Dec 2005
Posts: 81

PostPosted: Fri Jan 13, 2006 3:01 am    Post subject: Reply with quote

danzel wrote:
Wouldn't it be much nicer to use libcurl ;)

Here, I've just finished porting it.

http://localhost.geek.nz/crap/curl-psp.tar.gz

Instructions:
Download the latest curl source code (I used 7.15.1).

Apply the patch and build curl: (change ../patch-curl to the location of the patch)
Code:

tar -jxf curl-7.15.1.tar.bz2
cd curl-7.15.1
cat ../curl-psp-patch | patch -p1
chmod +x buildit
./buildit

(buildit is a short script to create some empty folders needed and also contains the huge configure line)

now you should have libcurl.a inside curl-7.15.1/lib/.libs/

To use libcurl you will need to link against libpsp_req.a from inside the psp_req folder and libcurl.a
(psp_req is a quick implementation of gethostbyname)

Copy both of these to the example directory and compile it with make.
(You will probally need to change the -I../curl-7.15.1 line in the Makefile)
The included example downloads my website and throws all the html on the screen :)

I haven't tryed to build an ssl library to get https support, but it will likely work, I also haven't got zlib installed so I dont know if that works ;)

Oh, The example seems to hang when you try to quit, not sure why... might be the resolver created in gethostbyname.


Did you get binary transfers working with this?

David Beyer
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Fri Jan 13, 2006 4:18 am    Post subject: Reply with quote

Well ok then....I'll give curl a try. See, we were trying to keep it light weight, but I'll take a look at what you've got.

If you've got the already patched version, if would be appreciated if you could email it to me)

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Fri Jan 13, 2006 4:19 am    Post subject: Reply with quote

If curl works, this makes my job a hell of a lot easier.

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Fri Jan 13, 2006 7:59 am    Post subject: Reply with quote

I see no reason that binary transfers wouldnt work with this, but I haven't tested it. (peldet can do binary transfers fine, so its not a psp issue)

I've uploaded a prebuilt version here: http://localhost.geek.nz/crap/curl_psp_prebuilt.tar.bz2
The example is included, and should build straight off.

Another lib you may want (although it may be a bit big) is libxml2 http://xmlsoft.org

You can build it (atleast version 2.6.22) with the following lines:
Code:

CC=psp-gcc CXX=psp-g++ LIBS="-lpspdebug -lpspdisplay -lpspge -lc -lpspuser -lpspkernel" LDFLAGS="-L/usr/local/pspdev/psp/sdk/lib" ./configure --host=mips --with-minimum --without-threads --without-writer --without-modules
make

It errors out partway through when building the tests, however this is after it successfully builds the library in the .libs dir.
I haven't played with this yet, just tryed to build it to see if it would. This is also probally not the most minimal build, you could definately make it smaller.

There is a libcurl + libxml example here: http://curl.haxx.se/lxr/source/docs/examples/htmltitle.cc
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Fri Jan 13, 2006 8:16 am    Post subject: Reply with quote

It has an issue with select.h, can you put that up?

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Fri Jan 13, 2006 9:03 am    Post subject: Reply with quote

<sys/select.h> is part of the pspsdk, make sure you have the latest version and the latest newlib patch, as there has been a few changes in there to do with the networking stuff lately.

Code:
svn checkout svn://svn.pspdev.org/psp/trunk/psptoolchain
cd psptoolchain
./toolchain.sh


copy the .tar.bz2/gz of the newlib/gcc/binutils/gdb source files into the psptoolchain folder before running ./toolchain.sh if you want to save on downloading them again.

Edit: Duh, figured out why the sample wont let you quit, its because of this:
Code:
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
+++    sceKernelExitGame();
    return 0;
}

There was no sceKernelExitGame(); in there. Add it and you can quit.
Back to top
View user's profile Send private message
planetusa



Joined: 06 Dec 2005
Posts: 21
Location: Asheville

PostPosted: Fri Jan 13, 2006 5:33 pm    Post subject: Reply with quote

Updated my toolchain, that fixed it.....now....it downloads binary files, but my old method for writing to the MS causes the PSP to crash....so I'm looking for a new way to store the data it downloads. I've tried a few different things in place of your "spamming the screen", but so far, they either crash the psp, or don't work. Recommendations to save the returned data to a buffer so we can read it into libjpeg, or our parser, or such?

Michael
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
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 1, 2  Next
Page 1 of 2

 
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