int initSocket(lua_State *L) { if (!initializeSocket) { socketBuffer = memalign(0x1000, 0x100000); Result socketIsInitialized = socInit(socketBuffer, 0x100000); if (R_FAILED(socketIsInitialized)) { luaError(L, "Failed to initialize LuaSocket."); } else { initializeSocket = true; } } luaL_Reg reg[] = { {"udp", newUDP}, {"tcp", socketNewTCP}, {"shutdown", socketShutdown}, { 0, 0 }, }; luaL_newmetatable(L, "Socket"); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); luaL_newlib(L, reg); return 1; }
int main(int argc, const char* argv[]) { gfxInitDefault(); Result setCpuTimeRes = APT_SetAppCpuTimeLimit(30); if(R_FAILED(setCpuTimeRes)) { util_panic("Failed to set syscore CPU time limit: %08lX", setCpuTimeRes); return 1; } romfsInit(); cfguInit(); acInit(); ptmuInit(); httpcInit(0); amInit(); AM_InitializeExternalTitleDatabase(false); soc_buffer = memalign(0x1000, 0x100000); if(soc_buffer != NULL) { socInit(soc_buffer, 0x100000); } screen_init(); ui_init(); mainmenu_open(); while(aptMainLoop() && ui_update()); cleanup(); return 0; }
int main(int argc, char** argv){ struct termios cfg, old; FILE* root = NULL; int gdbPort = 0; if(argc != 3 && argc != 2){ fprintf(stderr,"usage: %s path_to_disk [gdbPort]\n", argv[0]); return -1; } //setup the terminal { int ret; ret = tcgetattr(0, &old); cfg = old; if(ret) perror("cannot get term attrs"); #ifndef _DARWIN_ cfg.c_iflag &=~ (INLCR | INPCK | ISTRIP | IUCLC | IXANY | IXOFF | IXON); cfg.c_oflag &=~ (OPOST | OLCUC | ONLCR | OCRNL | ONOCR | ONLRET); cfg.c_lflag &=~ (ECHO | ECHOE | ECHONL | ICANON | IEXTEN | XCASE); #else cfmakeraw(&cfg); #endif ret = tcsetattr(0, TCSANOW, &cfg); if(ret) perror("cannot set term attrs"); } root = fopen64(argv[1], "r+b"); if(!root){ fprintf(stderr,"Failed to open root device\n"); exit(-1); } if(argc >= 3) gdbPort = atoi(argv[2]); start_mongoose(); init_console(); print_console(); //socInit(&soc, socRamModeAlloc, NULL, readchar, writechar, rootOps, root); socInit(&soc, socRamModeAlloc, NULL, get_one_keystroke, write_char_to_console, rootOps, root); signal(SIGINT, &ctl_cHandler); socRun(&soc, gdbPort); fclose(root); tcsetattr(0, TCSANOW, &old); return 0; }
bool ctr::soc::init() { socBuffer = (u32*) memalign(0x1000, 0x100000); if(socBuffer == NULL) { ctr::err::set({ctr::err::SOURCE_ALLOCATE_BUFFER, ctr::err::MODULE_NN_SOCKET, ctr::err::LEVEL_PERMANENT, ctr::err::SUMMARY_OUT_OF_RESOURCE, ctr::err::DESCRIPTION_OUT_OF_MEMORY}); return false; } ctr::err::parse(ctr::err::SOURCE_SOC_INIT, (u32) socInit(socBuffer, 0x100000)); if(ctr::err::has()) { free(socBuffer); socBuffer = NULL; return false; } return true; }
int main(int argc, const char* argv[]) { gfxInitDefault(); gfxSet3D(false); if(argc > 0) { svchax_init(true); if(!__ctr_svchax || !__ctr_svchax_srv) { util_panic("Failed to acquire kernel access."); return 1; } } aptOpenSession(); Result setCpuTimeRes = APT_SetAppCpuTimeLimit(30); aptCloseSession(); if(R_FAILED(setCpuTimeRes)) { util_panic("Failed to set syscore CPU time limit: %08lX", setCpuTimeRes); return 1; } romfsInit(); cfguInit(); acInit(); ptmuInit(); httpcInit(0); amInit(); AM_InitializeExternalTitleDatabase(false); soc_buffer = memalign(0x1000, 0x100000); if(soc_buffer != NULL) { socInit(soc_buffer, 0x100000); } screen_init(); ui_init(); task_init(); mainmenu_open(); while(aptMainLoop() && ui_update()); cleanup(); return 0; }
int main(int argc, char** argv) { struct termios cfg, old; FILE* root = NULL; int gdbPort = 0; if(argc != 3 && argc != 2) { fprintf(stderr,"usage: %s path_to_disk [gdbPort]\n", argv[0]); return -1; } //setup the terminal { int ret; ret = tcgetattr(0, &old); cfg = old; if(ret) perror("cannot get term attrs"); cfmakeraw(&cfg); ret = tcsetattr(0, TCSANOW, &cfg); if(ret) perror("cannot set term attrs"); } root = fopen(argv[1], "r+b"); if(!root) { fprintf(stderr,"Failed to open root device\n"); exit(-1); } if(argc >= 3) gdbPort = atoi(argv[2]); socInit(&soc, socRamModeAlloc, NULL, readchar, writechar, rootOps, root); signal(SIGINT, &ctl_cHandler); socRun(&soc, gdbPort); fclose(root); tcsetattr(0, TCSANOW, &old); return 0; }
/** * network_init: * * Platform specific socket library initialization. * * Returns: true (1) if successful, otherwise false (0). **/ bool network_init(void) { #ifdef _WIN32 WSADATA wsaData; #endif static bool inited = false; if (inited) return true; #if defined(_WIN32) if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { network_deinit(); return false; } #elif defined(__CELLOS_LV2__) && !defined(__PSL1GHT__) int timeout_count = 10; cellSysmoduleLoadModule(CELL_SYSMODULE_NET); sys_net_initialize_network(); if (cellNetCtlInit() < 0) return false; for (;;) { int state; if (cellNetCtlGetState(&state) < 0) return false; if (state == CELL_NET_CTL_STATE_IPObtained) break; retro_sleep(500); timeout_count--; if (timeout_count < 0) return 0; } #elif defined(VITA) SceNetInitParam initparam; if (sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT) { _net_compat_net_memory = malloc(COMPAT_NET_INIT_SIZE); initparam.memory = _net_compat_net_memory; initparam.size = COMPAT_NET_INIT_SIZE; initparam.flags = 0; sceNetInit(&initparam); sceNetCtlInit(); } retro_epoll_fd = sceNetEpollCreate("epoll", 0); #elif defined(GEKKO) char t[16]; if (if_config(t, NULL, NULL, TRUE, 10) < 0) return false; #elif defined(WIIU) socket_lib_init(); #elif defined(_3DS) _net_compat_net_memory = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE); if (_net_compat_net_memory == NULL) return false; Result ret = socInit(_net_compat_net_memory, SOC_BUFFERSIZE);//WIFI init if (ret != 0) return false; #else signal(SIGPIPE, SIG_IGN); /* Do not like SIGPIPE killing our app. */ #endif inited = true; return true; }
int main(int argc, char **argv) { gfxInitDefault(); socInit((u32 *)memalign(0x1000, 0x100000), 0x100000); // Check wifi status u32 wifiStatus = 0; ACU_GetWifiStatus(&wifiStatus); if (!wifiStatus) { printf("\x1b[1;1HNo WiFi! Is your wireless slider on?"); } // Use printf on top screen consoleInit(GFX_TOP, NULL); // Stuff for network magic int recvlen; unsigned char buf[BUFSIZE]; unsigned char IDBuf[BUFSIZE]; // Try create socket int err = 0; if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { err = 1; } // Setup socket addresses out.sin_family = in.sin_family = AF_INET; out.sin_port = in.sin_port = htons(PORT); // Set port in.sin_addr.s_addr = INADDR_ANY; // Try to bind socket to port if (err == 0 && bind(sock, (struct sockaddr *)&in, sizeof(in)) < 0) err = 2; // If any errors if (err != 0) printf("\x1b[4;1HError opening connection: %i", err); // Set socket receive to non-blocking to be able to exit while listening for broadcast fcntl(sock, F_SETFL, O_NONBLOCK); bool connected = false; // Main loop while (aptMainLoop()) { if (err == 0) { // If not connected, listen for broadcast if (!connected) { printf("\x1b[1;1HListening for broadcast on port %d", PORT); // Listen for packets. Returns packet size recvlen = recvfrom(sock, buf, BUFSIZE, 0, (struct sockaddr *)&out, &addrlen); // Check if correct packet. recvlen < 0 -> Error if (recvlen > 0) { printf("\x1b[2;1Hr"); // Debug print. Writes r for any packet received buf[recvlen] = 0; // Don't remember what this is for. Oops // Check if message is connection port (Broadcast sends out port number as broadcast) int i = atoi(buf); if (i == PORT) { // If broadcast message, assign send address to received address in.sin_addr.s_addr = out.sin_addr.s_addr; // Prepare program; Setting flag, turning of backlight, etc connected = true; consoleClear(); printf("\x1b[1;1HConnected"); if (backlightOff == false) disableBacklight(); backlightOff = true; } } } } printf("\x1b[28;1HPlayer ID: %i", playerID); printf("\x1b[29;1HPress START and SELECT to exit"); // Scan input hidScanInput(); // Save keystate u32 kDown = hidKeysHeld(); if ((kDown & KEY_START) && (kDown & KEY_SELECT)) break; // break in order to return to hbmenu // If no errors and connected. Contruct input message if (err == 0 && connected) { // Get circle pad state circlePosition pos; hidCircleRead(&pos); // Construct message message.ID = playerID; message.pdx = pos.dx; message.pdy = pos.dy; message.btn = kDown; // Query for touchscreen information touchPosition touch; hidTouchRead(&touch); message.touch_px = touch.px; message.touch_py = touch.py; // Send packet to address broadcast came from sendto(sock, &message, sizeof(message), 0, (struct sockaddr *)&in, sizeof(in)); int recv = 0; int count = 0; do { count++; // Send packet to address broadcast came from sendto(sock, &message, sizeof(message), 0, (struct sockaddr *)&in, sizeof(in)); // Listen for packet to get player ID and check if server is alive //int recv = read(sock, IDBuf, sizeof(IDBuf)); recv = recvfrom(sock, IDBuf, BUFSIZE, 0, (struct sockaddr *)&out, &addrlen); } while (atoi(IDBuf) == PORT && count < 5); if (count < 5 && recv > 0) playerID = IDBuf[0] | IDBuf[1] << 1; else { connected = false; playerID = 0; consoleClear(); printf("\x1b[2;1HDisconnected"); if (backlightOff == true) { enableBacklight(); backlightOff = false; } } } u64 sleepDuration = 16000000ULL; svcSleepThread(sleepDuration); // Draw stuff gfxFlushBuffers(); gfxSwapBuffers(); gspWaitForVBlank(); } // On exit if (backlightOff == true) enableBacklight(); socExit(); gfxExit(); return 0; }
int main(int argc, char **argv) { (void) argc, (void) argv; Result res = MAKERESULT(RL_SUCCESS, RS_SUCCESS, 0, RD_SUCCESS); gfxInitDefault(); PrintConsole top; consoleInit(GFX_TOP, &top); util_debug_init(); consoleSelect(&top); if (R_FAILED(res = acInit())) { util_presult("acInit failed", res); goto ac_failure; } u32 wifi = 0; if (R_FAILED(res = ACU_GetWifiStatus(&wifi))) { util_presult("ACU_GetWifiStatus failed", res); fprintf(stderr, "Did you enable Wifi?\n"); goto wifi_check_failure; } if (!wifi) { fprintf(stderr, "Wifi disabled.\n"); goto wifi_check_failure; } if ((sock_ctx = memalign(SOCU_BUFSZ, SOCU_ALIGN)) == NULL) { util_perror("Allocating SOC buffer"); res = MAKERESULT( RL_PERMANENT, RS_OUTOFRESOURCE, RM_SOC, RD_OUT_OF_MEMORY); goto soc_alloc_failure; } if (R_FAILED(res = socInit(sock_ctx, SOCU_BUFSZ))) { util_presult("socInit failed", res); goto soc_failure; } if (R_FAILED(res = hidInit())) { util_presult("hidInit failed", res); goto hid_failure; } if (R_FAILED(res = HIDUSER_EnableAccelerometer())) { util_presult("Failed to enable accelerometer", res); goto accel_failure; } if (R_FAILED(res = ctrollerInit())) { fprintf(stderr, "Do you have a valid IP in\n '" CFG_FILE "'?"); goto failure; } bool isHomebrew = envIsHomebrew(); printf("Press %s to exit.\n", isHomebrew ? EXIT_DESC : "HOME"); fflush(stdout); while (aptMainLoop()) { if (isHomebrew) { if (hidKeysHeld() == EXIT_KEYS) { res = RL_SUCCESS; break; } } if (ctrollerSendHIDInfo()) { util_perror("Sending HID info"); fflush(stderr); for (int i = 3; i > 0; i--) { util_debug_printf("\rRetrying in %ds... ", i); svcSleepThread(1000000000L); } util_debug_printf("\rRetrying now.\x1b[K\n"); } gspWaitForVBlank(); gfxFlushBuffers(); gfxSwapBuffers(); } puts("Exiting..."); failure: HIDUSER_DisableAccelerometer(); accel_failure: hidExit(); hid_failure: socExit(); soc_failure: free(sock_ctx); soc_alloc_failure: wifi_check_failure: acExit(); ac_failure: if (R_FAILED(res)) { util_hang(res); } gfxExit(); return res; }
int main(int argc, const char* argv[]) { /* Sadly svchax crashes too much, so only allow install mode when running as a CIA // Trigger svchax so we can install CIAs if(argc > 0) { svchax_init(true); if(!__ctr_svchax || !__ctr_svchax_srv) { bSvcHaxAvailable = false; //printf("Failed to acquire kernel access. Install mode disabled.\n"); } } */ // argc is 0 when running as a CIA, and 1 when running as a 3dsx if (argc > 0) { bSvcHaxAvailable = false; } u32 *soc_sharedmem, soc_sharedmem_size = 0x100000; gfxInitDefault(); consoleInit(GFX_TOP, NULL); httpcInit(0); soc_sharedmem = (u32 *)memalign(0x1000, soc_sharedmem_size); socInit(soc_sharedmem, soc_sharedmem_size); sslcInit(0); hidInit(); acInit(); cfguInit(); if (bSvcHaxAvailable) { amInit(); AM_InitializeExternalTitleDatabase(false); } init_menu(GFX_TOP); // Make sure all CIAngel directories exists on the SD card mkpath("/CIAngel", 0777); mkpath("/CIAngel/tmp/", 0777); loadConfig(); // Set up the reading of json check_JSON(); load_JSON_data(); menu_main(); if (bSvcHaxAvailable) { amExit(); } cfguExit(); acExit(); gfxExit(); hidExit(); httpcExit(); socExit(); sslcExit(); }
//--------------------------------------------------------------------------------- int main(int argc, char **argv) { //--------------------------------------------------------------------------------- int ret; u32 clientlen; struct sockaddr_in client; struct sockaddr_in server; char temp[1026]; static int hits=0; gfxInitDefault(); // register gfxExit to be run when app quits // this can help simplify error handling atexit(gfxExit); consoleInit(GFX_TOP, NULL); printf ("\nlibctru sockets demo\n"); // allocate buffer for SOC service SOC_buffer = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE); if(SOC_buffer == NULL) { failExit("memalign: failed to allocate\n"); } // Now intialise soc:u service if ((ret = socInit(SOC_buffer, SOC_BUFFERSIZE)) != 0) { failExit("socInit: 0x%08X\n", (unsigned int)ret); } // register socShutdown to run at exit // atexit functions execute in reverse order so this runs before gfxExit atexit(socShutdown); // libctru provides BSD sockets so most code from here is standard clientlen = sizeof(client); sock = socket (AF_INET, SOCK_STREAM, IPPROTO_IP); if (sock < 0) { failExit("socket: %d %s\n", errno, strerror(errno)); } memset (&server, 0, sizeof (server)); memset (&client, 0, sizeof (client)); server.sin_family = AF_INET; server.sin_port = htons (80); server.sin_addr.s_addr = gethostid(); printf("Point your browser to http://%s/\n",inet_ntoa(server.sin_addr)); if ( (ret = bind (sock, (struct sockaddr *) &server, sizeof (server))) ) { close(sock); failExit("bind: %d %s\n", errno, strerror(errno)); } // Set socket non blocking so we can still read input to exit fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); if ( (ret = listen( sock, 5)) ) { failExit("listen: %d %s\n", errno, strerror(errno)); } while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); csock = accept (sock, (struct sockaddr *) &client, &clientlen); if (csock<0) { if(errno != EAGAIN) { failExit("accept: %d %s\n", errno, strerror(errno)); } } else { // set client socket to blocking to simplify sending data back fcntl(csock, F_SETFL, fcntl(csock, F_GETFL, 0) & ~O_NONBLOCK); printf("Connecting port %d from %s\n", client.sin_port, inet_ntoa(client.sin_addr)); memset (temp, 0, 1026); ret = recv (csock, temp, 1024, 0); if ( !strncmp( temp, http_get_index, strlen(http_get_index) ) ) { hits++; send(csock, http_200, strlen(http_200),0); send(csock, http_html_hdr, strlen(http_html_hdr),0); sprintf(temp, indexdata, hits); send(csock, temp, strlen(temp),0); } close (csock); csock = -1; } u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; } close(sock); return 0; }
int main(){ SD sd; init(); sei(); if(!sdInit(&sd)) err_str("sd init failed"); printf("SD Init Successfully\n"); #if SD_CHECK == 1 #define E(x) do{printf(x); while(1);}while(0) UInt32 p, numSec; Boolean ret; UInt16 i; UInt8 buf[512],data; printf("Performing some basic tests\n"); numSec = sdGetNumSec(&sd); printf(" - card is %ld sectors (%ld MB)\n", numSec, numSec >> 11UL); ret = sdSecRead(&sd, 0, buf); if(!ret) E("card read fails\n"); /*for(i = 0; i < 512; i++){ if(i & 0x0F) printf(" "); else printf("\n%04X ", i); } printf("\n"); //if(numSec > 32UL * 1024UL) numSec = 512; //Check full card for(p = 0; p < numSec; p++){ ret = sdSecRead(&sd, p, buf); if(!ret) E("card read fails\n"); printf("\r reading %ld/%ld", p, numSec); for(i = 0; i < 512; i += 16) ramWrite((p << 9) + i, buf + i, 16); } printf("\n");*/ for(p = 1; p < numSec; p+=4096){ printf("Sector:%lu\n",p); ret = sdSecRead(&sd, p, buf); if(!ret) E("card read fails\n"); for(i = 0;i < 512;i++){ *(buf+i) = 0x11; } ((uint32_t *)buf)[0] = p;//~*(buf+i); ret = sdSecWrite(&sd, p, buf); if(!ret) E("card write fails\n"); /*for(i = 0;i < 512;i+=16){ ramRead(i,&data,1); if(data != buf[i]){ printf("mismatch on %llu,data in memory is 0x%x,data in SD card is 0x%x\n",(uint64_t)p*512+i,data,buf[i]); while(1); } }*/ } printf("Finished!\n"); while(1); #endif socInit(&soc, socRamModeCallout, coRamAccess, readchar, writechar, rootOps, &sd); if(!(PIND & (1<<5))){ //hack for faster boot in case we know all variables & button is pressed #ifndef FAST_BOOT2 printf("Faster boot\n"); UInt32 i, s = 786464UL; UInt32 d = 0xA0E00000; UInt16 j; UInt8* b = (UInt8*)soc.blkDevBuf; for(i = 0; i < 4096; i++){ sdSecRead(&sd, s++, b); for(j = 0; j < 512; j += 32, d+= 32){ ramWrite(d, b + j, 32); } } soc.cpu.regs[15] = 0xA0E00000UL+512UL; printf("Faster boot start\n"); #else uint64_t i; /*struct ArmCpu cpu; cpu.extra_regs = soc.cpu.extra_regs;//backup pointer cpu.regs = soc.cpu.regs; cpu.coproc = soc.cpu.coproc; cpu.userdata = soc.cpu.userdata;*/ printf("Faster boot 2\n"); UInt8* buf = (UInt8*)soc.blkDevBuf; UInt32 s = (uint64_t)(0x18204000)/512;//1 sector 512 bytes sdSecRead(&sd, s++, buf); if(*buf == FAST_BOOT2_MAGIC_NUMBER){ //Magic Number for(i = 0; i < RAM_SIZE/512; i++){ sdSecRead(&sd, s++, buf); /*for(j = 0; j < 512; j += 32){ ramWrite(i<<9 + j,buf + j, 32); }*/ ramWrite(i<<9,buf,512); } sdSecRead(&sd, s, buf);//registers for(i = 0;i<16;i++){ soc.cpu.regs[i] = buf[i]; } /*soc.cpu = *(struct ArmCpu *)(buf+16);//Get CPU's information soc.cpu.extra_regs = cpu.extra_regs; soc.cpu.regs = cpu.regs; soc.cpu.coproc = cpu.coproc; soc.cpu.userdata = cpu.userdata; */ printf("Faster boot start\n"); }else{ printf("Magic number not match,try to boot in a normal way.\n"); } #endif } #ifdef GDB_SUPPORT socRun(&soc,0); #else socRun(&soc); #endif while(1); return 0; }