 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
Posted: Fri Oct 05, 2007 7:47 am Post subject: Resolver under 3.xx |
|
|
Please, anything wrong in the following example?
thanks
| Code: | ////////////////////////////////
// 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("Resolver 3xx", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
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[100];
int port;
char mNickname[100];
int status;
int sock;
int net_loop=0;
int MyThread=0;
char res_server[50];
res_port=80;
////////////////////////////////////////
// RESOLVE THREAD
///////////////////////////////////////
int resolvThread(SceSize args, void *argp)
{
PSP_resolvd = gethostbyname(PSP_strtoresolv);
return 0;
}
struct hostent * PSP_gethostbyname(char* addr)
{
PSP_strtoresolv = addr;
PSP_resolvd = NULL;
int a;
for (a = 0; a < 4 && PSP_resolvd==NULL; a++) //try up to 4 times
{
SceUID dlthread = sceKernelCreateThread("afkim_resolver", resolvThread, 0x18, 0x10000, 0, NULL);
sceKernelStartThread(dlthread, 0, NULL);
unsigned int startTime = sceKernelGetSystemTimeLow();
do
{
sceKernelDelayThread(1000*1000);
}
while (PSP_resolvd == NULL && startTime+1000*1000 >= sceKernelGetSystemTimeLow());
if (PSP_resolvd == NULL)
{
// printf("\nNot possible to resolve server address\n");
printf(". ");
}
else
{
printf("Server address resolved!\n");
}
int ret = sceKernelTerminateDeleteThread(dlthread);
if (ret < 0)
{
printf("Error terminating and deleting resolver Thread\n");
}
else
{
// printf("Resolver Thread terminated and deleted\n");
}
}
return PSP_resolvd;
}
int doConnect()
{
//Disconnect if connected.
if (status != CS_OFFLINE)
{
doDisconnect();
}
strcpy(&server ,"www.google.com");
status = CS_CONNECTING;
//**********************************************
//resolve: LOOK HERE please
struct hostent *resolvd;
resolvd = PSP_gethostbyname(&server);
printf(inet_ntoa(resolvd));
printf(&server);
if (resolvd == NULL)
{
status = CS_OFFLINE;
return 0;
}
return 1;
//**********************************************
}
void doDisconnect()
{
close(sock);
sock = 0;
status = CS_OFFLINE;
}
////////////////////////////////////////
// RESOLVER
///////////////////////////////////////
int connect_to_apctl(int config)
{
int err;
int stateLast = -1;
printf("Wireless Network Connection to apctl\n");
err = sceNetApctlConnect(config);
if (err != 0)
{
printf("sceNetApctlConnect returns %08X\n", err);
return 0;
}
else
{
printf("sceNetApctlConnect ok\n");
}
printf("Connecting...\n");
while (1)
{
int state;
err = sceNetApctlGetState(&state);
if (err != 0)
{
printf("sceNetApctlGetState returns $%x\n", err);
break;
}
if (state > stateLast)
{
printf(" connection state %d of 4\n", state);
stateLast = state;
}
if (state == 4)
break; // connected with static IP
sceKernelDelayThread(50*1000); // 50ms
}
printf("Connected!\n");
if(err != 0)
{
return 0;
}
return 1;
}
int resolver_thread(SceSize args, void *argp)
{
int err;
int retconn;
char* res_server;
char* nickname;
int port;
printf("Wireless Network Thread\n");
do
{
if((err = pspSdkInetInit()))
{
printf("Could not initialise the network\n");
printf("may you already started the Server or the Client before\n");
printf("please for running the resolver restart the program.\n");
break;
}
else
{
printf("Network initialized\n");
}
if(connect_to_apctl(1))
{
char szMyIPAddr[32];
if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
{
strcpy(szMyIPAddr, "unknown IP address");
}
else
{
printf("IP Address ok\n");
}
retconn=doConnect();
break;
}
else
{
printf("No connect_to_apctl\n");
}
}
while(net_loop);
printf("\nClose client\n");
return 0;
}
void run_resolver()
{
SceUID thid;
u32 oldButtons = 0;
pspDebugScreenInit();
pspDebugScreenClear();
pspDebugScreenSetXY(0, 1);
pspDebugScreenSetTextColor(GREEN);
printf("Test Resolver of www.yahoo.it\n");
pspDebugScreenSetTextColor(WHITE);
sceKernelDelayThread(1000*1000);
pspDebugScreenSetTextColor(RED);
printf("Press CROSS to Start Resolver\n");
printf("Press START to continue\n");
pspDebugScreenSetTextColor(WHITE);
for (;;)
{
sceCtrlReadBufferPositive(&pad, 1);
if (oldButtons != pad.Buttons)
{
if (pad.Buttons & PSP_CTRL_CROSS)
{
MyThread=1;
break;
}
if (pad.Buttons & PSP_CTRL_START)
{
MyThread=0;
break;
}
}
oldButtons = pad.Buttons;
sceDisplayWaitVblankStart();
sceKernelDelayThread(50000);
}
if (MyThread==1)
{
sceUtilityLoadNetModule(1);
sceUtilityLoadNetModule(3);
net_loop=0;
thid = sceKernelCreateThread("resolver_thread", resolver_thread, 16, 256*1024, 0, NULL);
if(thid < 0)
{
printf("Error, could not create net-resolver thread\n");
sceKernelSleepThread();
}
else
{
printf("Net-resolver thread created\n");
}
sceKernelStartThread(thid, 0, NULL);
printf("Resolver thread started\n");
pspDebugScreenSetTextColor(RED);
printf("Press CIRCLE to Stop Resolver\n");
printf("Press START to continue\n");
pspDebugScreenSetTextColor(WHITE);
for (;;)
{
sceCtrlReadBufferPositive(&pad, 1);
if (oldButtons != pad.Buttons)
{
if (pad.Buttons & PSP_CTRL_CIRCLE)
{
net_loop=1;
printf("\nClose net-resolver Thread\n");
sceKernelTerminateDeleteThread(thid);
sceNetApctlDisconnect();
// sceUtilityUnloadNetModule(3);
// sceUtilityUnloadNetModule(1);
break;
}
if (pad.Buttons & PSP_CTRL_START)
{
break;
}
}
oldButtons = pad.Buttons;
sceDisplayWaitVblankStart();
sceKernelDelayThread(50000);
}
sceKernelDelayThread(3*1000*1000);
}
}
////////////////////////////////////////
// MAIN
///////////////////////////////////////
void main()
{
SetupCallbacks();
pspDebugScreenInit();
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
res_port=80;
run_resolver();
sceKernelExitGame();
}
////////////////////////////////////////
// Calback formalities
///////////////////////////////////////
/* 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;
} |
|
|
| Back to top |
|
 |
Smong
Joined: 04 Sep 2007 Posts: 82
|
Posted: Fri Oct 05, 2007 8:27 pm Post subject: |
|
|
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); |
_________________ (+[__]%) |
|
| Back to top |
|
 |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
Posted: Fri Oct 05, 2007 9:26 pm Post subject: |
|
|
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: | | strcpy(&server,"www.pspdev.org"); |
but please, what can I initialize as right argument of gethostbyname through | Code: | | PSP_gethostbyname(char *addr) | ??
is | Code: | | PSP_gethostbyname(&server) | wrong?
To read and write out hostent informations, I should use as follows, right?:
| Code: | char **alias;
char *addr;
char buffer[INET_ADDRSTRLEN];
if (resolvd == NULL) {
printf("Errore di risoluzione");
}
printf("Canonical name %s\n", resolvd->h_name);
alias = resolvd->h_aliases;
while (*alias != NULL) {
printf("Alias %s\n", *alias);
alias++;
}
if (resolvd->h_addrtype == AF_INET) {
printf("Address are IPv4\n");
} else if (resolvd->h_addrtype == AF_INET6) {
printf("Address are IPv6\n");
} else {
printf("Tipo di indirizzo non valido\n");
}
alias = resolvd->h_addr_list;
while (*alias != NULL) {
addr = inet_ntop(resolvd->h_addrtype, *alias, buffer, sizeof(buffer));
printf("Indirizzo %s\n", addr);
alias++;
} |
|
|
| Back to top |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
Posted: Fri Oct 05, 2007 9:42 pm Post subject: |
|
|
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
...
|
If you compile with the compiler command line option "-Wall" it should warn you when you have got your pointers messed up. |
|
| Back to top |
|
 |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
Posted: Fri Oct 05, 2007 10:18 pm Post subject: |
|
|
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) |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Oct 05, 2007 10:26 pm Post subject: |
|
|
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: | | thid = sceKernelCreateThread("resolver_thread", resolver_thread, 16, 256*1024, 0, NULL); |
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. |
|
| Back to top |
|
 |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
Posted: Fri Oct 05, 2007 10:32 pm Post subject: |
|
|
| 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: | | thid = sceKernelCreateThread("resolver_thread", resolver_thread, 16, 256*1024, 0, NULL); |
|
Yes sure, I'm using for 3.xx the following
| Code: | | SceUID dlthread = sceKernelCreateThread("afkim_resolver", resolv_thread, 16, 256*1024, 0, NULL); |
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. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat Oct 06, 2007 2:36 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
Posted: Sat Oct 06, 2007 4:55 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sat Oct 06, 2007 8:38 am Post subject: |
|
|
| Quote: | 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);
|
| Quote: |
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: |
char array[5];
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 _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
Posted: Sat Oct 06, 2007 1:25 pm Post subject: |
|
|
| Jim wrote: |
What are you guys talking about?
| Code: |
char array[5];
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. |
|
| Back to top |
|
 |
tacoSunday

Joined: 31 Aug 2007 Posts: 34
|
Posted: Sat Oct 06, 2007 4:29 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sat Oct 06, 2007 6:01 pm Post subject: |
|
|
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 _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
mypspdev
Joined: 11 Jul 2007 Posts: 178
|
|
| Back to top |
|
 |
|
|
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
|