Advertisement
xerpi

Untitled

Feb 1st, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.35 KB | None | 0 0
  1. #include <pspkernel.h>
  2. #include <psputility.h>
  3. #include <pspdebug.h>
  4. #include <pspsdk.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <pspnet.h>
  9. #include <pspnet_inet.h>
  10. #include <pspnet_apctl.h>
  11. #include <pspnet_resolver.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include <sys/select.h>
  15. #include <errno.h>
  16.  
  17. #define printf pspDebugScreenPrintf
  18.  
  19. #define MODULE_NAME "NetSample"
  20. #define HELLO_MSG   "Hello there. Type away.\r\n"
  21.  
  22. PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
  23. PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  24.  
  25. int exit_callback(int arg1, int arg2, void *common);
  26. int CallbackThread(SceSize args, void *argp);
  27. int SetupCallbacks(void);
  28.  
  29. #define SERVER_PORT 4299
  30.  
  31. int make_socket(uint16_t port);
  32. void start_server(const char *szIpAddr);
  33. int connect_to_apctl(int config);
  34. void list_netconfigs();
  35.  
  36. void Handler(
  37.         int prev_state,
  38.         int new_state,
  39.         int event,
  40.         int error_code,
  41.         void *arg
  42.         )
  43. {
  44.     printf("Handler:\n\tprev_state: %i\n\tnew_state: %i\n\tevent: %i\n\terror_code: %i\n", prev_state, new_state, event, error_code);
  45. }
  46.  
  47.  
  48.  
  49. int main(int argc, char **argv)
  50. {
  51.     SetupCallbacks();
  52.     pspDebugScreenInit();
  53.  
  54.     if(sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON) < 0 ||
  55.        sceUtilityLoadNetModule(PSP_NET_MODULE_INET) < 0) {
  56.         printf("Error, could not load inet modules\n");
  57.         sceKernelSleepThread();
  58.     }
  59.    
  60. int err;
  61.     err = sceNetInit(128*1024, 42, 4*1024, 42, 4*1024);
  62.     printf("sceNetInit returns: %i\n", err);
  63.     err = sceNetInetInit();
  64.     printf("sceNetInetInit returns: %i\n", err);
  65.     err = sceNetApctlInit(0x8000, 48);
  66.     printf("sceNetApctlInit returns: %i\n", err);
  67.    
  68.     sceNetApctlAddHandler(Handler, NULL);
  69.    
  70.     list_netconfigs();
  71.    
  72.     while (connect_to_apctl(1) == 0) sceKernelDelayThread(1000*1000);
  73.    
  74.     union SceNetApctlInfo info_ip;
  75.    
  76.     if (sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info_ip) != 0)
  77.         strcpy(info_ip.ip, "unknown IP");
  78.        
  79.     union SceNetApctlInfo info_ssid;
  80.     if (sceNetApctlGetInfo(PSP_NET_APCTL_INFO_SSID, &info_ssid) != 0)
  81.         strcpy(info_ssid.ip, "unknown SSID");
  82.     printf("SSID: %s\n", info_ssid.ssid);
  83.    
  84.     start_server(info_ip.ip);
  85.  
  86.     sceKernelSleepThread();
  87.  
  88.     return 0;
  89. }
  90.  
  91. int make_socket(uint16_t port)
  92. {
  93.     int sock;
  94.     int ret;
  95.     struct sockaddr_in name;
  96.  
  97.     sock = socket(PF_INET, SOCK_STREAM, 0);
  98.     if (sock < 0) {
  99.         return -1;
  100.     }
  101.  
  102.     name.sin_family = AF_INET;
  103.     name.sin_port = htons(port);
  104.     name.sin_addr.s_addr = htonl(INADDR_ANY);
  105.     ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
  106.     if (ret < 0) {
  107.         return -1;
  108.     }
  109.  
  110.     return sock;
  111. }
  112.  
  113. /* Start a simple tcp echo server */
  114. void start_server(const char *szIpAddr)
  115. {
  116.     int ret;
  117.     int sock;
  118.     int new = -1;
  119.     struct sockaddr_in client;
  120.     size_t size;
  121.     int readbytes;
  122.     char data[1024];
  123.     fd_set set;
  124.     fd_set setsave;
  125.  
  126.     /* Create a socket for listening */
  127.     sock = make_socket(SERVER_PORT);
  128.     if (sock < 0) {
  129.         printf("Error creating server socket\n");
  130.         return;
  131.     }
  132.  
  133.     ret = listen(sock, 1);
  134.     if (ret < 0) {
  135.         printf("Error calling listen\n");
  136.         return;
  137.     }
  138.  
  139.     printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
  140.  
  141.     FD_ZERO(&set);
  142.     FD_SET(sock, &set);
  143.     setsave = set;
  144.  
  145.     while (1) {
  146.         int i;
  147.         set = setsave;
  148.         if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0) {
  149.             printf("select error\n");
  150.             return;
  151.         }
  152.  
  153.         for (i = 0; i < FD_SETSIZE; i++) {
  154.             if (FD_ISSET(i, &set)) {
  155.                 int val = i;
  156.  
  157.                 if (val == sock) {
  158.                     new = accept(sock, (struct sockaddr *) &client, &size);
  159.                     if(new < 0) {
  160.                         printf("Error in accept %s\n", strerror(errno));
  161.                         close(sock);
  162.                         return;
  163.                     }
  164.  
  165.                     printf("New connection %d from %s:%d\n", val,
  166.                             inet_ntoa(client.sin_addr),
  167.                             ntohs(client.sin_port));
  168.  
  169.                     write(new, HELLO_MSG, strlen(HELLO_MSG));
  170.  
  171.                     FD_SET(new, &setsave);
  172.                 } else {
  173.                     readbytes = read(val, data, sizeof(data));
  174.                     if(readbytes <= 0) {
  175.                         printf("Socket %d closed\n", val);
  176.                         FD_CLR(val, &setsave);
  177.                         close(val);
  178.                     } else {
  179.                         write(val, data, readbytes);
  180.                         printf("%.*s", readbytes, data);
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.     }
  186.  
  187.     close(sock);
  188. }
  189.  
  190. /* Connect to an access point */
  191. int connect_to_apctl(int config)
  192. {
  193.     int err;
  194.     int stateLast = -1;
  195.  
  196.     printf("Before connect\n");
  197.  
  198.     /* Connect using the first profile */
  199.     err = sceNetApctlConnect(config);
  200.     if (err != 0) {
  201.         printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
  202.         return 0;
  203.     }
  204.     printf("After connect\n");
  205.  
  206.     printf(MODULE_NAME ": Connecting...\n");
  207.     while (1) {
  208.         int state;
  209.         err = sceNetApctlGetState(&state);
  210.         if (err != 0) {
  211.             printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
  212.             break;
  213.         }
  214.         if (state > stateLast) {
  215.             printf("  connection state %d of 4\n", state);
  216.             stateLast = state;
  217.         }
  218.         if (state == 4)
  219.             break;  // connected with static IP
  220.  
  221.         // wait a little before polling again
  222.         sceKernelDelayThread(50*1000); // 50ms
  223.     }
  224.     printf(MODULE_NAME ": Connected!\n");
  225.  
  226.     if(err != 0) {
  227.         return 0;
  228.     }
  229.     return 1;
  230. }
  231.  
  232. void list_netconfigs()
  233. {
  234.     #define MAX_CONFIG 10
  235.     int i;
  236.     for (i = 1; i <= MAX_CONFIG; ++i) {
  237.         if (sceUtilityCheckNetParam(i) == 0) {
  238.             printf("->Configuration %i\n", i);
  239.             netData data;
  240.             sceUtilityGetNetParam(i, PSP_NETPARAM_NAME, &data);
  241.             printf("\tNAME: %s\n", data.asString);
  242.             sceUtilityGetNetParam(i, PSP_NETPARAM_SSID, &data);
  243.             printf("\tSSID: %s\n", data.asString);
  244.             sceUtilityGetNetParam(i, PSP_NETPARAM_SECURE, &data);
  245.             printf("\tSECURE: %s\n", data.asUint==2?"WEP":(data.asUint==3?"WPA":"NONE"));
  246.         }
  247.     }
  248. }
  249.  
  250. /* Exit callback */
  251. int exit_callback(int arg1, int arg2, void *common)
  252. {
  253.     sceKernelExitGame();
  254.     return 0;
  255. }
  256.  
  257. /* Callback thread */
  258. int CallbackThread(SceSize args, void *argp)
  259. {
  260.     int cbid;
  261.     cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  262.     sceKernelRegisterExitCallback(cbid);
  263.     sceKernelSleepThreadCB();
  264.     return 0;
  265. }
  266.  
  267. /* Sets up the callback thread and returns its thread id */
  268. int SetupCallbacks(void)
  269. {
  270.     int thid = 0;
  271.     thid = sceKernelCreateThread("update_thread", CallbackThread,
  272.              0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
  273.     if (thid >= 0) {
  274.         sceKernelStartThread(thid, 0, 0);
  275.     }
  276.     return thid;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement