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("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;
}