Exemplo n.º 1
0
int _main(void)
{
	// Init and resolve libraries
	initKernel();
	initLibc();
	initNetwork();
	initPthread();

	// Init netdebug
	struct sockaddr_in server;
	server.sin_family = sceNetHtons(AF_INET);
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));

	netdbg_sock = sceNetSocket("netdebug", AF_INET, SOCK_STREAM, 0);
	sceNetConnect(netdbg_sock, (struct sockaddr *)&server, sizeof(server));


	ftp_init(PS4_IP, PS4_PORT);

	//INFO("PS4 listening on IP %s Port %i\n", PS4_IP, PS4_PORT);

	while (1) {
		sceKernelUsleep(100 * 1000);
	}

	ftp_fini();

	return 0;
}
Exemplo n.º 2
0
int _main(void) {
	// Pass address of a syscall gadget in rcx
	register f rcx asm("rcx");
	directSyscall = rcx;
	
	
	// Init and resolve libraries
	initLibc();
	initNetwork();
	
	
	// Connect to server and send message
	char socketName[] = "debug";

	struct sockaddr_in server;

	server.sin_family = sceNetHtons(AF_INET);
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));

	int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
	sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));
	

	char buffer[512];
	struct dirent *dent;
	
	int dfd = open("/", O_RDONLY, 0);
	if(dfd < 0) {
		debug(sock, "Invalid directory.\n");
		return 1;
	}
	
	while(getdents(dfd, buffer, sizeof(buffer)) != 0) {
        dent = (struct dirent *)buffer;

        while(dent->d_fileno) {
                debug(sock, "%s\n", dent->d_name);

                dent = (struct dirent *)((void *)dent + dent->d_reclen);
        }
		
		memset(buffer, 0, sizeof(buffer));
	}
	
	
	// Return to browser
	close(dfd);
	sceNetSocketClose(sock);
	
	return 0;
}
Exemplo n.º 3
0
int ps4LinkLseek(int fd, int offset, int whence)	
{
	ps4link_pkt_lseek_req *lseekreq;
	ps4link_pkt_file_rly *lseekrly;


	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] file lseek req (fd: %d)\n", fd);

	lseekreq = (ps4link_pkt_lseek_req *)&send_packet[0];
	lseekrly = (ps4link_pkt_file_rly *)&recv_packet[0];

	lseekreq->cmd = sceNetHtonl(PS4LINK_LSEEK_CMD);
	lseekreq->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_lseek_req));
	lseekreq->fd = sceNetHtonl(fd);
	lseekreq->offset = sceNetHtonl(offset);
	lseekreq->whence = sceNetHtonl(whence);

	if(ps4link_send(ps4LinkGetValue(FILEIO_SOCK), lseekreq, sizeof(ps4link_pkt_lseek_req), MSG_DONTWAIT) < 0) {
		return -1;
	}

	if(!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), (char *)lseekrly,sizeof(ps4link_pkt_file_rly), PS4LINK_LSEEK_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_lseek_file: did not receive PS4LINK_LSEEK_RLY\n");
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_lseek_file: lseek reply received (ret %d)\n",sceNetNtohl(lseekrly->retval));

	return sceNetNtohl(lseekrly->retval);
	
}
Exemplo n.º 4
0
int ps4LinkRmdir(const char *dirname)
{
	ps4link_pkt_rmdir_req *rmdirreq;
	ps4link_pkt_file_rly *rmdirrly;

	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_file: remove dir req (%s)\n", dirname);

	rmdirreq = (ps4link_pkt_rmdir_req *)&send_packet[0];

	// Build packet
	rmdirreq->cmd = sceNetHtonl(PS4LINK_RMDIR_CMD);
	rmdirreq->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_rmdir_req));
	strncpy(rmdirreq->name, dirname, PS4LINK_MAX_PATH);
	rmdirreq->name[PS4LINK_MAX_PATH - 1] = 0; // Make sure it's null-terminated

	if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), rmdirreq, sizeof(ps4link_pkt_rmdir_req), MSG_DONTWAIT) < 0) {
		return -1;
	}

	if (!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), recv_packet, sizeof(ps4link_pkt_file_rly), PS4LINK_RMDIR_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_file: ps4link_rmdir: did not receive RMDIR_RLY\n");
		return -1;
	}

    rmdirrly = (ps4link_pkt_file_rly *)recv_packet;
	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_file: remove dir reply received (ret %d)\n", sceNetNtohl(rmdirrly->retval));
	return sceNetNtohl(rmdirrly->retval);
}
Exemplo n.º 5
0
int ps4LinkDopen(const char *dirname)
{
	ps4link_pkt_open_req *openreq;
	ps4link_pkt_file_rly *openrly;

	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] dir open req (%s)\n", dirname);

	openreq = (ps4link_pkt_open_req *)&send_packet[0];

	// Build packet
	openreq->cmd = sceNetHtonl(PS4LINK_OPENDIR_CMD);
	openreq->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_open_req));
 	openreq->flags = sceNetHtonl(0);
	strncpy(openreq->path, dirname, PS4LINK_MAX_PATH);
	openreq->path[PS4LINK_MAX_PATH - 1] = 0; // Make sure it's null-terminated

	if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), openreq, sizeof(ps4link_pkt_open_req), MSG_DONTWAIT) < 0) {
		return -1;
	}

	if (!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), recv_packet, sizeof(ps4link_pkt_file_rly), PS4LINK_OPENDIR_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_open_dir: did not receive OPENDIR_RLY\n");
		return -1;
	}

	openrly = (ps4link_pkt_file_rly *)recv_packet;
    
	debugNetPrintf(DEBUG,"[PS4LINK] dir open reply received (ret %d)\n", sceNetNtohl(openrly->retval));

	return sceNetNtohl(openrly->retval);
}
Exemplo n.º 6
0
int ps4LinkDclose(int fd)	
{
 	ps4link_pkt_close_req *closereq;
	ps4link_pkt_file_rly *closerly;


	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_file: dir close req (fd: %d)\n", fd);

	closereq = (ps4link_pkt_close_req *)&send_packet[0];
	closerly = (ps4link_pkt_file_rly *)&recv_packet[0];

	closereq->cmd = sceNetHtonl(PS4LINK_CLOSEDIR_CMD);
	closereq->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_close_req));
	closereq->fd = sceNetHtonl(fd);

	if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), closereq, sizeof(ps4link_pkt_close_req), MSG_DONTWAIT) < 0) {
		return -1;
	}

	if(!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), (char *)closerly, sizeof(ps4link_pkt_file_rly), PS4LINK_CLOSEDIR_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_close_dir: did not receive PS4LINK_CLOSEDIR_RLY\n");
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] dir close reply received (ret %d)\n", sceNetNtohl(closerly->retval));

	return sceNetNtohl(closerly->retval);
}
Exemplo n.º 7
0
void console_init()
{
	SceNetInitParam initparam;

	if (console_initialzed) {
		return;
	}

	console_mtx = sceKernelCreateMutex("console_mutex", 0, 0, NULL);

	if (sceSysmoduleIsLoaded(SCE_SYSMODULE_NET) != SCE_SYSMODULE_LOADED)
		sceSysmoduleLoadModule(SCE_SYSMODULE_NET);

	if (sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT) {
		net_memory = malloc(NET_INIT_SIZE);

		initparam.memory = net_memory;
		initparam.size = NET_INIT_SIZE;
		initparam.flags = 0;

		sceNetInit(&initparam);
	}

	sock = sceNetSocket("netdbg", SCE_NET_AF_INET, SCE_NET_SOCK_DGRAM, 0);
	memset(&target, 0, sizeof(target));
	target.sin_family = SCE_NET_AF_INET;
	target.sin_port   = sceNetHtons(3490);

	sceNetInetPton(SCE_NET_AF_INET, "192.168.1.104", &target.sin_addr);

	console_initialzed = 1;
}
Exemplo n.º 8
0
int _main(void) {
	// Init and resolve libraries
	initKernel();
	
	initLibc();
	initNetwork();


	// Connect to server and send message
	char socketName[] = "debug";

	struct sockaddr_in server;

	server.sin_len = sizeof(server);
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));

	int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
	sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));

	debug(sock, "PID: %d", syscall(20));

	sceNetSocketClose(sock);


	// Return to browser
	return 0;
}
Exemplo n.º 9
0
static void cmd_PASV_func(ClientInfo *client)
{
	int ret;
	UNUSED(ret);

	char cmd[512];
	unsigned int namelen;
	SceNetSockaddrIn picked;

	/* Create data mode socket name */
	char data_socket_name[64];
	sprintf(data_socket_name, "FTPVita_client_%i_data_socket",
		client->num);

	/* Create the data socket */
	client->data_sockfd = sceNetSocket(data_socket_name,
		PSP2_NET_AF_INET,
		PSP2_NET_SOCK_STREAM,
		0);

	DEBUG("PASV data socket fd: %d\n", client->data_sockfd);

	/* Fill the data socket address */
	client->data_sockaddr.sin_family = PSP2_NET_AF_INET;
	client->data_sockaddr.sin_addr.s_addr = sceNetHtonl(PSP2_NET_INADDR_ANY);
	/* Let the PSVita choose a port */
	client->data_sockaddr.sin_port = sceNetHtons(0);

	/* Bind the data socket address to the data socket */
	ret = sceNetBind(client->data_sockfd,
		(SceNetSockaddr *)&client->data_sockaddr,
		sizeof(client->data_sockaddr));
	DEBUG("sceNetBind(): 0x%08X\n", ret);

	/* Start listening */
	ret = sceNetListen(client->data_sockfd, 128);
	DEBUG("sceNetListen(): 0x%08X\n", ret);

	/* Get the port that the PSVita has chosen */
	namelen = sizeof(picked);
	sceNetGetsockname(client->data_sockfd, (SceNetSockaddr *)&picked,
		&namelen);

	DEBUG("PASV mode port: 0x%04X\n", picked.sin_port);

	/* Build the command */
	sprintf(cmd, "227 Entering Passive Mode (%hhu,%hhu,%hhu,%hhu,%hhu,%hhu)\n",
		(vita_addr.s_addr >> 0) & 0xFF,
		(vita_addr.s_addr >> 8) & 0xFF,
		(vita_addr.s_addr >> 16) & 0xFF,
		(vita_addr.s_addr >> 24) & 0xFF,
		(picked.sin_port >> 0) & 0xFF,
		(picked.sin_port >> 8) & 0xFF);

	client_send_ctrl_msg(client, cmd);

	/* Set the data connection type to passive! */
	client->data_con_type = FTP_DATA_CONNECTION_PASSIVE;
}
Exemplo n.º 10
0
uint16_t inet_htons(uint16_t hostshort)
{
#if defined(VITA) || defined(__ORBIS__)
   return sceNetHtons(hostshort);
#else
   return htons(hostshort);
#endif
}
Exemplo n.º 11
0
int _main(void) {
	// Init and resolve libraries
	initKernel();
	
	initLibc();
	initNetwork();
	
	
	// Connect to server
	char socketName[] = "debug";
	
	struct sockaddr_in server;
	
	server.sin_len = sizeof(server);
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));
	
	int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
	sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));
	
	
	// Get font path
	char path[256] = "/";
	int length = 11;
	getSandboxDirectory(path + 1, &length);
	strcpy(path + 11, "/common/font/DFHEI5-SONY.ttf");
	
	
	// Open for reading, and get size
	int fd = open(path, O_RDONLY, 0);
	
	struct stat s;
	fstat(fd, &s);
	
	
	// Allocate buffer and read
	char *buffer = malloc(s.st_size);
	
	read(fd, buffer, s.st_size);
	
	close(fd);
	
	
	// Send
	sceNetSend(sock, buffer, s.st_size, 0);
	
	free(buffer);
	
	sceNetSocketClose(sock);
	
	
	// Return to browser
	return 0;
}
Exemplo n.º 12
0
void blob_transfer_init()
{
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_len = sizeof(server);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = BLOBS_ADDR;
    server.sin_port = sceNetHtons(BLOBS_PORT);

    blobs_sockfd = sceNetSocket("blobs", AF_INET, SOCK_STREAM, 0);
    sceNetConnect(blobs_sockfd, (struct sockaddr *)&server, sizeof(server));
}
Exemplo n.º 13
0
int _main(void) {
	// Init and resolve libraries
	initLibc();
	initNetwork();
	
	
	// Connect to server
	char socketName[] = "debug";

	struct sockaddr_in server;

	server.sin_len = sizeof(server);
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));

	int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
	sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));

	
	// Read / directory
	char buffer[512];
	struct dirent *dent;

	int dfd = open("/", O_RDONLY, 0);
	if(dfd < 0) {
		debug(sock, "Invalid directory.\n");
		return 1;
	}

	while(getdents(dfd, buffer, sizeof(buffer)) != 0) {
		dent = (struct dirent *)buffer;

		while(dent->d_fileno) {
				debug(sock, "[%s]: %s\n", entryName(dent->d_type), dent->d_name);

				dent = (struct dirent *)((void *)dent + dent->d_reclen);
		}

		memset(buffer, 0, sizeof(buffer));
	}


	// Return to browser
	close(dfd);
	sceNetSocketClose(sock);

	return 0;
}
Exemplo n.º 14
0
static void cmd_PORT_func(ClientInfo *client)
{
	unsigned char data_ip[4];
	unsigned char porthi, portlo;
	unsigned short data_port;
	char ip_str[16];
	SceNetInAddr data_addr;

	sscanf(client->recv_buffer, "%*s %hhu,%hhu,%hhu,%hhu,%hhu,%hhu",
		&data_ip[0], &data_ip[1], &data_ip[2], &data_ip[3],
		&porthi, &portlo);

	data_port = portlo + porthi*256;

	/* Convert to an X.X.X.X IP string */
	sprintf(ip_str, "%d.%d.%d.%d",
		data_ip[0], data_ip[1], data_ip[2], data_ip[3]);

	/* Convert the IP to a SceNetInAddr */
	sceNetInetPton(PSP2_NET_AF_INET, ip_str, &data_addr);

	DEBUG("PORT connection to client's IP: %s Port: %d\n", ip_str, data_port);

	/* Create data mode socket name */
	char data_socket_name[64];
	sprintf(data_socket_name, "FTPVita_client_%i_data_socket",
		client->num);

	/* Create data mode socket */
	client->data_sockfd = sceNetSocket(data_socket_name,
		PSP2_NET_AF_INET,
		PSP2_NET_SOCK_STREAM,
		0);

	DEBUG("Client %i data socket fd: %d\n", client->num,
		client->data_sockfd);

	/* Prepare socket address for the data connection */
	client->data_sockaddr.sin_family = PSP2_NET_AF_INET;
	client->data_sockaddr.sin_addr = data_addr;
	client->data_sockaddr.sin_port = sceNetHtons(data_port);

	/* Set the data connection type to active! */
	client->data_con_type = FTP_DATA_CONNECTION_ACTIVE;

	client_send_ctrl_msg(client, "200 PORT command successful!\n");
}
Exemplo n.º 15
0
int ps4LinkDread(int fd, struct dirent *dir)
{
	ps4link_pkt_dread_req *dirreq;
	ps4link_pkt_dread_rly *dirrly;
	struct dirent *dirent;

	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] dir read req (%x)\n", fd);

	dirreq = (ps4link_pkt_dread_req *)&send_packet[0];

	// Build packet
	dirreq->cmd = sceNetHtonl(PS4LINK_READDIR_CMD);
	dirreq->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_dread_req));
	dirreq->fd = sceNetHtonl(fd);

	if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), dirreq, sizeof(ps4link_pkt_dread_req), MSG_DONTWAIT) < 0) {
		return -1;
	}

	if (!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), recv_packet, sizeof(ps4link_pkt_dread_rly), PS4LINK_READDIR_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_dir: did not receive OPENDIR_RLY\n");
		return -1;
	}

	dirrly = (ps4link_pkt_dread_rly *)recv_packet;
    
	debugNetPrintf(DEBUG,"[PS4LINK] dir read reply received (ret %d)\n", sceNetNtohl(dirrly->retval));

	
	dirent = (struct dirent *) dir;
	   
	strncpy(dirent->d_name,dirrly->name,256);
	dirent->d_type=dirrly->type;

	return sceNetNtohl(dirrly->retval);
}
Exemplo n.º 16
0
int _main(void) {
	// Init and resolve libraries
	initLibc();
	initNetwork();
	initPad();
	
	
	// Connect to debug server
	char socketName[] = "debug";
	
	struct sockaddr_in server;
	
	server.sin_len = sizeof(server);
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = IP(192, 168, 0, 4);
	server.sin_port = sceNetHtons(9023);
	memset(server.sin_zero, 0, sizeof(server.sin_zero));
	
	int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
	sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));
	
	
	// Browser /user/home/ to see all user IDs (in hex)
	int userID = 0x10000000;
	
	int pad = scePadOpen(userID, 0, 0, NULL);
	
	while(1) {
		scePadReadState(pad, data);
		sceNetSend(sock, data, 0x60, 0);
	}
	
	
	// Return to browser
	scePadClose(pad);
	sceNetSocketClose(sock);
	
	return 0;
}
Exemplo n.º 17
0
/* connecthostport()
 * return a socket connected (TCP) to the host and port
 * or -1 in case of error */
int connecthostport(const char * host, unsigned short port)
{
	printk("Connect Attempt: %s:%u\n", host, port);
	// Target Address
	SceNetInetSockaddrIn in;
	
	// Clear Memory
	memset(&in, 0, sizeof(in));
	
	// Set Address Family
	in.sin_family = AF_INET;
	
	// Set Structure Length
	in.sin_len = sizeof(in);
	
	// Translate Port
	in.sin_port = sceNetHtons(port);
	
	// Resolve Domain
	if(sceNetResolverStartNtoA(_rid, host, &in.sin_addr, 500000, 2) != 0)
	{
		// Attempt IP Conversion
		sceNetInetInetAton(host, &in.sin_addr);
	}
	
	// Create Socket
	int s = sceNetInetSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	// Valid Socket
	if(s > 0)
	{
		// Enabler
		int one = 1;
		
		// Enable Port Re-use
		sceNetInetSetsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
		sceNetInetSetsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
		
		// Set 3-Second Timeout
		uint32_t timeout = 3000000;
		sceNetInetSetsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
		sceNetInetSetsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
		
		// Connect to Destination
		if(sceNetInetConnect(s, (SceNetInetSockaddr *)&in, sizeof(in)) == 0/* || sceNetInetGetErrno() == 106*/)
		{
			printk("Connection Success\n");
			// Return Socket
			return s;
		}
		
		printk("Errno: %d\n", sceNetInetGetErrno());
		
		// Close Socket
		sceNetInetClose(s);
		
		logSockets();
	}

	// Error State
	return -1;
}
Exemplo n.º 18
0
/**
 * Init debugnet library 
 *
 * @par Example:
 * @code
 * #define LOGLEVEL 3  
 * int ret;
 * ret = debugNetInit("172.26.0.2", 18194, DEBUG);
 * @endcode
 *
 * @param serverIP - your pc/mac server ip
 * @param port - udp port server
 * @param level - DEBUG,ERROR,INFO or NONE 
 */
int debugNetInit(char *serverIp, int port, int level)
{
    int ret;
    SceNetInitParam initparam;
    SceNetCtlInfo info;
    struct SceNetSockaddrIn stSockAddr;
	debugNetSetLogLevel(level);
    if (debugnet_initialized) {
        return debugnet_initialized;
    }
    /*net initialazation code from xerpi at https://github.com/xerpi/FTPVita/blob/master/ftp.c*/
    /* Init Net */
    if (sceNetShowNetstat() == PSP2_NET_ERROR_ENOTINIT) {
        net_memory = malloc(NET_INIT_SIZE);

        initparam.memory = net_memory;
        initparam.size = NET_INIT_SIZE;
        initparam.flags = 0;

        ret = sceNetInit(&initparam);
        //printf("sceNetInit(): 0x%08X\n", ret);
    } else {
        //printf("Net is already initialized.\n");
    }

    /* Init NetCtl */
    ret = sceNetCtlInit();
    //printf("sceNetCtlInit(): 0x%08X\n", ret);
   

    /* Get IP address */
    ret = sceNetCtlInetGetInfo(PSP2_NETCTL_INFO_GET_IP_ADDRESS, &info);
    //printf("sceNetCtlInetGetInfo(): 0x%08X\n", ret);


    /* Save the IP of PSVita to a global variable */
    sceNetInetPton(PSP2_NET_AF_INET, info.ip_address, &vita_addr);
	
	/* Create datagram udp socket*/
    SocketFD = sceNetSocket("debugnet_socket",
        PSP2_NET_AF_INET , PSP2_NET_SOCK_DGRAM, PSP2_NET_IPPROTO_UDP);
   
    memset(&stSockAddr, 0, sizeof stSockAddr);
	
	/*Populate SceNetSockaddrIn structure values*/
    stSockAddr.sin_family = PSP2_NET_AF_INET;
    stSockAddr.sin_port = sceNetHtons(port);
    sceNetInetPton(PSP2_NET_AF_INET, serverIp, &stSockAddr.sin_addr);

	/*Connect socket to server*/
    sceNetConnect(SocketFD, (struct SceNetSockaddr *)&stSockAddr, sizeof stSockAddr);

	/*Show log on pc/mac side*/
	debugNetUDPPrintf("debugnet initialized\n");
	debugNetUDPPrintf("Copyright (C) 2010,2015 Antonio Jose Ramos Marquez aka bigboss @psxdev\n");
	debugNetUDPPrintf("This Program is subject to the terms of the Mozilla Public\n"
		"License, v. 2.0. If a copy of the MPL was not distributed with this\n"
		"file, You can obtain one at http://mozilla.org/MPL/2.0/.\n");
    debugNetUDPPrintf("ready to have a lot of fun...\n");

	/*library debugnet initialized*/
    debugnet_initialized = 1;

    return debugnet_initialized;
}
Exemplo n.º 19
0
/* upnpDiscover() :
 * return a chained list of all devices found or NULL if
 * no devices was found.
 * It is up to the caller to free the chained list
 * delay is in millisecond (poll) */
struct UPNPDev *
upnpDiscover(int delay, const char * multicastif,
             const char * minissdpdsock, int sameport,
             int ipv6, //unused in psp port
             int * error)
{
	struct UPNPDev * tmp;
	struct UPNPDev * devlist = 0;
	int opt = 1;
	static const char MSearchMsgFmt[] = 
	"M-SEARCH * HTTP/1.1\r\n"
	"HOST: %s:" XSTR(PORT) "\r\n"
	"ST: %s\r\n"
	"MAN: \"ssdp:discover\"\r\n"
	"MX: %u\r\n"
	"\r\n";
	static const char * const deviceList[] = {
#if 0
		"urn:schemas-upnp-org:device:InternetGatewayDevice:2",
		"urn:schemas-upnp-org:service:WANIPConnection:2",
#endif
		"urn:schemas-upnp-org:device:InternetGatewayDevice:1",
		"urn:schemas-upnp-org:service:WANIPConnection:1",
		"urn:schemas-upnp-org:service:WANPPPConnection:1",
		"upnp:rootdevice",
		0
	};
	int deviceIndex = 0;
	char bufr[1536];	/* reception and emission buffer */
	int sudp;
	int n;
	SceNetInetSockaddrIn sockudp_r;
	unsigned int mx;
	SceNetInetSockaddrIn sockudp_w;
	int linklocal = 1;

	if(error)
		*error = UPNPDISCOVER_UNKNOWN_ERROR;

	/* fallback to direct discovery */
	//sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0);
	sudp = sceNetInetSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if(sudp < 0)
	{
		if(error)
			*error = UPNPDISCOVER_SOCKET_ERROR;
		return NULL;
	}
	
	/* reception */
	memset(&sockudp_r, 0, sizeof(SceNetInetSockaddrIn));

	SceNetInetSockaddrIn * p = (SceNetInetSockaddrIn *)&sockudp_r;
	p->sin_family = AF_INET;
	if(sameport)
		p->sin_port = sceNetHtons(PORT);
	p->sin_addr = INADDR_ANY;


	// Enable Port Re-use
	sceNetInetSetsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
	sceNetInetSetsockopt(sudp, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
	
	/* Avant d'envoyer le paquet on bind pour recevoir la reponse */
    if (sceNetInetBind(sudp, (const struct SceNetInetSockaddr *)&sockudp_r,
	         sizeof(SceNetInetSockaddrIn)) != 0)
	{
		if(error)
			*error = UPNPDISCOVER_SOCKET_ERROR;
		sceNetInetClose(sudp);
		return NULL;
    }

	if(error)
		*error = UPNPDISCOVER_SUCCESS;
	/* Calculating maximum response time in seconds */
	mx = ((unsigned int)delay) / 1000u;
	/* receiving SSDP response packet */
	for(n = 0; deviceList[deviceIndex]; deviceIndex++)
	{
	if(n == 0)
	{
		/* sending the SSDP M-SEARCH packet */
		n = snprintf(bufr, sizeof(bufr),
		             MSearchMsgFmt,
		             UPNP_MCAST_ADDR,
		             deviceList[deviceIndex], mx);

		/* the following code is not using getaddrinfo */
		/* emission */
		memset(&sockudp_w, 0, sizeof(SceNetInetSockaddrIn));

		SceNetInetSockaddrIn * p = (SceNetInetSockaddrIn *)&sockudp_w;
		p->sin_family = AF_INET;
		p->sin_port = sceNetHtons(PORT);
		sceNetInetInetAton(UPNP_MCAST_ADDR, &p->sin_addr);
		
		n = sceNetInetSendto(sudp, bufr, n, 0,
		           (SceNetInetSockaddr *)&sockudp_w,
		           sizeof(SceNetInetSockaddrIn));
		if (n < 0) {
			if(error)
				*error = UPNPDISCOVER_SOCKET_ERROR;
			break;
		}
	}
	/* Waiting for SSDP REPLY packet to M-SEARCH */
	n = receivedata(sudp, bufr, sizeof(bufr), delay);
	if (n < 0) {
		/* error */
		if(error)
			*error = UPNPDISCOVER_SOCKET_ERROR;
		break;
	} else if (n == 0) {
		/* no data or Time Out */
		if (devlist) {
			/* no more device type to look for... */
			if(error)
				*error = UPNPDISCOVER_SUCCESS;
			break;
		}
		if(ipv6) {
			if(linklocal) {
				linklocal = 0;
				--deviceIndex;
			} else {
				linklocal = 1;
			}
		}
	} else {
		const char * descURL=NULL;
		int urlsize=0;
		const char * st=NULL;
		int stsize=0;
        /*printf("%d byte(s) :\n%s\n", n, bufr);*/ /* affichage du message */
		parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize);
		if(st&&descURL)
		{
			for(tmp=devlist; tmp; tmp = tmp->pNext) {
				if(memcmp(tmp->descURL, descURL, urlsize) == 0 &&
				   tmp->descURL[urlsize] == '\0' &&
				   memcmp(tmp->st, st, stsize) == 0 &&
				   tmp->st[stsize] == '\0')
					break;
			}
			/* at the exit of the loop above, tmp is null if
			 * no duplicate device was found */
			if(tmp)
				continue;
			tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize);
			if(!tmp) {
				/* memory allocation error */
				if(error)
					*error = UPNPDISCOVER_MEMORY_ERROR;
				break;
			}
			tmp->pNext = devlist;
			tmp->descURL = tmp->buffer;
			tmp->st = tmp->buffer + 1 + urlsize;
			memcpy(tmp->buffer, descURL, urlsize);
			tmp->buffer[urlsize] = '\0';
			memcpy(tmp->buffer + urlsize + 1, st, stsize);
			tmp->buffer[urlsize+1+stsize] = '\0';
			devlist = tmp;
		}
	}
	}
	sceNetInetClose(sudp);
	return devlist;
}
Exemplo n.º 20
0
static int network_interface_up(struct sockaddr_in *target, int index,
      const char *ip_address, unsigned udp_port, int *s)
{
   (void)index;

#if defined(VITA)
   if (sceNetShowNetstat() == PSP2_NET_ERROR_ENOTINIT)
   {
      SceNetInitParam initparam;
      net_memory = malloc(NET_INIT_SIZE);

      initparam.memory = net_memory;
      initparam.size = NET_INIT_SIZE;
      initparam.flags = 0;

      sceNetInit(&initparam);
   }
   *s                 = sceNetSocket("RA_netlogger", PSP2_NET_AF_INET, PSP2_NET_SOCK_DGRAM, 0);
   target->sin_family = PSP2_NET_AF_INET;
   target->sin_port   = sceNetHtons(udp_port);

   sceNetInetPton(PSP2_NET_AF_INET, ip_address, &target->sin_addr);
#else

#if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
   int ret = 0;

   int state, timeout_count = 10;
   ret = cellNetCtlInit();
   if (ret < 0)
      return -1;

   for (;;)
   {
      ret = cellNetCtlGetState(&state);
      if (ret < 0)
         return -1;

      if (state == CELL_NET_CTL_STATE_IPObtained)
         break;

      retro_sleep(500);
      timeout_count--;
      if (index && timeout_count < 0)
         return 0;
   }
#elif defined(GEKKO)
   char t[16];
   if (if_config(t, NULL, NULL, TRUE) < 0)
      ret = -1;
#endif
   if (ret < 0)
      return -1;

   *s                 = socket(AF_INET, SOCK_DGRAM, 0);

   target->sin_family = AF_INET;
   target->sin_port   = htons(udp_port);
#ifdef GEKKO
   target->sin_len    = 8;
#endif

   inet_pton(AF_INET, ip_address, &target->sin_addr);
#endif
   return 0;
}
Exemplo n.º 21
0
/**
 * Initialize Networking Components for Adhocctl Emulator
 * @param adhoc_id Game Product Code
 * @param server_ip Server IP
 * @return 0 on success or... -1
 */
int _initNetwork(const SceNetAdhocctlAdhocId * adhoc_id, const char * server_ip)
{
	// WLAN Switch Check
	if(sceWlanGetSwitchState() == 1)
	{
		// Initialize Access Point Control
		if(sceNetApctlInit(0x1800, 0x30) == 0)
		{
			// Attempt Counter
			int attemptmax = 10;
			
			// Attempt Number
			int attempt = 0;
			
			// Attempt Connection Setup
			for(; attempt < attemptmax; attempt++)
			{
				// Start Connection
				if(sceNetApctlConnect(_hotspot) == 0)
				{
					// Wait for Connection
					int statebefore = 0;
					int state = 0; while(state != 4)
					{
						// Query State
						int getstate = sceNetApctlGetState(&state);
						
						// Log State Change
						if(statebefore != state) printk("New Connection State: %d\n", state);					
						
						// Query Success
						if(getstate == 0 && state != 4)
						{
							// Wait for Retry
							sceKernelDelayThread(1000000);
						}
						
						// Query Error
						else break;
						
						// Save Before State
						statebefore = state;
					}
					
					// Connected
					if(state == 4)
					{
						// Create Friend Finder Socket
						int socket = sceNetInetSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
						
						// Created Socket
						if(socket > 0)
						{
							// Enable Port Re-use
							sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &_one, sizeof(_one));
							sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &_one, sizeof(_one));
							
							// Apply Receive Timeout Settings to Socket
							// uint32_t timeout = ADHOCCTL_RECV_TIMEOUT;
							// sceNetInetSetsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
							
							// Server IP
							uint32_t ip = 0;
							
							// Initialize DNS Resolver
							if(sceNetResolverInit() == 0)
							{
								// Create DNS Resolver
								unsigned char rbuf[512]; int rid = 0;
								if(sceNetResolverCreate(&rid, rbuf, sizeof(rbuf)) == 0)
								{
									// Resolve Domain
									if(sceNetResolverStartNtoA(rid, server_ip, &ip, 500000, 2) != 0)
									{
										// Attempt IP Conversion
										sceNetInetInetAton(server_ip, &ip);
									}
									
									// Delete DNS Resolver
									sceNetResolverDelete(rid);
								}
								
								// Shutdown DNS Resolver
								sceNetResolverTerm();
							}
							
							// Prepare Server Address
							SceNetInetSockaddrIn addr;
							addr.sin_len = sizeof(addr);
							addr.sin_family = AF_INET;
							addr.sin_addr = ip;
							addr.sin_port = sceNetHtons(ADHOCCTL_METAPORT);
							
							// Connect to Server
							if(sceNetInetConnect(socket, (SceNetInetSockaddr *)&addr, sizeof(addr)) == 0)
							{
								// Save Meta Socket
								_metasocket = socket;
								
								// Save Product Code
								_product_code = *adhoc_id;
								
								// Clear Event Handler
								memset(_event_handler, 0, sizeof(_event_handler[0]) * ADHOCCTL_MAX_HANDLER);
								memset(_event_args, 0, sizeof(_event_args[0]) * ADHOCCTL_MAX_HANDLER);
								
								// Clear Internal Control Status
								memset(&_parameter, 0, sizeof(_parameter));
								
								// Read PSP Player Name
								sceUtilityGetSystemParamString(PSP_SYSTEMPARAM_ID_STRING_NICKNAME, (char *)_parameter.nickname.data, ADHOCCTL_NICKNAME_LEN);
								
								// Read Adhoc Channel
								sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_ADHOC_CHANNEL, &_parameter.channel);
								
								// Fake Channel Number 1 on Automatic Channel
								if(_parameter.channel == 0) _parameter.channel = 1;
								
								// Read PSP MAC Address
								sceWlanGetEtherAddr((void *)&_parameter.bssid.mac_addr.data);
								
								// Prepare Login Packet
								SceNetAdhocctlLoginPacketC2S packet;
								
								// Set Packet Opcode
								packet.base.opcode = OPCODE_LOGIN;
								
								// Set MAC Address
								packet.mac = _parameter.bssid.mac_addr;
								
								// Set Nickname
								packet.name = _parameter.nickname;
								
								// Set Game Product ID
								memcpy(packet.game.data, adhoc_id->data, ADHOCCTL_ADHOCID_LEN);
								
								// Acquire Network Layer Lock
								_acquireNetworkLock();
								
								// Send Login Packet
								sceNetInetSend(_metasocket, &packet, sizeof(packet), INET_MSG_DONTWAIT);
								
								// Free Network Layer Lock
								_freeNetworkLock();
								
								// Load UPNP Library
								_upnp_uid = sceKernelLoadModule("ms0:/kd/pspnet_miniupnc.prx", 0, NULL);
								
								// Start UPNP Library
								int status = 0; sceKernelStartModule(_upnp_uid, 0, NULL, &status, NULL);
								
								// Return Success
								return 0;
							}
							
							// Delete Socket
							sceNetInetClose(socket);
						}
						
						// Close Hotspot Connection
						sceNetApctlDisconnect();
					}
				}
			}
			
			// Terminate Access Point Control
			sceNetApctlTerm();	
		}
	}
	
	// Generic Error
	return -1;
}
Exemplo n.º 22
0
/**
 * Adhoc Emulator PTP Active Socket Creator
 * @param saddr Local MAC (Unused)
 * @param sport Local Binding Port
 * @param daddr Target MAC
 * @param dport Target Port
 * @param bufsize Socket Buffer Size
 * @param rexmt_int Retransmit Interval (in Microseconds)
 * @param rexmt_cnt Retransmit Count
 * @param flag Bitflags (Unused)
 * @return Socket ID > 0 on success or... ADHOC_NOT_INITIALIZED, ADHOC_INVALID_ARG, ADHOC_INVALID_ADDR, ADHOC_INVALID_PORT
 */
int proNetAdhocPtpOpen(const SceNetEtherAddr * saddr, uint16_t sport, const SceNetEtherAddr * daddr, uint16_t dport, uint32_t bufsize, uint32_t rexmt_int, int rexmt_cnt, int flag)
{
	// Library is initialized
	if(_init)
	{
		// Valid Addresses
		if(saddr != NULL && _IsLocalMAC(saddr) && daddr != NULL && !_isBroadcastMAC(daddr))
		{
			// Random Port required
			if(sport == 0)
			{
				// Find unused Port
				while(sport == 0 || _IsPTPPortInUse(sport))
				{
					// Generate Port Number
					sport = (uint16_t)_getRandomNumber(65535);
				}
			}
			
			// Valid Ports
			if(!_IsPTPPortInUse(sport) && dport != 0)
			{
				// Valid Arguments
				if(bufsize > 0 && rexmt_int > 0 && rexmt_cnt > 0)
				{
					// Create Infrastructure Socket
					int socket = sceNetInetSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
					
					// Valid Socket produced
					if(socket > 0)
					{
						// Enable Port Re-use
						sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &_one, sizeof(_one));
						sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &_one, sizeof(_one));
						
						// Binding Information for local Port
						SceNetInetSockaddrIn addr;
						addr.sin_len = sizeof(addr);
						addr.sin_family = AF_INET;
						addr.sin_addr = INADDR_ANY;
						addr.sin_port = sceNetHtons(sport);
						
						// Bound Socket to local Port
						if(sceNetInetBind(socket, (SceNetInetSockaddr *)&addr, sizeof(addr)) == 0)
						{
							// Allocate Memory
							SceNetAdhocPtpStat * internal = (SceNetAdhocPtpStat *)malloc(sizeof(SceNetAdhocPtpStat));
							
							// Allocated Memory
							if(internal != NULL)
							{
								// Find Free Translator ID
								int i = 0; for(; i < 255; i++) if(_ptp[i] == NULL) break;
								
								// Found Free Translator ID
								if(i < 255)
								{
									// Clear Memory
									memset(internal, 0, sizeof(SceNetAdhocPtpStat));
									
									// Copy Infrastructure Socket ID
									internal->id = socket;
									
									// Copy Address Information
									internal->laddr = *saddr;
									internal->paddr = *daddr;
									internal->lport = sport;
									internal->pport = dport;
									
									// Set Buffer Size
									internal->rcv_sb_cc = bufsize;
									
									// Link PTP Socket
									_ptp[i] = internal;
									
									// Add Port Forward to Router
									sceNetPortOpen("TCP", sport);
									
									// Return PTP Socket Pointer
									return i + 1;
								}
								
								// Free Memory
								free(internal);
							}
						}
						
						// Close Socket
						sceNetInetClose(socket);
					}
				}
				
				// Invalid Arguments
				return ADHOC_INVALID_ARG;
			}
			
			// Invalid Ports
			return ADHOC_INVALID_PORT;
		}
		
		// Invalid Addresses
		return ADHOC_INVALID_ADDR;
	}
	
	// Library is uninitialized
	return ADHOC_NOT_INITIALIZED;
}
Exemplo n.º 23
0
/**
 * Adhoc Emulator PDP Socket Creator
 * @param saddr Local MAC (Unused)
 * @param sport Local Binding Port
 * @param bufsize Socket Buffer Size
 * @param flag Bitflags (Unused)
 * @return Socket ID > 0 on success or... ADHOC_NOT_INITIALIZED, ADHOC_INVALID_ARG, ADHOC_SOCKET_ID_NOT_AVAIL, ADHOC_INVALID_ADDR, ADHOC_PORT_NOT_AVAIL, ADHOC_INVALID_PORT, ADHOC_PORT_IN_USE, NET_NO_SPACE
 */
int proNetAdhocPdpCreate(const SceNetEtherAddr * saddr, uint16_t sport, int bufsize, int flag)
{
	// Library is initialized
	if(_init)
	{
		// Valid Arguments are supplied
		if(saddr != NULL && bufsize > 0)
		{
			// Valid MAC supplied
			if(_IsLocalMAC(saddr))
			{
				// Random Port required
				if(sport == 0)
				{
					// Find unused Port
					while(sport == 0 || _IsPDPPortInUse(sport))
					{
						// Generate Port Number
						sport = (uint16_t)_getRandomNumber(65535);
					}
				}
				
				// Unused Port supplied
				if(!_IsPDPPortInUse(sport))
				{
					// Create Internet UDP Socket
					int socket = sceNetInetSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
					
					// Valid Socket produced
					if(socket > 0)
					{
						// Enable Port Re-use
						sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &_one, sizeof(_one));
						sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &_one, sizeof(_one));
						
						// Binding Information for local Port
						SceNetInetSockaddrIn addr;
						addr.sin_len = sizeof(addr);
						addr.sin_family = AF_INET;
						addr.sin_addr = INADDR_ANY;
						addr.sin_port = sceNetHtons(sport);
						
						// Bound Socket to local Port
						if(sceNetInetBind(socket, (SceNetInetSockaddr *)&addr, sizeof(addr)) == 0)
						{
							// Allocate Memory for Internal Data
							SceNetAdhocPdpStat * internal = (SceNetAdhocPdpStat *)malloc(sizeof(SceNetAdhocPdpStat));
							
							// Allocated Memory
							if(internal != NULL)
							{
								// Clear Memory
								memset(internal, 0, sizeof(SceNetAdhocPdpStat));
								
								// Find Free Translator Index
								int i = 0; for(; i < 255; i++) if(_pdp[i] == NULL) break;
								
								// Found Free Translator Index
								if(i < 255)
								{
									// Fill in Data
									internal->id = socket;
									internal->laddr = *saddr;
									internal->lport = sport;
									internal->rcv_sb_cc = bufsize;
									
									// Link Socket to Translator ID
									_pdp[i] = internal;
									
									// Forward Port on Router
									sceNetPortOpen("UDP", sport);
									
									// Success
									return i + 1;
								}
								
								// Free Memory for Internal Data
								free(internal);
							}
						}
						
						// Close Socket
						sceNetInetClose(socket);
					}
					
					// Default to No-Space Error
					return NET_NO_SPACE;
				}
				
				// Port is in use by another PDP Socket
				return ADHOC_PORT_IN_USE;
			}
			
			// Invalid MAC supplied
			return ADHOC_INVALID_ADDR;
		}
		
		// Invalid Arguments were supplied
		return ADHOC_INVALID_ARG;
	}
	
	// Library is uninitialized
	return ADHOC_NOT_INITIALIZED;
}
Exemplo n.º 24
0
void *ps4link_requests_thread(void * args)
{
	int ret;
	struct sockaddr_in serveraddr;
	ps4link_requests_connected=0;
	/* Create server socket */
	configuration->ps4link_requests_sock = sceNetSocket("requests_server_sock",AF_INET,SOCK_STREAM,0);
	if(ps4LinkGetValue(REQUESTS_SOCK)>=0)
	{
		debugNetPrintf(DEBUG,"[PS4LINK] Created ps4link_requests_sock: %d\n", ps4LinkGetValue(REQUESTS_SOCK));
		
	}
  	
	/* Fill the server's address */
	serveraddr.sin_len = 16;
	serveraddr.sin_family = 2;
	serveraddr.sin_addr.s_addr = sceNetHtonl(INADDR_ANY);
	serveraddr.sin_port = sceNetHtons(ps4LinkGetValue(REQUESTS_PORT));
	memset(serveraddr.sin_zero, 0, sizeof(serveraddr.sin_zero));

	/* Bind the server's address to the socket */
	ret = sceNetBind(ps4LinkGetValue(REQUESTS_SOCK), (struct sockaddr *)&serveraddr, sizeof(serveraddr));
	if(ret<0)
	{
		debugNetPrintf(DEBUG,"[PS4LINK] sceNetBind error: 0x%08X\n", ret);
		sceNetSocketClose(ps4LinkGetValue(REQUESTS_SOCK));
		return NULL;
	}
	debugNetPrintf(DEBUG,"[PS4LINK] bind to ps4link_requests_sock done\n");
	
	/* Start listening */
	ret = sceNetListen(ps4LinkGetValue(REQUESTS_SOCK), 5);
	if(ret<0)
	{
		debugNetPrintf(DEBUG,"[PS4LINK] sceNetListen error: 0x%08X\n", ret);
		sceNetSocketClose(ps4LinkGetValue(REQUESTS_SOCK));
		return NULL;
	}
	
	
	debugNetPrintf(DEBUG,"[PS4LINK] Ready for connection %d\n", ps4LinkGetValue(FILEIO_ACTIVE));
   
   
	while(ps4LinkGetValue(FILEIO_ACTIVE))
	{
		debugNetPrintf(DEBUG,"[PS4LINK] Waiting for connection\n", ret);
		
		/* Accept clients */
		struct sockaddr_in clientaddr;
		int client_sock;
		unsigned int addrlen = sizeof(clientaddr);
		
		client_sock = sceNetAccept(ps4LinkGetValue(REQUESTS_SOCK), (struct sockaddr *)&clientaddr, &addrlen);
		if (client_sock < 0) {
			debugNetPrintf(DEBUG,"[PS4LINK] sceNetAccept error (0x%08X)\n", client_sock);
		    continue;
		}
		
		
		/* Get the client's IP address */
		remote_pc_addr = clientaddr.sin_addr.s_addr;
		char remote_ip[16];
		sceNetInetNtop(AF_INET,&clientaddr.sin_addr.s_addr,remote_ip,sizeof(remote_ip));
		debugNetPrintf(DEBUG,"[PS4LINK] Client connected from %s port: %i\n ",remote_ip, clientaddr.sin_port);			
		if (ps4LinkGetValue(FILEIO_SOCK) > 0) {
				debugNetPrintf(DEBUG,"[PS4LINK] Client reconnected\n");
				// sceNetSocketClose(ps4LinkGetValue(FILEIO_SOCK));
		}
		
		configuration->ps4link_fileio_sock = client_sock;
			
		ps4link_requests_connected=1;
		
		debugNetPrintf(DEBUG,"[PS4LINK] sock ps4link_fileio set %d connected %d\n",ps4LinkGetValue(FILEIO_SOCK),ps4link_requests_connected);
		
		
		
	}
	debugNetPrintf(DEBUG,"[PS4LINK] exit thread requests\n");
	if(ps4LinkGetValue(FILEIO_SOCK))
	{
		debugNetPrintf(DEBUG,"[PS4LINK] closing fileio_sock\n");
		sceNetSocketClose(ps4LinkGetValue(FILEIO_SOCK));
		configuration->ps4link_fileio_sock=-1;
	}
	if(ps4LinkGetValue(REQUESTS_SOCK))
	{
		debugNetPrintf(DEBUG,"[PS4LINK] closing server_request_sock\n");
		sceNetSocketClose(ps4LinkGetValue(REQUESTS_SOCK));
		configuration->ps4link_requests_sock=-1;
	}
	
	ps4link_requests_connected=0;
	
	
	//sceKernelExitDeleteThread(0);
	return NULL;
}
Exemplo n.º 25
0
int ps4LinkRead(int fd, void *data, size_t size)
{
	int readbytes;
	int nbytes;
	int i;
	ps4link_pkt_read_req *readcmd;
	ps4link_pkt_read_rly *readrly;


	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	readcmd = (ps4link_pkt_read_req *)&send_packet[0];
	readrly = (ps4link_pkt_read_rly *)&recv_packet[0];
	readbytes = 0;

	readcmd->cmd = sceNetHtonl(PS4LINK_READ_CMD);
	readcmd->len = sceNetHtons((unsigned short)sizeof(ps4link_pkt_read_req));
	readcmd->fd = sceNetHtonl(fd);

	readbytes = 0;

	/*if (size < 0) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_file: illegal req!! (whish to read < 0 bytes!)\n");
		return -1;
	}*/

	readcmd->nbytes = sceNetHtonl(size);

	i = ps4link_send(ps4LinkGetValue(FILEIO_SOCK), readcmd, sizeof(ps4link_pkt_read_req), MSG_DONTWAIT);
	if (i<0)
	{
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_file: ps4link_read_file: send failed (%d)\n", i);
		return -1;
	}

	if(!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), (char *)readrly, sizeof(ps4link_pkt_read_rly), PS4LINK_READ_RLY)) {
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_file: " "did not receive PS4LINK_READ_RLY\n");
		return -1;
	}

	nbytes = sceNetNtohl(readrly->nbytes);
	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_file: Reply said there's %d bytes to read " "(wanted %d)\n", nbytes, size);

	// Now read the actual file data divide in chunk
	int numread=nbytes/PACKET_MAXSIZE;
	int lastread=nbytes%PACKET_MAXSIZE;
	int j;
	for(j=0;j<numread;j++)
	{
		if(j<numread-1)
		{
			i = ps4link_recv_bytes(ps4LinkGetValue(FILEIO_SOCK), data+j*PACKET_MAXSIZE, PACKET_MAXSIZE);
		}
		else
		{
			i = ps4link_recv_bytes(ps4LinkGetValue(FILEIO_SOCK), data+j*PACKET_MAXSIZE, PACKET_MAXSIZE+lastread);
			
		}
		if (i < 0) {
	    	debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_file, data read error\n");
	    	return -1;
		}
		debugNetPrintf(DEBUG,"[PS4LINK] ps4link_read_file: chunk %d  readed %d\n", j,i);
		
		
	}
	
	
	return nbytes;
}
Exemplo n.º 26
0
int psp2link_commands_thread(SceSize args, void *argp)
{
	struct SceNetSockaddrIn serveraddr;
	struct SceNetSockaddrIn remote_addr;
	int ret;
	int len;
	unsigned int addrlen;
	unsigned int cmd;
	psp2link_pkt_hdr *header;
		
	debugNetPrintf(DEBUG,"[PSP2LINK] Command Thread Started.\n" );
	
	configuration->psp2link_commands_sock = sceNetSocket("commands_server_sock",SCE_NET_AF_INET, SCE_NET_SOCK_DGRAM, SCE_NET_IPPROTO_UDP);
	
	if (psp2LinkGetValue(COMMANDS_SOCK) >=0)
	{
		debugNetPrintf(DEBUG,"[PSP2LINK] Created psp2link_commands_sock: %d\n", psp2LinkGetValue(COMMANDS_SOCK));
	}
	else
	{
		debugNetPrintf(DEBUG,"[PSP2LINK] Error creating socket psp2link_commands_sock  0x%08X\n", psp2LinkGetValue(COMMANDS_SOCK));
		psp2LinkFinish();
		return -1;
	}
	/* Fill the server's address */
    memset(&serveraddr, 0, sizeof serveraddr);
	serveraddr.sin_family = SCE_NET_AF_INET;
	serveraddr.sin_addr.s_addr = sceNetHtonl(SCE_NET_INADDR_ANY);
	serveraddr.sin_port = sceNetHtons(psp2LinkGetValue(COMMANDS_PORT));	
	
	ret = sceNetBind(psp2LinkGetValue(COMMANDS_SOCK), (SceNetSockaddr *)&serveraddr, sizeof(serveraddr));
	
	
	
	
	if (ret < 0) 
	{
		debugNetPrintf(DEBUG,"[PSP2LINK] command listener sceNetBind error: 0x%08X\n", ret);
		sceNetSocketClose(psp2LinkGetValue(COMMANDS_SOCK));
		psp2LinkFinish();
		return -1;
	}
	// Do tha thing
	debugNetPrintf(DEBUG,"[PSP2LINK] Command listener waiting for commands...\n");
	
	while(psp2LinkGetValue(CMDSIO_ACTIVE)) {

		addrlen = sizeof(remote_addr);
		//wait for new command
		
		len = sceNetRecvfrom(psp2LinkGetValue(COMMANDS_SOCK), &recvbuf[0], BUF_SIZE, 0, (struct SceNetSockaddr *)&remote_addr,&addrlen);
		debugNetPrintf(DEBUG,"[PSP2LINK] commands listener received packet size (%d)\n", len);

		if (len < 0) {
			debugNetPrintf(DEBUG,"[PSP2LINK] commands listener recvfrom size error (%d)\n", len);
			continue;
		}
		if (len < sizeof(psp2link_pkt_hdr)) {
			debugNetPrintf(DEBUG,"[PSP2LINK] commands listener recvfrom header size error (%d)\n", len);
			continue;
		}

		header = (psp2link_pkt_hdr *)recvbuf;
		cmd = sceNetHtonl(header->cmd);
		
		switch (cmd) {

			case PSP2LINK_EXECELF_CMD:
				psp2LinkCmdExecElf((psp2link_pkt_exec_cmd *)recvbuf);
				break;
			case PSP2LINK_EXECSPRX_CMD:
				psp2LinkCmdExecSprx((psp2link_pkt_exec_cmd *)recvbuf);
				break;
			case PSP2LINK_EXIT_CMD:
				psp2LinkCmdExit((psp2link_pkt_exec_cmd *)recvbuf);
				break;
			default: 
				debugNetPrintf(DEBUG,"[PSP2LINK] Unknown command received\n");
				break;
		}
		debugNetPrintf(DEBUG,"[PSP2LINK] commands listener waiting for next command\n");
	}
	debugNetPrintf(DEBUG,"[PSP2LINK] exit commands listener thread\n");
	if(psp2LinkGetValue(COMMANDS_SOCK))
	{
		debugNetPrintf(DEBUG,"[PSP2LINK] closing server_commands_sock\n");
		sceNetSocketClose(psp2LinkGetValue(COMMANDS_SOCK));
		configuration->psp2link_commands_sock=-1;
	}
	
	sceKernelExitDeleteThread(0);
	return 0;
}
Exemplo n.º 27
0
int ps4LinkWrite(int fd, const void *data, size_t size)
{
	ps4link_pkt_write_req *writecmd;
	ps4link_pkt_file_rly *writerly;
	int hlen;
	int writtenbytes;
	int nbytes;
	int retval;

	if (ps4LinkGetValue(FILEIO_SOCK) < 0) {
		return -1;
	}

	debugNetPrintf(DEBUG,"[PS4LINK] file write req (fd: %d)\n", fd);

	writecmd = (ps4link_pkt_write_req *)&send_packet[0];
	writerly = (ps4link_pkt_file_rly *)&recv_packet[0];

	hlen = (unsigned short)sizeof(ps4link_pkt_write_req);
	writecmd->cmd = sceNetHtonl(PS4LINK_WRITE_CMD);
	writecmd->len = sceNetHtons(hlen);
	writecmd->fd = sceNetHtonl(fd);

	// Divide the write request
	writtenbytes = 0;
	while (writtenbytes < size) {

		if ((size - writtenbytes) > PS4LINK_MAX_WRITE_SEGMENT) {
			// Need to split in several read reqs
			nbytes = PS4LINK_MAX_READ_SEGMENT;
        }
		else 
		{
			nbytes = size - writtenbytes;
		}

		writecmd->nbytes = sceNetHtonl(nbytes);
#ifdef ZEROCOPY
		/* Send the packet header.  */
		if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), writecmd, hlen, MSG_DONTWAIT) < 0)
			return -1;
		/* Send the write() data.  */
		if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), &data[writtenbytes], nbytes, MSG_DONTWAIT) < 0)
			return -1;
#else
		// Copy data to the acutal packet
		memcpy(&send_packet[sizeof(ps4link_pkt_write_req)], &data[writtenbytes],nbytes);

		if (ps4link_send(ps4LinkGetValue(FILEIO_SOCK), writecmd, hlen + nbytes, MSG_DONTWAIT) < 0)
			return -1;
#endif


		// Get reply
		if(!ps4link_accept_pkt(ps4LinkGetValue(FILEIO_SOCK), (char *)writerly, sizeof(ps4link_pkt_file_rly), PS4LINK_WRITE_RLY)) {
			debugNetPrintf(DEBUG,"[PS4LINK] ps4link_write_file: " "did not receive PS4LINK_WRITE_RLY\n");
			return -1;
		}
		retval = sceNetNtohl(writerly->retval);

		debugNetPrintf(DEBUG,"[PS4LINK] wrote %d bytes (asked for %d)\n", retval, nbytes);

		if (retval < 0) {
			// Error
			debugNetPrintf(DEBUG,"[PS4LINK] ps4link_write_file: received error on write req (%d)\n",retval);
			return retval;
		}

		writtenbytes += retval;
		if (retval < nbytes) {
			// EOF?
			break;
		}
	}
	
	return writtenbytes;	

	
}
Exemplo n.º 28
0
/**
 * Adhoc Emulator PDP Send Call
 * @param id Socket File Descriptor
 * @param daddr Target MAC Address
 * @param dport Target Port
 * @param data Data Payload
 * @param len Payload Length
 * @param timeout Send Timeout
 * @param flag Nonblocking Flag
 * @return 0 on success or... ADHOC_INVALID_ARG, ADHOC_NOT_INITIALIZED, ADHOC_INVALID_SOCKET_ID, ADHOC_SOCKET_DELETED, ADHOC_INVALID_ADDR, ADHOC_INVALID_PORT, ADHOC_INVALID_DATALEN, ADHOC_SOCKET_ALERTED, ADHOC_TIMEOUT, ADHOC_THREAD_ABORTED, ADHOC_WOULD_BLOCK, NET_NO_SPACE, NET_INTERNAL
 */
int proNetAdhocPdpSend(int id, const SceNetEtherAddr * daddr, uint16_t dport, const void * data, int len, uint32_t timeout, int flag)
{
	// Library is initialized
	if(_init)
	{
		// Valid Port
		if(dport != 0)
		{
			// Valid Data Length
			if(len > 0)
			{
				// Valid Socket ID
				if(id > 0 && id <= 255 && _pdp[id - 1] != NULL)
				{
					// Cast Socket
					SceNetAdhocPdpStat * socket = _pdp[id - 1];
					
					// Valid Data Buffer
					if(data != NULL)
					{
						// Valid Destination Address
						if(daddr != NULL)
						{
							// Log Destination
							#ifdef DEBUG
							printk("Attempting PDP Send to %02X:%02X:%02X:%02X:%02X:%02X on Port %u\n", daddr->data[0], daddr->data[1], daddr->data[2], daddr->data[3], daddr->data[4], daddr->data[5], dport);
							#endif
							
							// Schedule Timeout Removal
							if(flag) timeout = 0;
							
							// Apply Send Timeout Settings to Socket
							sceNetInetSetsockopt(socket->id, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
							
							// Single Target
							if(!_isBroadcastMAC(daddr))
							{
								// Fill in Target Structure
								SceNetInetSockaddrIn target;
								target.sin_family = AF_INET;
								target.sin_port = sceNetHtons(dport);
								
								// Get Peer IP
								if(_resolveMAC((SceNetEtherAddr *)daddr, &target.sin_addr) == 0)
								{
									// Acquire Network Lock
									_acquireNetworkLock();
									
									// Send Data
									int sent = sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target));
									
									// Free Network Lock
									_freeNetworkLock();
									
									// Sent Data
									if(sent == len)
									{
										// Success
										return 0;
									}
									
									// Blocking Situation
									if(flag) return ADHOC_WOULD_BLOCK;
									
									// Timeout
									return ADHOC_TIMEOUT;
								}
							}
							
							// Broadcast Target
							else
							{
								// Acquire Network Lock
								_acquireNetworkLock();
								
								#ifdef BROADCAST_TO_LOCALHOST
								// Get Local IP Address
								union SceNetApctlInfo info; if(sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info) == 0)
								{
									// Fill in Target Structure
									SceNetInetSockaddrIn target;
									target.sin_family = AF_INET;
									sceNetInetInetAton(info.ip, &target.sin_addr);
									target.sin_port = sceNetHtons(dport);
									
									// Send Data
									sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target));
								}
								#endif
								
								// Acquire Peer Lock
								_acquirePeerLock();
								
								// Iterate Peers
								SceNetAdhocctlPeerInfo * peer = _getInternalPeerList();
								for(; peer != NULL; peer = peer->next)
								{
									// Fill in Target Structure
									SceNetInetSockaddrIn target;
									target.sin_family = AF_INET;
									target.sin_addr = peer->ip_addr;
									target.sin_port = sceNetHtons(dport);
									
									// Send Data
									sceNetInetSendto(socket->id, data, len, ((flag != 0) ? (INET_MSG_DONTWAIT) : (0)), (SceNetInetSockaddr *)&target, sizeof(target));
								}
								
								// Free Peer Lock
								_freePeerLock();
								
								// Free Network Lock
								_freeNetworkLock();
								
								// Broadcast never fails!
								return 0;
							}
						}
						
						// Invalid Destination Address
						return ADHOC_INVALID_ADDR;
					}
					
					// Invalid Argument
					return ADHOC_INVALID_ARG;
				}
				
				// Invalid Socket ID
				return ADHOC_INVALID_SOCKET_ID;
			}
			
			// Invalid Data Length
			return ADHOC_INVALID_DATALEN;
		}
		
		// Invalid Destination Port
		return ADHOC_INVALID_PORT;
	}
	
	// Library is uninitialized
	return ADHOC_NOT_INITIALIZED;
}
Exemplo n.º 29
0
static int server_thread(SceSize args, void *argp)
{
	int ret;
	UNUSED(ret);

	SceNetSockaddrIn serveraddr;

	DEBUG("Server thread started!\n");

	/* Create server socket */
	server_sockfd = sceNetSocket("FTPVita_server_sock",
		PSP2_NET_AF_INET,
		PSP2_NET_SOCK_STREAM,
		0);

	DEBUG("Server socket fd: %d\n", server_sockfd);

	/* Fill the server's address */
	serveraddr.sin_family = PSP2_NET_AF_INET;
	serveraddr.sin_addr.s_addr = sceNetHtonl(PSP2_NET_INADDR_ANY);
	serveraddr.sin_port = sceNetHtons(FTP_PORT);

	/* Bind the server's address to the socket */
	ret = sceNetBind(server_sockfd, (SceNetSockaddr *)&serveraddr, sizeof(serveraddr));
	DEBUG("sceNetBind(): 0x%08X\n", ret);

	/* Start listening */
	ret = sceNetListen(server_sockfd, 128);
	DEBUG("sceNetListen(): 0x%08X\n", ret);

	while (1) {

		/* Accept clients */
		SceNetSockaddrIn clientaddr;
		int client_sockfd;
		unsigned int addrlen = sizeof(clientaddr);

		DEBUG("Waiting for incoming connections...\n");

		client_sockfd = sceNetAccept(server_sockfd, (SceNetSockaddr *)&clientaddr, &addrlen);
		if (client_sockfd >= 0) {

			DEBUG("New connection, client fd: 0x%08X\n", client_sockfd);

			/* Get the client's IP address */
			char remote_ip[16];
			sceNetInetNtop(PSP2_NET_AF_INET,
				&clientaddr.sin_addr.s_addr,
				remote_ip,
				sizeof(remote_ip));

			INFO("Client %i connected, IP: %s port: %i\n",
				number_clients, remote_ip, clientaddr.sin_port);

			/* Create a new thread for the client */
			char client_thread_name[64];
			sprintf(client_thread_name, "FTPVita_client_%i_thread",
				number_clients);

			SceUID client_thid = sceKernelCreateThread(
				client_thread_name, client_thread,
				0x10000100, 0x10000, 0, 0, NULL);

			DEBUG("Client %i thread UID: 0x%08X\n", number_clients, client_thid);

			/* Allocate the ClientInfo struct for the new client */
			ClientInfo *client = malloc(sizeof(*client));
			client->num = number_clients;
			client->thid = client_thid;
			client->ctrl_sockfd = client_sockfd;
			client->data_con_type = FTP_DATA_CONNECTION_NONE;
			sprintf(client->cur_path, "%s%s", FTP_DEFAULT_PREFIX, FTP_DEFAULT_PATH);
			memcpy(&client->addr, &clientaddr, sizeof(client->addr));

			/* Add the new client to the client list */
			client_list_add(client);

			/* Start the client thread */
			sceKernelStartThread(client_thid, sizeof(*client), client);

			number_clients++;
		} else {
			/* if sceNetAccept returns < 0, it means that the listening
			 * socket has been closed, this means that we want to
			 * finish the server thread */
			DEBUG("Server socket closed, 0x%08X\n", client_sockfd);
			break;
		}
	}

	DEBUG("Server thread exiting!\n");

	sceKernelExitDeleteThread(0);
	return 0;
}