tcp listener update
This commit is contained in:
parent
a9fdf86f16
commit
eb4320d16f
3
.idea/dictionaries/User.xml
Normal file
3
.idea/dictionaries/User.xml
Normal file
@ -0,0 +1,3 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="User" />
|
||||
</component>
|
||||
@ -2,5 +2,12 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="CodeBlock2Expr" enabled="true" level="TEXT ATTRIBUTES" enabled_by_default="true" editorAttributes="CONSIDERATION_ATTRIBUTES" />
|
||||
<inspection_tool class="GrazieInspection" enabled="false" level="GRAMMAR_ERROR" enabled_by_default="false" />
|
||||
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
|
||||
<option name="processCode" value="true" />
|
||||
<option name="processLiterals" value="true" />
|
||||
<option name="processComments" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@ -5,6 +5,11 @@
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
|
||||
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/port-tunnel.iml" filepath="$PROJECT_DIR$/.idea/port-tunnel.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
2
.idea/port-tunnel.iml
Normal file
2
.idea/port-tunnel.iml
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="External" external.linked.project.id="port-tunnel" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="Makefile" type="CPP_MODULE" version="4" />
|
||||
@ -43,7 +43,8 @@ void ConnectorTCP_construct(ConnectorTCP* ptr, knIPV4Endpoint listener_end, knIP
|
||||
if(out_mode == OutputMode_Bind){
|
||||
tryLast(knSocketTCP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketTCP_bindAndListen(ptr->main_sock, connector_end), _m7u7, ;);
|
||||
tryLast(knSocketTCP_bind(ptr->main_sock, connector_end), _m7u7, ;);
|
||||
tryLast(knSocketTCP_listen(ptr->main_sock), _m7u77, ;);
|
||||
}
|
||||
else if(out_mode == OutputMode_Send){
|
||||
tryLast(knSocketTCP_open(false), m_s, ;);
|
||||
|
||||
@ -4,7 +4,8 @@ void ListenerTCP_construct(ListenerTCP* ptr, knIPV4Endpoint listener_end){
|
||||
__PipeStorage_construct(&ptr->pipes);
|
||||
tryLast(knSocketTCP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketTCP_bindAndListen(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
tryLast(knSocketTCP_bind(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
tryLast(knSocketTCP_listen(ptr->main_sock), _m7u77, ;);
|
||||
ptr->connector_end = knIPV4Endpoint_INVALID;
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,76 @@ int errs(char* err_msg){
|
||||
return -1;
|
||||
}
|
||||
|
||||
const knIPV4Endpoint listener_end = knIPV4Endpoint_create(knIPV4Address_LOOPBACK, 25565);
|
||||
|
||||
Maybe listener() {
|
||||
try(knSocketTCP_open(true), m_sock, ;);
|
||||
kprintf("opened socket\n");
|
||||
knSocketTCP* s = m_sock.value.VoidPtr;
|
||||
try(knSocketTCP_bind(s, listener_end), _m566, knSocketTCP_close(s));
|
||||
try(knSocketTCP_listen(s), _m568, knSocketTCP_close(s));
|
||||
kprintf("listening...\n");
|
||||
try(knSocketTCP_accept(s), m_cs, knSocketTCP_close(s))
|
||||
kprintf("connection has been accepted\n");
|
||||
knSocketTCP* connection = m_cs.value.VoidPtr;
|
||||
InternalPacketMessage msg;
|
||||
try(knSocketTCP_receiveN(connection, &msg, sizeof(InternalPacketMessage), sizeof(InternalPacketMessage)), _m8823, knSocketTCP_close(s))
|
||||
if(msg != InternalPacketMessage_ConnectorHandshake)
|
||||
safethrow("wrong message", ;);
|
||||
kprintf("received connector handshake\n");
|
||||
msg = InternalPacketMessage_ListenerHandshake;
|
||||
try(knSocketTCP_send(connection, &msg, sizeof(msg)), _muu632, knSocketTCP_close(s));
|
||||
kprintf("sent listener handshake\n");
|
||||
knIPV4Endpoint connector_end = connection->remoteEndpoint;
|
||||
try(knSocketTCP_shutdown(connection, knShutdownType_Both), _m7823, knSocketTCP_close(s));
|
||||
try(knSocketTCP_close(connection), _m71823, knSocketTCP_close(s));
|
||||
try(knSocketTCP_close(s), _m718823, ;);
|
||||
|
||||
try(knSocketTCP_open(false), m_sockc, ;);
|
||||
kprintf("opened connecting socket\n");
|
||||
knSocketTCP* c = m_sockc.value.VoidPtr;
|
||||
|
||||
kprintf("connecting to %s\n", knIPV4Endpoint_toString(connection->remoteEndpoint));
|
||||
sleepMsec(1000);
|
||||
try(knSocketTCP_connect(c, connector_end), _m866, knSocketTCP_close(c));
|
||||
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
Maybe connector() {
|
||||
try(knSocketTCP_open(true), m_sock, ;);
|
||||
kprintf("opened socket\n");
|
||||
knSocketTCP* s = m_sock.value.VoidPtr;
|
||||
sleepMsec(1000);
|
||||
try(knSocketTCP_connect(s, listener_end), _m866, knSocketTCP_close(s));
|
||||
kprintf("connected to listener\n");
|
||||
InternalPacketMessage msg = InternalPacketMessage_ConnectorHandshake;
|
||||
try(knSocketTCP_send(s, &msg, sizeof(msg)), _muu632, ;);
|
||||
kprintf("sent connector handshake\n");
|
||||
try(knSocketTCP_receiveN(s, &msg, sizeof(InternalPacketMessage), sizeof(InternalPacketMessage)), _m8823, ;)
|
||||
if(msg != InternalPacketMessage_ListenerHandshake)
|
||||
safethrow("wrong message", ;);
|
||||
kprintf("received listener handshake\n");
|
||||
|
||||
try(knSocketTCP_open(true), m_l, ;);
|
||||
kprintf("opened listening socket\n");
|
||||
knSocketTCP* l = m_l.value.VoidPtr;
|
||||
|
||||
knIPV4Endpoint public_end = knIPV4Endpoint_INVALID;
|
||||
//TODO: get endpoint from server because s->localEndpoint.port may not match the port in inter network
|
||||
|
||||
try(knSocketTCP_bind(l, public_end), _m173, {knSocketTCP_close(l); knSocketTCP_close(s);});
|
||||
kprintf("bound to %s\n", knIPV4Endpoint_toString(public_end));
|
||||
try(knSocketTCP_listen(l), _m18263, {knSocketTCP_close(l); knSocketTCP_close(s);});
|
||||
kprintf("listening...\n");
|
||||
|
||||
try(knSocketTCP_accept(l), m_cs, knSocketTCP_close(l))
|
||||
knSocketTCP* connection = m_cs.value.VoidPtr;
|
||||
kprintf("accepted incoming connection\n");
|
||||
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
#define argIs(STR) cptr_equals(arg, STR)
|
||||
|
||||
#define argNext() argv[++argi < argc ? argi : errs(cptr_concat("option '",arg,"' must have a parameter"))]
|
||||
@ -46,6 +116,15 @@ int main(int argc, const char* const* argv){
|
||||
|
||||
tryLast(kn_tryInit(), _m11257, ;)
|
||||
|
||||
if(argc == 1 || cptr_equals(argv[1], "l")){
|
||||
tryLast(listener(), _m6, ;);
|
||||
}
|
||||
else if(cptr_equals(argv[1], "c")){
|
||||
tryLast(connector(), _m12, ;);
|
||||
}
|
||||
else throw(ERR_KEYNOTFOUND);
|
||||
return 0;
|
||||
|
||||
TunnelProtocol tunnel_protocol = TunnelProtocol_None;
|
||||
InputMode input_mode = InputMode_None;
|
||||
OutputMode output_mode = OutputMode_None;
|
||||
@ -167,13 +246,13 @@ int main(int argc, const char* const* argv){
|
||||
logDebug(logCtx, "output_mode: %i", output_mode);
|
||||
char* temps;
|
||||
if(!knIPV4Endpoint_isINVALID(listener_end)){
|
||||
temps = knIPV4Endpoint_toString(&listener_end);
|
||||
temps = knIPV4Endpoint_toString(listener_end);
|
||||
logDebug(logCtx, "listener_end: %s", temps);
|
||||
free(temps);
|
||||
}
|
||||
else logDebug(logCtx, "listener_end: INVALID");
|
||||
if(!knIPV4Endpoint_isINVALID(connector_end)){
|
||||
temps = knIPV4Endpoint_toString(&connector_end);
|
||||
temps = knIPV4Endpoint_toString(connector_end);
|
||||
logDebug(logCtx, "connector_end: %s", temps);
|
||||
free(temps);
|
||||
}
|
||||
|
||||
@ -185,14 +185,14 @@ void ConnectorTCP_start(ConnectorTCP* ptr);
|
||||
////////////////////////////////////////
|
||||
|
||||
typedef union MagicUnion {
|
||||
char array[8];
|
||||
u8 array[8];
|
||||
u64 U64;
|
||||
} MagicUnion;
|
||||
|
||||
typedef enum InternalPacketMessage {
|
||||
InternalPacketMessage_ConnectorHandshake,
|
||||
InternalPacketMessage_ListenerHandshake,
|
||||
} __attribute__((__aligned__(8))) InternalPacketMessage;
|
||||
} __attribute__((__aligned__(4))) InternalPacketMessage;
|
||||
|
||||
/// connector <-> listener
|
||||
typedef struct InternalPacket {
|
||||
@ -201,7 +201,7 @@ typedef struct InternalPacket {
|
||||
/* 8 bytes */
|
||||
union {
|
||||
InternalPacketMessage message;
|
||||
char __padding[8];
|
||||
u8 __padding[4];
|
||||
};
|
||||
} InternalPacket;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user