Resolver under 3.xx

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

Moderators: cheriff, TyRaNiD

Post Reply
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Resolver under 3.xx

Post by mypspdev »

Please, anything wrong in the following example?

thanks

Code: Select all

////////////////////////////////
// include
////////////////////////////////
#include <pspkernel.h>
#include <pspdisplay.h>

#include <pspdebug.h>
#include <pspsdk.h>
#include <pspctrl.h>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspmoduleinfo.h>
#include <psppower.h>
#include <psputility.h>

#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <netinet/in.h>

#define printf pspDebugScreenPrintf

////////////////////////////////
// Module Header
////////////////////////////////
PSP_MODULE_INFO&#40;"Resolver 3xx", 0, 1, 0&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

SceCtrlData pad;
const char* PSP_strtoresolv = NULL;
struct hostent *PSP_resolvd = NULL;

//Status of irc connection

#define	CS_OFFLINE 1		//Not connected at all
#define	CS_CONNECTING 2		//Attempting to connect
#define	CS_IDENTIFYING 3		//connected to server, registering nick and waiting for okay message
#define	CS_CONNECTED 4		//Really connected to server, do what you want
#define GREEN 0xff00ff00
#define WHITE 0xffffffff
#define RED 0xff0000ff

char server&#91;100&#93;;
int port;
char mNickname&#91;100&#93;;
int status;
int sock;
int net_loop=0;
int MyThread=0;
char res_server&#91;50&#93;;
res_port=80;

////////////////////////////////////////
// RESOLVE THREAD
///////////////////////////////////////
int resolvThread&#40;SceSize args, void *argp&#41;
&#123;
	PSP_resolvd = gethostbyname&#40;PSP_strtoresolv&#41;;
	return 0;
&#125;


struct hostent * PSP_gethostbyname&#40;char* addr&#41;
&#123;
	PSP_strtoresolv = addr;
	PSP_resolvd = NULL;
	int a;
	for &#40;a = 0; a < 4 && PSP_resolvd==NULL; a++&#41; //try up to 4 times
	&#123;		
		SceUID dlthread = sceKernelCreateThread&#40;"afkim_resolver", resolvThread, 0x18, 0x10000, 0, NULL&#41;;
		sceKernelStartThread&#40;dlthread, 0, NULL&#41;;
		unsigned int startTime = sceKernelGetSystemTimeLow&#40;&#41;;
		do
		&#123;
			sceKernelDelayThread&#40;1000*1000&#41;;
		&#125; 
		while &#40;PSP_resolvd == NULL && startTime+1000*1000 >= sceKernelGetSystemTimeLow&#40;&#41;&#41;;
		
		if &#40;PSP_resolvd == NULL&#41;
		&#123;
//			printf&#40;"\nNot possible to resolve server address\n"&#41;;
			printf&#40;". "&#41;;
		&#125;
		else
		&#123;
			printf&#40;"Server address resolved!\n"&#41;;
		&#125;
		int ret = sceKernelTerminateDeleteThread&#40;dlthread&#41;;
		if &#40;ret < 0&#41;
		&#123;
			printf&#40;"Error terminating and deleting resolver Thread\n"&#41;; 
		&#125;
		else
		&#123;
//			printf&#40;"Resolver Thread terminated and deleted\n"&#41;; 
		&#125;
	&#125;	
	return PSP_resolvd;
&#125;

int doConnect&#40;&#41;
&#123;
//Disconnect if connected.
	if &#40;status != CS_OFFLINE&#41;
	&#123;
		doDisconnect&#40;&#41;;	
	&#125;
	strcpy&#40;&server ,"www.google.com"&#41;;
	status = CS_CONNECTING;		

//**********************************************
//resolve&#58; LOOK HERE please
	struct hostent *resolvd;
	resolvd = PSP_gethostbyname&#40;&server&#41;;
	printf&#40;inet_ntoa&#40;resolvd&#41;&#41;;
	printf&#40;&server&#41;;
	if &#40;resolvd == NULL&#41;
	&#123;
		status = CS_OFFLINE;
		return 0;
	&#125;	
	return 1;
//**********************************************
&#125;


void doDisconnect&#40;&#41;
&#123;
	close&#40;sock&#41;;
	sock = 0;
	status = CS_OFFLINE;
&#125;

////////////////////////////////////////
// RESOLVER
///////////////////////////////////////
int connect_to_apctl&#40;int config&#41;
&#123;
	int err;
	int stateLast = -1;
	printf&#40;"Wireless Network Connection to apctl\n"&#41;;
	err = sceNetApctlConnect&#40;config&#41;;
	if &#40;err != 0&#41;
	&#123;
		printf&#40;"sceNetApctlConnect returns %08X\n", err&#41;;
		return 0;
	&#125;
	else
	&#123;
		printf&#40;"sceNetApctlConnect ok\n"&#41;;
	&#125;
	printf&#40;"Connecting...\n"&#41;;
	while &#40;1&#41;
	&#123;
		int state;
		err = sceNetApctlGetState&#40;&state&#41;;
		if &#40;err != 0&#41;
		&#123;
			printf&#40;"sceNetApctlGetState returns $%x\n", err&#41;;
			break;
		&#125;
		if &#40;state > stateLast&#41;
		&#123;
			printf&#40;"  connection state %d of 4\n", state&#41;;
			stateLast = state;
		&#125;
		if &#40;state == 4&#41;
			break;  // connected with static IP
		sceKernelDelayThread&#40;50*1000&#41;; // 50ms
	&#125;
	printf&#40;"Connected!\n"&#41;;
	if&#40;err != 0&#41;
	&#123;
		return 0;
	&#125;
	return 1;
&#125;

int resolver_thread&#40;SceSize args, void *argp&#41;
&#123;
	int err;
	int retconn;
	char* res_server;
	char* nickname;
	int port;
	printf&#40;"Wireless Network Thread\n"&#41;;
	do
	&#123;
		if&#40;&#40;err = pspSdkInetInit&#40;&#41;&#41;&#41;
		&#123;
			printf&#40;"Could not initialise the network\n"&#41;;
			printf&#40;"may you already started the Server or the Client before\n"&#41;;
			printf&#40;"please for running the resolver restart the program.\n"&#41;;
			break;
		&#125;
		else
		&#123;
			printf&#40;"Network initialized\n"&#41;;
		&#125;
		if&#40;connect_to_apctl&#40;1&#41;&#41;
		&#123;
			char szMyIPAddr&#91;32&#93;;
			if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41;
			&#123;
				strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
			&#125;
			else
			&#123;
				printf&#40;"IP Address ok\n"&#41;;
			&#125;
			retconn=doConnect&#40;&#41;;
			break;
		&#125;
		else
		&#123;
			printf&#40;"No connect_to_apctl\n"&#41;;
		&#125;
	&#125;
	while&#40;net_loop&#41;;
	printf&#40;"\nClose client\n"&#41;;
	return 0;
&#125;

void run_resolver&#40;&#41;
&#123;
	SceUID thid;
   	u32 oldButtons = 0;
   	pspDebugScreenInit&#40;&#41;;
   	pspDebugScreenClear&#40;&#41;;
	pspDebugScreenSetXY&#40;0, 1&#41;;
	pspDebugScreenSetTextColor&#40;GREEN&#41;;	
	printf&#40;"Test Resolver of www.yahoo.it\n"&#41;;
	pspDebugScreenSetTextColor&#40;WHITE&#41;;	
	sceKernelDelayThread&#40;1000*1000&#41;;
	pspDebugScreenSetTextColor&#40;RED&#41;;
	printf&#40;"Press CROSS to Start Resolver\n"&#41;;
	printf&#40;"Press START to continue\n"&#41;;
	pspDebugScreenSetTextColor&#40;WHITE&#41;;
	
	for &#40;;;&#41;
	&#123;		
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if &#40;oldButtons != pad.Buttons&#41; 
		&#123;
    			if &#40;pad.Buttons & PSP_CTRL_CROSS&#41; 
			&#123;
				MyThread=1;
				break;
			&#125;
    			if &#40;pad.Buttons & PSP_CTRL_START&#41; 
			&#123;
				MyThread=0;
				break;
			&#125;
		&#125;
		oldButtons = pad.Buttons;
		sceDisplayWaitVblankStart&#40;&#41;;
		sceKernelDelayThread&#40;50000&#41;;
	&#125;

	if &#40;MyThread==1&#41;
	&#123;
		sceUtilityLoadNetModule&#40;1&#41;;
		sceUtilityLoadNetModule&#40;3&#41;;
		net_loop=0;
		thid = sceKernelCreateThread&#40;"resolver_thread", resolver_thread, 16, 256*1024, 0, NULL&#41;;
		if&#40;thid < 0&#41;
		&#123;
			printf&#40;"Error, could not create net-resolver thread\n"&#41;;
			sceKernelSleepThread&#40;&#41;;
		&#125;
		else
		&#123;
			printf&#40;"Net-resolver thread created\n"&#41;;
		&#125;

		sceKernelStartThread&#40;thid, 0, NULL&#41;;
		printf&#40;"Resolver thread started\n"&#41;;

		pspDebugScreenSetTextColor&#40;RED&#41;;
		printf&#40;"Press CIRCLE to Stop Resolver\n"&#41;;
		printf&#40;"Press START to continue\n"&#41;;
		pspDebugScreenSetTextColor&#40;WHITE&#41;;
		for &#40;;;&#41;
		&#123;		
			sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
			if &#40;oldButtons != pad.Buttons&#41; 
			&#123;
    				if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; 
				&#123;
					net_loop=1;
					printf&#40;"\nClose net-resolver Thread\n"&#41;;
					sceKernelTerminateDeleteThread&#40;thid&#41;;
					sceNetApctlDisconnect&#40;&#41;;
//					sceUtilityUnloadNetModule&#40;3&#41;;
//					sceUtilityUnloadNetModule&#40;1&#41;;
					break;
				&#125;
    				if &#40;pad.Buttons & PSP_CTRL_START&#41; 
				&#123;
					break;
				&#125;
			&#125;
			oldButtons = pad.Buttons;
			sceDisplayWaitVblankStart&#40;&#41;;
			sceKernelDelayThread&#40;50000&#41;;
		&#125;
		sceKernelDelayThread&#40;3*1000*1000&#41;;
	&#125;
&#125;

////////////////////////////////////////
// MAIN
///////////////////////////////////////
void main&#40;&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;

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

	res_port=80;
	run_resolver&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
&#125;

////////////////////////////////////////
// Calback formalities
///////////////////////////////////////
/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;
/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;
	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;
	sceKernelSleepThreadCB&#40;&#41;;
	return 0;
&#125;
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;
	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;
	return thid;
&#125;
Smong
Posts: 82
Joined: Tue Sep 04, 2007 4:44 am

Post by Smong »

This looks like a language/pointers problem:

Code: Select all

char server&#91;100&#93;;
...
   resolvd = PSP_gethostbyname&#40;&server&#41;;
   printf&#40;inet_ntoa&#40;resolvd&#41;&#41;;
   printf&#40;&server&#41;;
You shouldn't be using & there.

Also when using format strings it is good practise to pass string variables like this:

Code: Select all

printf&#40;"%s", server&#41;;
(+[__]%)
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

Thanks a lot for your help, and of course I apologize, I'm so tedious with my issues due to .... missing familiarity with C.....

Yes, there is a pointer problem, surely.

I would like like to pass to

gethostbyname(.....)

something that should work.

If I start from the string "www.pspdev.org" , which by means

Code: Select all

strcpy&#40;&server,"www.pspdev.org"&#41;;
but please, what can I initialize as right argument of gethostbyname through

Code: Select all

PSP_gethostbyname&#40;char *addr&#41;
??

is

Code: Select all

PSP_gethostbyname&#40;&server&#41;
wrong?

To read and write out hostent informations, I should use as follows, right?:

Code: Select all

      char **alias; 
      char *addr; 
      char buffer&#91;INET_ADDRSTRLEN&#93;;       
	if &#40;resolvd == NULL&#41; &#123; 
          printf&#40;"Errore di risoluzione"&#41;; 
      &#125; 
      printf&#40;"Canonical name %s\n", resolvd->h_name&#41;; 
      alias = resolvd->h_aliases; 
      while &#40;*alias != NULL&#41; &#123; 
          printf&#40;"Alias %s\n", *alias&#41;; 
          alias++; 
      &#125; 
      if &#40;resolvd->h_addrtype == AF_INET&#41; &#123; 
          printf&#40;"Address are IPv4\n"&#41;; 
      &#125; else if &#40;resolvd->h_addrtype == AF_INET6&#41; &#123; 
          printf&#40;"Address are IPv6\n"&#41;; 
      &#125; else &#123; 
          printf&#40;"Tipo di indirizzo non valido\n"&#41;; 
      &#125; 
      alias = resolvd->h_addr_list; 
      while &#40;*alias != NULL&#41; &#123; 
          addr = inet_ntop&#40;resolvd->h_addrtype, *alias, buffer, sizeof&#40;buffer&#41;&#41;; 
          printf&#40;"Indirizzo %s\n", addr&#41;; 
          alias++; 
      &#125;
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Wrong.
You need to be careful with arrays in c as they are already pointers.

Code: Select all

char server&#91;100&#93;;
strcpy&#40;server, "www.pspdev.org"&#41;; //NOTE&#58; no & as server is already a pointer to 100 char's
printf&#40;"%s", server"&#41;; // OR   printf&#40;server&#41;; //but the 2nd one is bad &#40;if the string contains a % character it will explode&#41;, NOTE&#58; no &
resolvd = PSP_gethostbyname&#40;server&#41;; // no & here
...
If you compile with the compiler command line option "-Wall" it should warn you when you have got your pointers messed up.
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

thanks to all both for answers and for your attention

I studied also the required missing includes and structures:

netdb.h for gethostbyname and hostent structure
arpa\inet.h for inet_ntop etc..
netinet\in.h for INET_ADDSTRLEN
\sys\socket.h for socket management (next step)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Well, one thing I see is you aren't creating ANY of the threads in user mode. For example, you have at one place:

Code: Select all

thid = sceKernelCreateThread&#40;"resolver_thread", resolver_thread, 16, 256*1024, 0, NULL&#41;;
See that "0" right before NULL? That's the thread attributes. You should set those to "PSP_THREAD_ATTR_USER" for 3.xx. You cannot spawn kernel threads from a user app in 3.xx. I figured that out while making an ogg player that worked in 3.xx.

People seem to constantly neglect that, and the VFPU attribute when making threads for handling graphics. The USER attr you can slide on with 1.50-based cfw, but not the VFPU, so they generally catch that one while missing the USER attr altogether.
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

J.F. wrote:Well, one thing I see is you aren't creating ANY of the threads in user mode. For example, you have at one place:

Code: Select all

thid = sceKernelCreateThread&#40;"resolver_thread", resolver_thread, 16, 256*1024, 0, NULL&#41;;
Yes sure, I'm using for 3.xx the following

Code: Select all

SceUID dlthread = sceKernelCreateThread&#40;"afkim_resolver", resolv_thread, 16, 256*1024, 0, NULL&#41;;
and I have no problems at all, in place.
(GoCam thread, Net_thread I'm using with that thread initialization, o instead of THREAD_ATTR_USER).

It was a rest of previous version.

I'm also studing CURL instead of this approach.

I'll let you, when finalized the applciation.

Thank jf (hope everything well with TTS Flite for pspsdk...)

EDIT:
from documentation:
PSP_THREAD_ATTR_USER Start the thread in user mode (done automatically if the thread creating it is in user mode).

It appears that iven if thread is created with "0", if creator thread is in user mode, it automatically is, too.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

mypspdev wrote: EDIT:
from documentation:
PSP_THREAD_ATTR_USER Start the thread in user mode (done automatically if the thread creating it is in user mode).

It appears that iven if thread is created with "0", if creator thread is in user mode, it automatically is, too.
Hmm... I'll have to try that ogg stuff again. I'm fairly certain it didn't work on my slim until I starting using the USER attr. Maybe it was something else I fixed at the same time.
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

I'm not able at all to run this porting of "resolver" based on gethostbyname .

I've been able to port under 3.xx the CURL example.
The test seems to work, now I've to understand what to do.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

This looks like a language/pointers problem:
Code:
char server[100];
...
resolvd = PSP_gethostbyname(&server);
printf(inet_ntoa(resolvd));
printf(&server);
You shouldn't be using & there.

Also when using format strings it is good practise to pass string variables like this:
Code:
printf("%s", server);
Wrong.
You need to be careful with arrays in c as they are already pointers.

Code:

char server[100];
strcpy(server, "www.pspdev.org"); //NOTE: no & as server is already a pointer to 100 char's
printf("%s", server"); // OR printf(server); //but the 2nd one is bad (if the string contains a % character it will explode), NOTE: no &
resolvd = PSP_gethostbyname(server); // no & here
...
What are you guys talking about?

Code: Select all

char array&#91;5&#93;;
char *b;
b = &array;
b = array;
are the same. Taking the address of an array returns a pointer to its first element. It's not the source of the OP's problem.

Jim
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Jim wrote: What are you guys talking about?

Code: Select all

char array&#91;5&#93;;
char *b;
b = &array;
b = array;
are the same. Taking the address of an array returns a pointer to its first element. It's not the source of the OP's problem.
Wow you are right, thats a weird one.
User avatar
tacoSunday
Posts: 34
Joined: Fri Aug 31, 2007 10:05 pm

Post by tacoSunday »

Huh, I haven't come across that one before!

Its a tricky one too, only applies if the array was declared on the stack. I would guess that is because an array only pretends to be a pointer when allocated on the stack. It's really just a hard coded index into the stack frame.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Eh? It applies for globals too.

The first of the pointers is type
char *
"pointer to char"
the other is type
char (*)[5]
"pointer to array of char"
It's bad programming though. Good C compilers will give a warning about incompatible pointer types possibly leading to undefined behaviour, and C++ compilers will refuse to compile it at all.

Jim
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

Hi to all!!

Here is a package with source code and makefile for a Resolver under 3.xx fw.

http://rapidshare.com/files/60685449/Re ... x.rar.html

Please if anybody would like to verify and try to finalize it, thanks.

My best to all
Post Reply