📄 lnet.cpp
字号:
/*
* LNet.cpp
*
* Created on: 2008-10-19
* Author: Administrator
*/
#include "../include/LNet.h"
LNet::LNet() {
conf = new lncfg;
getNetConfig(1);
initflage = 0;
// TODO Auto-generated constructor stub
}
LNet::~LNet() {
// TODO Auto-generated destructor stub
delete conf;
}
int LNet::LNetStart(char * LIp, uint16_t port, int config) {
//if (sceWlanDevIsPowerOn() != 1){
//return -2;
//}
if (LNetSetup() == 0) {
if (LNetconnect_to_apctl(config)==1) {
return LMakeSocket(LIp, port);
}
}
return -1;
}
int LNet::LMakeSocket(char * ip, uint16_t port) {
int tmpsock, i;
tmpsock = socket(PF_INET, SOCK_STREAM, 0); // make socket
if (tmpsock < 0) {
return -1;
}
m_LSocket.sin_family = AF_INET;
m_LSocket.sin_port = htons(port);
m_LSocket.sin_addr.s_addr = inet_addr(ip);
if (fcntl(tmpsock, F_SETFL, FNONBIO) == -1) {
close(tmpsock);
return (-2);
}
if (connect(tmpsock, (struct sockaddr *) &m_LSocket, sizeof(m_LSocket)) < 0) {
if ((errno!= EWOULDBLOCK) && (errno!= EINPROGRESS)) {
/* 如果错误代码是EWOULDBLOCK和EINPROGRESS,则不用关闭套接字,因为系统将在之后继续为套接字建立连接,连接是否建立成功可用select()函数来检测套接字是否“可写”来确定。*/
close(tmpsock);
return (-3); /* Connect error. */
}
}
FD_SET(tmpsock, &Mysock.readfds);
FD_SET(tmpsock, &Mysock.writefds);
FD_SET(tmpsock, &Mysock.exceptfds);
i = 0;
while (Mysock.Sockets[i] != 0)
i++; /* look for a blank sockets position */
if (i >= 8) {
close(tmpsock);
return (-4); /* too many connections */
}
Mysock.Sockets[i] = tmpsock;
Mysock.SockNum++;
return (i);
}
int LNet::LNetSetup() {
// 加载网络module
if(initflage == 0){
if (sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON)) {
// pspprintf("[-] Unable to load common net module\n");
printf("[-] Unable to load common net module\n");
return 1;
}
initflage++;
}
if(initflage == 1){
if (sceUtilityLoadNetModule(PSP_NET_MODULE_INET)) // load inet
{
// pspprintf("[-] Unable to load inet module\n");
printf("[-] Unable to load common net module\n");
return 1;
}
initflage++;
}
if(initflage == 2){
if (sceNetInit(0x20000, 0x20, 0x1000, 0x20, 0x1000)) {
// pspprintf("[-] Error when sceNetInit()\n");
printf("[-] Unable to load common net module\n");
return 1;
}
initflage++;
}
if(initflage == 3){
if (sceNetInetInit()) {
// pspprintf("[-] Error when sceNetInetInit()\n");
printf("[-] Unable to load common net module\n");
return 1;
}
initflage++;
}
if(initflage == 4){
if (sceNetApctlInit(0x1400, 0x42)) { // increase stack size
// pspprintf("[-] Error when sceNetApctlInit()\n");
printf("[-] Unable to load common net module\n");
return 1;
}
initflage++;
}
return 5-initflage;
}
int LNet::LNetconnect_to_apctl(int config) {
int err;
int stateLast = -1;
/* Connect using the first profile */
err = sceNetApctlConnect(config);
if (err != 0) {
// pspprintf(": sceNetApctlConnect returns %08X\n", err);
return 0;
}
// printf(": Connecting...\n");
while (1) {
int state;
err = sceNetApctlGetState(&state);
if (err != 0) {
// pspprintf(": sceNetApctlGetState returns $%x\n", err);
break;
}
if (state > stateLast) {
// pspprintf(" connection state %d of 4\n", state);
stateLast = state;
}
if (state < stateLast) {
printf("can not conn with config %d\n", config);
return 0;
}
if (state == 4)
break; // connected with static IP
// wait a little before polling again
sceKernelDelayThread(50 * 1000); // 50ms
}
// pspprintf(": Connected!\n");
if (err != 0) {
return 0;
}
return 1;
}
int LNet::LNetRecvBuff(int Sockno, char *buf, int size) {
int actlen;
if ((Sockno >= 8) || (Sockno < 0) || (Mysock.Sockets[Sockno] == 0))
return (0);
if ((actlen = recv(Mysock.Sockets[Sockno], buf, size, 0)) < 0)
return (0 - errno);
return (actlen); /* actlen是接收的数据长度,如果为零,指示连接被对方关闭。*/
}
int LNet::LNetSendBuff(int Sockno, char * buff, int len) {
/* Send a packet. If succeed return the number of send data, else return -1 */
int actlen;
if ((Sockno >= 8) || (Sockno < 0) || (Mysock.Sockets[Sockno] == 0))
return (0);
if ((actlen = send(Mysock.Sockets[Sockno], buff, len, 0)) < 0)
return (-1);
return (actlen);
}
int LNet::QuerySocketsMsg()
/* Query Sockets Message. If succeed return message number, else return -1.
The message information stored in struct SockMsg. */
{
fd_set rfds, wfds, efds;
int retcode, i;
struct timeval TimeOut;
rfds = Mysock.readfds;
wfds = Mysock.writefds;
efds = Mysock.exceptfds;
TimeOut.tv_sec = 0; /* 立即返回,不阻塞。*/
TimeOut.tv_usec = 0;
bzero((char *) &SockMsg, sizeof(SockMsg));
if ((retcode = select(8, &rfds, &wfds, &efds, &TimeOut)) == 0)
return (0);
if (FD_ISSET(Mysock.DaemonSock, &rfds))
SockMsg.AcceptNum = 1; /* some client call server. */
for (i = 0; i < 8; i++) /* Data in message */
{
if ((Mysock.Sockets[i] > 0) && (FD_ISSET(Mysock.Sockets[i], &rfds)))
SockMsg.ReadQueue[SockMsg.ReadNum++] = i;
}
for (i = 0; i < 8; i++) /* Data out ready message */
{
if ((Mysock.Sockets[i] > 0) && (FD_ISSET(Mysock.Sockets[i], &wfds)))
SockMsg.WriteQueue[SockMsg.WriteNum++] = i;
}
if (FD_ISSET(Mysock.DaemonSock, &efds))
SockMsg.AcceptNum = -1; /* server socket error. */
for (i = 0; i < 8; i++) /* Error message */
{
if ((Mysock.Sockets[i] > 0) && (FD_ISSET(Mysock.Sockets[i], &efds)))
SockMsg.ExceptQueue[SockMsg.ExceptNum++] = i;
}
return (retcode);
}
int LNet::CloseConnection(int Sockno)
/* Close a connection indicated by Sockno. */
{
int retcode;
if ((Sockno >= 8) || (Sockno < 0) || (Mysock.Sockets[Sockno] == 0))
return (0);
retcode = close(Mysock.Sockets[Sockno]);
FD_CLR(Mysock.Sockets[Sockno], &Mysock.readfds);
FD_CLR(Mysock.Sockets[Sockno], &Mysock.writefds);
FD_CLR(Mysock.Sockets[Sockno], &Mysock.exceptfds);
Mysock.Sockets[Sockno] = 0;
Mysock.SockNum--;
return (retcode);
}
int LNet::getconfAcount() {
int numNetConfigs = 1;
while (sceUtilityCheckNetParam(numNetConfigs) == 0) {
numNetConfigs++;
}
numNetConfigs--;
return numNetConfigs;
}
void LNet::getNetConfig(int config) {
conf->netConfIndex = config;
netData data;
data.asUint = 0xBADF00D;
memset(&data.asString[4], 0, 124);
sceUtilityGetNetParam(config, 0, &data);
conf->name = data;
data.asUint = 0xBADF00D;
memset(&data.asString[4], 1, 124);
sceUtilityGetNetParam(config, 0, &data);
conf->ssid = data;
data.asUint = 0xBADF00D;
memset(&data.asString[4], 5, 124);
sceUtilityGetNetParam(config, 0, &data);
conf->localip = data;
data.asUint = 0xBADF00D;
memset(&data.asString[4], 6, 124);
sceUtilityGetNetParam(config, 0, &data);
conf->subnet = data;
data.asUint = 0xBADF00D;
memset(&data.asString[4], 7, 124);
sceUtilityGetNetParam(config, 0, &data);
conf->gateway = data;
}
char * LNet::getName() {
return conf->name.asString;
}
char * LNet::getSSID() {
return conf->ssid.asString;
}
char * LNet::getGateway() {
return conf->gateway.asString;
}
parsedmsg LNet::getmsg(char * buf) {
parsedmsg msg;
char msgcode[11];
char ch;
int count = 0;
msg.errcode = 0;
msg.optcode = 0;
msg.ctlcode = 0;
int i = 0;
int z = 0;
int y = 0;
for (i = 0; i < 9; i++) {
msgcode[i] = buf[count++];
z = (int) msgcode[i];
y = z - 48;
if (i == 0)
msg.errcode += (y * 100);
if (i == 1)
msg.errcode += (y * 10);
if (i == 2)
msg.errcode += (y * 1);
if (i == 3)
msg.optcode += (y * 100);
if (i == 4)
msg.optcode += (y * 10);
if (i == 5)
msg.optcode += (y * 1);
if (i == 6)
msg.ctlcode += (y * 100);
if (i == 7)
msg.ctlcode += (y * 10);
if (i == 8)
msg.ctlcode += (y * 1);
}
int j = 0;
while ((ch = buf[count]) != 0) {
count += 1;
msg.databuf[j++] = ch;
}
return msg;
}
char * LNet::putpacket(parsedmsg * msg) {
static char outputbuffer[4096];
char checksum;
int count;
char ch;
int i = 0;
checksum = 0;
count = 0;
char errcode[3];
sprintf(errcode, "%03d", msg->errcode);
char optcode[3];
sprintf(optcode, "%03d", msg->optcode);
char ctlcode[3];
sprintf(ctlcode, "%03d", msg->ctlcode);
while ((ch = errcode[count]) != 0) {
checksum += ch;
count += 1;
outputbuffer[i++] = ch;
}
count = 0;
while ((ch = optcode[count]) != 0) {
checksum += ch;
count += 1;
outputbuffer[i++] = ch;
}
count = 0;
while ((ch = ctlcode[count]) != 0) {
checksum += ch;
count += 1;
outputbuffer[i++] = ch;
}
count = 0;
while ((ch = msg->databuf[count]) != 0) {
checksum += ch;
count += 1;
outputbuffer[i++] = ch;
}
return outputbuffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -