PDA

View Full Version : سوال: محاسبه دقیق پهنای باند مصرفی اینترنت و پروتکل raw



amin_sltny
پنج شنبه 18 دی 1393, 08:15 صبح
سلام بچه ها.

من سورسی می خوام که بشه مصرف پهنای باند اینترنت را جدا از مصرف داخلی شبکه محلی محاسبه کنه برای همین با استفاده از پروتکل raw سورسی به شکل زیر نوشته ام:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;


namespace Networking
{
class Program
{
static void Main(string[] args)
{
long SumInternet = 0;
IPAddress ip = null;


foreach(IPAddress item in Dns.GetHostAddresses(Environment.MachineName))
{
if(item.AddressFamily == AddressFamily.InterNetwork)
{
ip = item;
break;
}
}


if (ip == null)
return;
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Console.WriteLine("IP Address: {0} is binding . . .", ip);
sock.Bind(new IPEndPoint(ip, 0));


byte[] outValue = BitConverter.GetBytes(0);
byte[] inValue = BitConverter.GetBytes(1);
sock.IOControl(IOControlCode.ReceiveAll, inValue, outValue);



while (true)
{
byte[] buffer = new byte[sock.ReceiveBufferSize];


/* uint bytesAvailable = BitConverter.ToUInt32(outValue, 0);
Console.WriteLine("server has {0} bytes pending. ",
bytesAvailable);
bytesAvailable = BitConverter.ToUInt32(inValue, 0);
Console.WriteLine("server has {0} bytes pending. ",
bytesAvailable);*/
// Console.WriteLine("Available property says {1}.",
// sock.Available);
int count = sock.Receive(buffer);


//sock.Send(Encoding.ASCII.GetBytes(request));
IPHeader hdr = new IPHeader(buffer, count);

Console.WriteLine("IP : {0} -> {1} Len: {2} ", hdr.SourceAddress.ToString(), hdr.DestinationAddress, hdr.TotalLength);
if (!IsPrivateIp(hdr.DestinationAddress.ToString()))
{
SumInternet += long.Parse(hdr.TotalLength);
}

if(hdr.ProtocolType == Protocol.TCP)
{
TCPHeader tcpheader = new TCPHeader(buffer, count);


Console.WriteLine("TCP :{0} -> {1} Len: {2}", tcpheader.SourcePort, tcpheader.DestinationPort, tcpheader.HeaderLength + tcpheader.MessageLength);
}
else if (hdr.ProtocolType == Protocol.UDP)
{


}






Console.WriteLine("");
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.Write("Internet Usage : {0} ", SumInternet);
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("");
}




}


public static bool IsPrivateIp(string ipAddress)
{
return (ipAddress.StartsWith("192.168") || ipAddress.Equals("127.0.0.1") || ipAddress.StartsWith("10.0"));
}


}
}





می خواستم نظرتون را بهم بگید و اینکه آیا پروتکل raw هم packet های ارسالی را نشون میده هم پکت های دریافتی؟ یه سوال دیگه هم اینکه میشه در پکت ها تغییری اعمال کرد. مثلا مسیر پکت های حاوی پروتکل http را عوض کرد؟؟

ممنون

elec60
پنج شنبه 18 دی 1393, 10:47 صبح
http://www.fiddler2.com/fiddler/dev/IFiddlerExtension.asp

http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580 (http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580)

http://www.m0interactive.com/archives/2008/02/06/how_to_calculate_network_bandwidth_speed_in_c_/

amin_sltny
پنج شنبه 18 دی 1393, 12:32 عصر
http://www.fiddler2.com/fiddler/dev/IFiddlerExtension.asp

http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580 (http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580)

http://www.m0interactive.com/archives/2008/02/06/how_to_calculate_network_bandwidth_speed_in_c_/

ممنون دوست عزیز در مورد fiddler باید بگم که فقط پروتکل های http https ftp را کنترل میکنه ولی من می خوام وقتی یه برنامه ای جدا از این پروتکل ها به اینترنت وصل بشه اون را بست و از وصل شدنش جلوگیری کرد.

اما در مورد دو لینک دیگه که مربوط به محاسبه ترافیک شبکس باید بگم مشکل این جاس که ترافیک lan را هم جز شبکه میاره. مثلا در برنامه مدیریت گیم نت و کافی نت من می خوام وقتی کاربر تحت بازی شبکه است و ترافیک بازی شبکش جز ترافیک اینترنت نیاد.