Skip to content

Commit

Permalink
sent: 1
Browse files Browse the repository at this point in the history
  • Loading branch information
liulilittle committed Feb 24, 2020
1 parent 500ec75 commit 28db1c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 73 deletions.
7 changes: 5 additions & 2 deletions SkylakeNAT/nat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,11 @@ bool NAT::PrivateIntput(ip_hdr* packet, int size) {
break;
memcpy(message_data.get(), packet, size);
try {
socket->Send(NATCommands_kEthernetOutput, message_data, size);
success |= true;
int sent = 1;
for (int c = 0; c < sent; c++) {
socket->Send(NATCommands_kEthernetOutput, message_data, size);
success |= true;
}
}
catch (std::exception&) {
socket->Close();
Expand Down
95 changes: 24 additions & 71 deletions SupersocksR/SkylakeNAT/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,9 @@ private void StartReceive(IAsyncResult ar)
{
Socket socket = null;
lock (this._syncobj)
{
socket = this._socket;
}
if (socket == null)
{
return;
}
if (ar == null)
{
if (!_fhdr)
Expand All @@ -154,9 +150,7 @@ private void StartReceive(IAsyncResult ar)
socket.BeginReceive(_message, _fseek, suplus, SocketFlags.None, out error, StartReceive, null);
}
if (error == SocketError.IOPending)
{
error = SocketError.Success;
}
}
else
{
Expand Down Expand Up @@ -300,20 +294,16 @@ protected internal virtual void OnMessage(SkylakeNATMessage e)
this.Message?.Invoke(this, e);
}

public virtual bool Send(SkylakeNATMessage message)
public virtual bool Send(SkylakeNATMessage message, int sent = 1)
{
if (message == null)
{
return false;
}
Socket socket = null;
lock (this._syncobj)
{
socket = this._socket;
if (socket == null)
{
return false;
}
}
BufferSegment payload_segment = message.Payload;
#if !_USE_RC4_SIMPLE_ENCIPHER || __USE_UDP_PAYLOAD_TAP_PACKET
Expand All @@ -340,14 +330,19 @@ public virtual bool Send(SkylakeNATMessage message)
#if _USE_RC4_SIMPLE_ENCIPHER
try
{
socket.BeginSendTo(packet, 0, packet.Length, SocketFlags.None, this.LocalEndPoint, (ar) =>
if (sent <= 0)
sent = 1;
for (int i = 0; i < sent; i++)
{
try
socket.BeginSendTo(packet, 0, packet.Length, SocketFlags.None, this.LocalEndPoint, (ar) =>
{
socket.EndSendTo(ar);
}
catch (Exception) { }
}, null);
try
{
socket.EndSendTo(ar);
}
catch (Exception) { }
}, null);
}
return true;
}
catch (Exception)
Expand All @@ -370,9 +365,7 @@ public virtual bool Send(SkylakeNATMessage message)
error = SocketError.SocketError;
}
if (error != SocketError.Success)
{
this.CloseOrAbort();
}
}, null);
}
catch (Exception)
Expand Down Expand Up @@ -407,9 +400,7 @@ public virtual void Dispose()
{
var l = rsv.List;
if (l != null)
{
l.Remove(rsv);
}
}
this._rsv_current = null;
}
Expand Down Expand Up @@ -502,25 +493,19 @@ public Router(IPAddress ethernet, int port, string key, int subtract)
if (_sockets.TryGetValue(socket.Address, out LinkedList<SkylakeNATClient> s))
{
if (s == null)
{
deleteCompletely = true;
}
else
{
var node = socket._rsv_current;
if (node != null)
{
var l = node.List;
if (l != null)
{
l.Remove(node);
}
socket._rsv_current = null;
}
if (s.Count <= 0)
{
deleteCompletely = true;
}
}
}
if (deleteCompletely)
Expand All @@ -543,16 +528,12 @@ public Router(IPAddress ethernet, int port, string key, int subtract)
this._onSocketMessage = (sender, e) =>
{
if (sender is SkylakeNATClient socket)
{
this.ProcessMessage(socket, e);
}
};
this._onAuthentication = (sender, e) =>
{
if (sender is SkylakeNATClient socket)
{
this.ProcessAuthentication(socket);
}
};
// 建立以太网NAT链路工作引擎
#if NO_USAGE_PCAP_NET
Expand All @@ -574,9 +555,7 @@ public Router(IPAddress ethernet, int port, string key, int subtract)
bool freely = false;
var context = kv.Value;
if (context == null)
{
freely = true;
}
else
{
SkylakeNATClient clients = null;
Expand All @@ -588,10 +567,7 @@ public Router(IPAddress ethernet, int port, string key, int subtract)
clients = context.client;
}
}
if (clients != null)
{
clients.OnAbort(EventArgs.Empty);
}
clients?.OnAbort(EventArgs.Empty);
}
if (freely)
{
Expand All @@ -605,9 +581,7 @@ public Router(IPAddress ethernet, int port, string key, int subtract)
{
_addressAllocation.TryRemove(clients.Id, out address);
if (address == null)
{
address = clients.Address;
}
if (address != null && !Ethernet.Equals(address, IPAddress.Any))
{
_assignedAddresses.Remove(address);
Expand Down Expand Up @@ -664,33 +638,23 @@ public struct Dhcp
protected virtual IPAddress AddressAllocation(int id)
{
if (0 == id)
{
return null;
}
lock (_addressAllocation)
{
if (_addressAllocation.TryGetValue(id, out IPAddress address) && address != null)
{
return address;
}
foreach (IPAddress i in _dhcpAddressAllocationRange.AsEnumerable())
{
if (i == null)
{
continue;
}
fixed (byte* p = i.GetAddressBytes())
{
byte l = p[3];
if (l <= 1 || l >= 255)
{
continue;
}
}
if (_assignedAddresses.Contains(i))
{
continue;
}
_addressAllocation[id] = i;
_assignedAddresses.Add(i);
return i;
Expand All @@ -703,9 +667,7 @@ protected virtual void ProcessAuthentication(SkylakeNATClient socket)
{
IPAddress localIP = this.AddressAllocation(socket.Id);
if (localIP == null)
{
socket.Close();
}
else
{
if (this.ResponseAuthentication(socket, localIP, this._dhcpServerAddress, this._dnsServerAddress))
Expand All @@ -722,9 +684,7 @@ protected virtual void ProcessAuthentication(SkylakeNATClient socket)
}
#if !__USE_UDP_PAYLOAD_TAP_PACKET
if (s != null)
{
socket._rsv_current = s.AddLast(socket);
}
#endif
}
}
Expand All @@ -737,16 +697,12 @@ protected virtual void ProcessAuthentication(SkylakeNATClient socket)
protected virtual bool CloseManyClient(IPAddress address)
{
if (address == null)
{
return false;
}
lock (this._sockets)
{
_sockets.TryRemove(address, out LinkedList<SkylakeNATClient> s);
if (s == null)
{
return false;
}
var node = s.First;
SkylakeNATClient socket = null;
while (node != null)
Expand Down Expand Up @@ -811,46 +767,43 @@ protected virtual void PrivateInput(SkylakeNATClient socket, IPFrame packet)
this.NAT.PrivateInput(packet);
}

protected virtual bool SendMessageToClient(IPAddress address, Func<SkylakeNATMessage> message)
protected virtual bool Send(IPAddress address, Func<SkylakeNATMessage> message, int sent = 1)
{
if (address == null || message == null)
return false;
SkylakeNATClient socket = this.GetClient(address, out int sessions);
if (socket == null)
return false;
SkylakeNATMessage packet = message();
if (!socket.Send(packet))
if (!socket.Send(packet, sent))
{
for (int i = 0; i < sessions; i++)
{
socket = this.GetClient(address, out sessions);
if (socket == null)
break;
if (socket.Send(packet))
if (socket.Send(packet, sent))
return true;
}
}
return false;
}

protected virtual void PrivateOutput(IPFrame packet)
protected virtual bool PrivateOutput(IPFrame packet)
{
if (packet != null)
{
this.SendMessageToClient(packet.Destination, () =>
new SkylakeNATMessage(IPv4Layer.ToArray(packet))
{
Commands = Commands.NATCommands_kEthernetInput,
});
}
if (packet == null)
return false;
return this.Send(packet.Destination, () =>
new SkylakeNATMessage(IPv4Layer.ToArray(packet))
{
Commands = Commands.NATCommands_kEthernetInput,
});
}

public virtual void Listen(int backlog)
{
if (backlog <= 0)
{
backlog = 1;
}
Exception exception = null;
do
{
Expand Down

0 comments on commit 28db1c6

Please sign in to comment.