-
inet_addr and ntoa in C#
How do you do this?
i found IPAddress which has both HostToNetworkOrder and NetworkToHostOrder which work for htons and the otherone i can't remember.
I'm thinking the IPAddress.Parse Member is for inet_addr and ntoa but i can't seem to get it to work ;|
Code:
public static void Main()
{
Test p = new Test();
p.Port = 1080;
p.Type = 4;
p.UserID = "anonymous";
p.Password = "blah";
p.Server2 = "USEast.Battle.Net";
p.Port2 = 6112;
if (File.Exists(FILE_NAME) == true)
{
Console.WriteLine("{0} Exists, Loading...");
StreamReader sr = File.OpenText(FILE_NAME);
string input;
while ((input = sr.ReadLine()) != null)
{
Console.WriteLine("Validating Address...");
Regex r = new Regex(@"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", RegexOptions.Compiled);
Match m = r.Match(input);
if (m.Success == true)
{
Console.WriteLine("{0} is Valid!", m.Value);
try
{
Socket mySocket;
if ((mySocket = Connect(m.Value, p.Port)) != null)
{
NetworkStream myNetworkStream = new NetworkStream(mySocket);
if (myNetworkStream.CanWrite == true)
{
StreamWriter myStreamWriter = new StreamWriter(myNetworkStream);
DataBuffer myDataBuffer = new DataBuffer();
if (p.Type == 4)
{
Console.WriteLine("SOCKS 4 Connected, Authenticating...");
myDataBuffer.InsertByte(4);
myDataBuffer.InsertByte(1);
myDataBuffer.InsertInt16(IPAddress.HostToNetworkOrder(p.Port2));
myDataBuffer.InsertByteArray(Parse(p.Server2));
myDataBuffer.InsertCString(p.UserID);
}
else if (p.Type == 5)
{
myDataBuffer.InsertByte(5);
myDataBuffer.InsertByte(3);
myDataBuffer.InsertByte(0);
myDataBuffer.InsertByte(2);
}
myDataBuffer.WriteToOutputStream(myStreamWriter.BaseStream);
if (myNetworkStream.CanRead == true)
{
int count = 0;
do
{
StreamReader myStreamReader = new StreamReader(myNetworkStream);
DataReader myDataReader = new DataReader(myStreamReader.BaseStream);
myDataReader.Seek(1);
if (p.Type == 4)
{
switch (myDataReader.ReadByte())
{
case 0x90:
Console.WriteLine("SOCKS 4 Request Granted");
break;
case 0x91:
Console.WriteLine("SOCKS 4 Request Rejected Or Failed");
break;
case 0x92:
Console.WriteLine("SOCKS 4 Request Rejected Because SOCKS server cannot IDENT on the client");
break;
case 0x93:
Console.WriteLine("SOCKS 4 Request Rejected Because the Client Program and the ID Report Different User-IDs");
break;
default:
Console.WriteLine("SOCKS 4 Request Failed, Unknown Reason");
break;
}
}
else if (p.Type == 5)
{
myDataReader.Seek(1);
count++;
switch (count)
{
case 0x01:
switch (myDataReader.ReadByte())
{
case 0x00:
Console.WriteLine("SOCKS 5 Connected, Authenticating...");
myDataBuffer.InsertByte(5);
myDataBuffer.InsertByte(1);
myDataBuffer.InsertByte(0);
myDataBuffer.InsertByte(1);
myDataBuffer.InsertInt16(IPAddress.HostToNetworkOrder(p.Port2));
myDataBuffer.InsertByteArray(Parse(p.Server2));
myDataBuffer.WriteToOutputStream(myStreamWriter.BaseStream);
count++;
break;
case 0x02:
Console.WriteLine("SOCKS 5 Connected, UserID/Password Required, Sending...");
myDataBuffer.InsertByte(1);
myDataBuffer.InsertByte(Convert.ToByte(p.UserID.Length));
myDataBuffer.InsertByteArray(Encoding.ASCII.GetBytes(p.UserID));
myDataBuffer.InsertByte(Convert.ToByte(p.Password.Length));
myDataBuffer.InsertByteArray(Encoding.ASCII.GetBytes(p.Password));
break;
case 0xFF:
Console.WriteLine("SOCKS 5 Connected, No Methods Supported");
break;
default:
Console.WriteLine("SOCKS 5 Request Failed, Unknown Reason");
break;
}
break;
case 0x02:
switch (myDataReader.ReadByte())
{
case 0x00:
Console.WriteLine("SOCKS 5 UserID/Password Accepted, Authenticating...");
myDataBuffer.InsertByte(5);
myDataBuffer.InsertByte(1);
myDataBuffer.InsertByte(0);
myDataBuffer.InsertByte(1);
myDataBuffer.InsertInt16(IPAddress.HostToNetworkOrder(p.Port2));
myDataBuffer.InsertByteArray(Parse(p.Server2));
myDataBuffer.WriteToOutputStream(myStreamWriter.BaseStream);
break;
default:
Console.WriteLine("SOCKS 5 Request Failed, UserID/Password Rejected");
break;
}
break;
case 0x03:
switch (myDataReader.ReadByte())
{
case 0x00:
Console.WriteLine("SOCKS 5: Request Granted");
break;
case 0x01:
Console.WriteLine("SOCKS 5: Request Failed, General Server Failure");
break;
case 0x02:
Console.WriteLine("SOCKS 5: Request Failed, Connection Not Allowed By Ruleset");
break;
case 0x03:
Console.WriteLine("SOCKS 5: Request Failed, Network Unreachable");
break;
case 0x04:
Console.WriteLine("SOCKS 5: Request Failed, Host Unreachable");
break;
case 0x05:
Console.WriteLine("SOCKS 5: Request Failed, Connection Refused");
break;
case 0x06:
Console.WriteLine("SOCKS 5: Request Failed, TTL Expired");
break;
case 0x07:
Console.WriteLine("SOCKS 5: Request Failed, Command Not Supported");
break;
case 0x08:
Console.WriteLine("SOCKS 5: Request Failed, Address type not supported");
break;
default:
Console.WriteLine("SOCKS 5: Request Failed, Unknown Reason");
break;
}
break;
default:
Console.WriteLine("SOCKS 5 Unexpected Message Recieved");
break;
}
}
} while(myNetworkStream.DataAvailable == true);
}
else if (myNetworkStream.CanRead == false)
{
Console.WriteLine("Network Stream is Not Readable!");
}
}
else if (myNetworkStream.CanWrite == false)
{
Console.WriteLine("Network Stream is Not Writeable!");
}
}
else
{
Console.WriteLine("Socket is NULL");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
else
{
Console.WriteLine("{0} was not found.", FILE_NAME);
}
Console.ReadLine();
}
public static byte[] Parse(string Server)
{
IPAddress address = IPAddress.Parse(Server);
return address.GetAddressBytes();
}
I get this when i run it:
Code:
{0} Exists, Loading...
Validating Address...
211.143.183.66 is Valid!
Connecting To... 211.143.183.66 on Port 1080...
Connection Established!
SOCKS 4 Connected, Authenticating...
System.FormatException: An invalid IP address was specified.
at System.Net.IPAddress.Parse(String ipString)
at Test.Parse(String Server) in c:\documents and settings\joel zimmerman\my d
ocuments\my programming\c#\proxy\proxy.gui.console\entrypoint.cs:line 356
at Test.Main() in c:\documents and settings\joel zimmerman\my documents\my pr
ogramming\c#\proxy\proxy.gui.console\entrypoint.cs:line 153
Validating Address...
61.139.59.112 is Valid!
Connecting To... 61.139.59.112 on Port 1080...
Connection Established!
SOCKS 4 Connected, Authenticating...
System.FormatException: An invalid IP address was specified.
at System.Net.IPAddress.Parse(String ipString)
at Test.Parse(String Server) in c:\documents and settings\joel zimmerman\my d
ocuments\my programming\c#\proxy\proxy.gui.console\entrypoint.cs:line 356
at Test.Main() in c:\documents and settings\joel zimmerman\my documents\my pr
ogramming\c#\proxy\proxy.gui.console\entrypoint.cs:line 153
Validating Address...
200.168.38.160 is Valid!
Connecting To... 200.168.38.160 on Port 1080...
Connection Established!
SOCKS 4 Connected, Authenticating...
System.FormatException: An invalid IP address was specified.
at System.Net.IPAddress.Parse(String ipString)
at Test.Parse(String Server) in c:\documents and settings\joel zimmerman\my d
ocuments\my programming\c#\proxy\proxy.gui.console\entrypoint.cs:line 356
at Test.Main() in c:\documents and settings\joel zimmerman\my documents\my pr
ogramming\c#\proxy\proxy.gui.console\entrypoint.cs:line 153
Validating Address...
199.77.129.53 is Valid!
Connecting To... 199.77.129.53 on Port 1080...
I could also use some tips to making my code... BETTER. It looks horrible, and i'm sure it's HORRIBLY inefficient, haha.
Thank You :)
- Joel