This repository has been archived by the owner on Apr 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_setup.c
134 lines (114 loc) · 2.5 KB
/
common_setup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <libtransistor/nx.h>
#include <libtransistor/ipc.h>
#include <libtransistor/loader_config.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "defs.h"
#include "memory.h"
#include "http.h"
#include "server.h"
#include "nro.h"
#include "common_setup.h"
FILE custom_stdout;
int std_sck;
struct sockaddr_in stdout_server_addr =
{
.sin_family = AF_INET,
.sin_port = htons(STDOUT_PORT),
};
int common_init(int argc, char **argv) {
const char *hostname;
int ret = 0;
if(argc == 0)
hostname = "a.b"; // PegaSwitch will respond to any DNS request
else
hostname = argv[0]; // use provided hostname
ipc_debug_level = IPC_DEBUG_LEVEL_NONE;
if(sm_init() != RESULT_OK)
return 1;
if(bsd_init() != RESULT_OK)
{
sm_finalize();
return 1;
}
// init HTTP (resolve hostname)
if(http_init(hostname))
{
bsd_close(std_sck);
bsd_finalize();
sm_finalize();
return 1;
}
// get stdout IP
http_paste_ip((uint32_t*)&stdout_server_addr.sin_addr.s_addr);
// create stdout socket, optional
std_sck = bsd_socket(2, 1, 6); // AF_INET, SOCK_STREAM, PROTO_TCP
if(std_sck >= 0)
{
// connect to stdout server, optional
if(bsd_connect(std_sck, (struct sockaddr*) &stdout_server_addr, sizeof(stdout_server_addr)) < 0)
{
bsd_close(std_sck);
std_sck = -1; // invalidate
} else {
// redirect stdout and stderr
int fd = socket_from_bsd(std_sck);
if(fd < 0) {
printf("error creating fd\n");
} else {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
dbg_set_file(fd_file_get(fd));
fd_close(fd);
}
}
}
return ret;
}
result_t common_begin_server() {
int ret = 0;
void *ptr;
// initialize RO
ret = ro_init();
if(ret)
{
printf("- ldr:ro initialization error 0x%06X\n", ret);
return ret;
}
ret = bpc_init();
if(ret)
{
printf("- bpc initialization error 0x%06x\n", ret);
return ret;
}
ret = nifm_init();
if(ret)
{
printf("- nifm initialization error 0x%06x\n", ret);
return ret;
}
// release sm; it's not needed anymore
sm_finalize();
/*
// start autorun NRO - if found
ret = http_get_file("autorun.nro", heap_base, heap_size);
if(ret > 0)
{
uint64_t r;
printf("- starting autorun\n");
nro_arg_name("autorun");
nro_add_arg(http_hostname);
r = nro_execute(heap_base, ret);
printf("- NRO returned 0x%016lX\n", r);
// do not exit for GUIs
if(!nro_loaded_count || nro_unload_fail)
return 0;
}*/
// start 'push' server
if(!server_init())
server_loop();
return 0;
}