const char *inet_ntop_compat(int af, const void *src, char *dst, socklen_t cnt) { #if defined(VITA) || defined(__ORBIS__) return sceNetInetNtop(af,src,dst,cnt); #elif defined(WIIU) return inet_ntop(af, src, dst, cnt); #elif defined(_XBOX) return isockaddr_ntop(af, src, dst, cnt); #elif defined(_WIN32) if (af == AF_INET) { struct sockaddr_in in; memset(&in, 0, sizeof(in)); in.sin_family = AF_INET; memcpy(&in.sin_addr, src, sizeof(struct in_addr)); getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); return dst; } #if defined(AF_INET6) && !defined(HAVE_SOCKET_LEGACY) else if (af == AF_INET6) { struct sockaddr_in6 in; memset(&in, 0, sizeof(in)); in.sin6_family = AF_INET6; memcpy(&in.sin6_addr, src, sizeof(struct in_addr6_compat)); getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); return dst; } #endif else return NULL; #else return inet_ntop(af, src, dst, cnt); #endif }
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; }
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; }