/*** SenderWOLRequest(IPINFO ipInfo) * * Parameters: * * ipInfo - The global class containing all of the IP like info about the * this machine we are running on; and the server we want to contact. * * Return Values: * None * * Errors: * * * Description: * * This is the start of the client side RemoteWol code. * ------------------------------------------------------------ */ private static void SenderWOLRequest(IPINFO ipInfo) { TcpClient tcpClient = new TcpClient(); IPEndPoint ipEndPoint = new IPEndPoint(ipInfo.ip4, ipInfo.port); NetworkStream targetStrm = null; try { // connect to the server; there is some default timeout, it does break out if it can not connect. tcpClient.Connect(ipEndPoint); targetStrm = tcpClient.GetStream(); // send the MAC if (ipInfo.mac != null) { MSG msg = new MSG(); BHE bhe = new BHE(); msg.cmdmsg = MSG.CMDMSG.BroadCastMAC; msg.rgbData = ipInfo.mac.GetAddressBytes(); // ask that the magic packet be sent if (WriteMsgToStream(targetStrm, msg)) { msg = ReadMsgFromStream(targetStrm); } // See what we got back, it should only be the MAC we sent if (msg != null && msg.cmdmsg == MSG.CMDMSG.Succeeded) { Console.WriteLine("Magic Packet Sent."); } else { Console.WriteLine("Unable to send Magic Packet"); } } Console.WriteLine(""); // success or failure, close up the tcp Client targetStrm.Close(); tcpClient.Close(); } // we didn't even connect, probably the IP address is bad. catch (Exception e) { Console.WriteLine(e.ToString()); return; } }
public void AddBHE(BHE bhe) { if (iNext < listBHE.Count) { listBHE[iNext] = bhe; } else { listBHE.Add(bhe); } iNext = (iNext + 1) % (int)listBHE.Capacity; }
/*** void ListenForWOLRequest(Object obj) * * Parameters: * * obj - The obj is really an ipInfo and is casted to such. * The ipInfo class containing all of the IP like info about the * this machine we are running on; and the server we want to contact. * * Return Values: * None * * Errors: * * * Description: * * This is the start of the server side RemoteWol code. * * This was written as a seperate thread so that debugging of * the client in the main thread against a server in a different * thread could be done in one debug session. Client / Server * combined application is only when the debug flag fRunServerAndClient is TRUE. * ------------------------------------------------------------ */ private static void ListenForWOLRequest(Object obj) { IPINFO ipInfo = (IPINFO)obj; uint terminate = 0; TcpListener tcpListener = null; try { // this will throw and exception if ipAddress == null; tcpListener = new TcpListener(ipInfo.myIPs.ipMe, ipInfo.port); tcpListener.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); return; } // go in a loop waiting for someone to connect. do { TcpClient client = null; NetworkStream targetStrm = null; MSG msg;; try { // block until we get something Console.WriteLine("Listening on port: {0}.", ipInfo.port.ToString()); client = tcpListener.AcceptTcpClient(); targetStrm = client.GetStream(); // see if we can read something reasonable msg = ReadMsgFromStream(targetStrm); if (msg != null) { Console.WriteLine("{0}: Message {1} detected.", DateTime.Now.ToString("g"), msg.cmdmsg.ToString()); switch (msg.cmdmsg) { case MSG.CMDMSG.BroadCastMAC: // see if we have a valid MAC if (msg.rgbData.Length >= 6) { MSG msgRet = new MSG(MSG.CMDMSG.Failed); BHE bhe = new BHE(); // we only care about the MAC address Array.Resize(ref msg.rgbData, 6); PhysicalAddress macAddress = new PhysicalAddress(msg.rgbData); IPEndPoint ipEP = (IPEndPoint)client.Client.RemoteEndPoint; Console.WriteLine("Request to broadcast MAC: {0} by IP: {1}.", macAddress.ToString(), ipEP.ToString()); bhe.IP = ipEP.Address; bhe.MAC = macAddress; bhe.UtcTime = DateTime.Now.ToUniversalTime(); ipInfo.bhs.AddBHE(bhe); // do the broadcast msg.cmdmsg = MSG.CMDMSG.Failed; if (BroadCast(macAddress)) { msgRet.cmdmsg = MSG.CMDMSG.Succeeded; } // write back our status code WriteMsgToStream(targetStrm, msgRet); } break; case MSG.CMDMSG.HistoryRequest: WriteMsgToStream(targetStrm, ipInfo.bhs.GetBroadcastHistoryReplyMsg()); break; case MSG.CMDMSG.HistoryReply: WriteMsgToStream(targetStrm, new MSG(MSG.CMDMSG.Failed)); break; case MSG.CMDMSG.Terminate: WriteMsgToStream(targetStrm, new MSG(MSG.CMDMSG.Succeeded)); terminate = 0xFFFFFFFF; break; default: break; } } // we have done our work, close the stream targetStrm.Close(); client.Close(); } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); terminate = 0; } } while (terminate != 0xFFFFFFFF); // just stop listening, we are done. tcpListener.Stop(); }
/*** PrintBroadcastHistor(IPINFO ipInfo) * * Parameters: * * ipInfo - The global class containing all of the IP like info about the * this machine we are running on; and the server we want to contact. * * Return Values: * None * * Errors: * * * Description: * * This is the start of the client side RemoteWol code. * ------------------------------------------------------------ */ private static void PrintBroadcastHistory(IPINFO ipInfo, DateTimeKind dtk) { TcpClient tcpClient = new TcpClient(); IPEndPoint ipEndPoint = new IPEndPoint(ipInfo.ip4, ipInfo.port); NetworkStream targetStrm = null; try { // connect to the server; there is some default timeout, it does break out if it can not connect. tcpClient.Connect(ipEndPoint); targetStrm = tcpClient.GetStream(); MSG msg = new MSG(MSG.CMDMSG.HistoryRequest); // ask that the magic packet be sent if (WriteMsgToStream(targetStrm, msg)) { msg = ReadMsgFromStream(targetStrm); } // See what we got back, it should be a history reply if (msg != null && msg.cmdmsg == MSG.CMDMSG.HistoryReply && msg.rgbData.Length % BHE.Length == 0) { byte[] rgbBHE = new byte[BHE.Length]; BHE bhe = null; Console.WriteLine("Got the broadcast history."); for (int i = 0; i < msg.rgbData.Length; i += BHE.Length) { // get a bhe Array.Copy(msg.rgbData, i, rgbBHE, 0, BHE.Length); bhe = new BHE(rgbBHE); switch (dtk) { case DateTimeKind.Utc: Console.WriteLine("UTC Time: {0}, MAC: {1} from IP: {2}.", bhe.UtcTime.ToString("G"), bhe.MAC.ToString(), bhe.IP.ToString()); break; case DateTimeKind.Local: Console.WriteLine("Local Time: {0}, MAC: {1} from IP: {2}.", bhe.UtcTime.ToLocalTime().ToString("G"), bhe.MAC.ToString(), bhe.IP.ToString()); break; } } } else { Console.WriteLine("Unable to get Broadcast History."); } Console.WriteLine(""); // success or failure, close up the tcp Client targetStrm.Close(); tcpClient.Close(); } // we didn't even connect, probably the IP address is bad. catch (Exception e) { Console.WriteLine(e.ToString()); return; } }
/*** PrintBroadcastHistor(IPINFO ipInfo) * * Parameters: * * ipInfo - The global class containing all of the IP like info about the * this machine we are running on; and the server we want to contact. * * Return Values: * None * * Errors: * * * Description: * * This is the start of the client side RemoteWol code. * ------------------------------------------------------------ */ private static void PrintBroadcastHistory(IPINFO ipInfo, DateTimeKind dtk) { TcpClient tcpClient = new TcpClient(); IPEndPoint ipEndPoint = new IPEndPoint(ipInfo.ip4, ipInfo.port); NetworkStream targetStrm = null; try { // connect to the server; there is some default timeout, it does break out if it can not connect. tcpClient.Connect(ipEndPoint); targetStrm = tcpClient.GetStream(); MSG msg = new MSG(MSG.CMDMSG.HistoryRequest); // ask that the magic packet be sent if (WriteMsgToStream(targetStrm, msg)) { msg = ReadMsgFromStream(targetStrm); } // See what we got back, it should be a history reply if (msg != null && msg.cmdmsg == MSG.CMDMSG.HistoryReply && msg.rgbData.Length % BHE.Length == 0) { byte[] rgbBHE = new byte[BHE.Length]; BHE bhe = null; Console.WriteLine("Got the broadcast history."); for (int i = 0; i < msg.rgbData.Length; i += BHE.Length) { // get a bhe Array.Copy(msg.rgbData, i, rgbBHE, 0, BHE.Length); bhe = new BHE(rgbBHE); switch(dtk) { case DateTimeKind.Utc: Console.WriteLine("UTC Time: {0}, MAC: {1} from IP: {2}.", bhe.UtcTime.ToString("G"), bhe.MAC.ToString(), bhe.IP.ToString()); break; case DateTimeKind.Local: Console.WriteLine("Local Time: {0}, MAC: {1} from IP: {2}.", bhe.UtcTime.ToLocalTime().ToString("G"), bhe.MAC.ToString(), bhe.IP.ToString()); break; } } } else { Console.WriteLine("Unable to get Broadcast History."); } Console.WriteLine(""); // success or failure, close up the tcp Client targetStrm.Close(); tcpClient.Close(); } // we didn't even connect, probably the IP address is bad. catch (Exception e) { Console.WriteLine(e.ToString()); return; } }
/*** void ListenForWOLRequest(Object obj) * * Parameters: * * obj - The obj is really an ipInfo and is casted to such. * The ipInfo class containing all of the IP like info about the * this machine we are running on; and the server we want to contact. * * Return Values: * None * * Errors: * * * Description: * * This is the start of the server side RemoteWol code. * * This was written as a seperate thread so that debugging of * the client in the main thread against a server in a different * thread could be done in one debug session. Client / Server * combined application is only when the debug flag fRunServerAndClient is TRUE. * ------------------------------------------------------------ */ private static void ListenForWOLRequest(Object obj) { IPINFO ipInfo = (IPINFO) obj; uint terminate = 0; TcpListener tcpListener = null; try { // this will throw and exception if ipAddress == null; tcpListener = new TcpListener(ipInfo.myIPs.ipMe, ipInfo.port); tcpListener.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); return; } // go in a loop waiting for someone to connect. do { TcpClient client = null; NetworkStream targetStrm = null; MSG msg; ; try { // block until we get something Console.WriteLine("Listening on port: {0}.", ipInfo.port.ToString()); client = tcpListener.AcceptTcpClient(); targetStrm = client.GetStream(); // see if we can read something reasonable msg = ReadMsgFromStream(targetStrm); if (msg != null) { Console.WriteLine("{0}: Message {1} detected.", DateTime.Now.ToString("g"), msg.cmdmsg.ToString()); switch (msg.cmdmsg) { case MSG.CMDMSG.BroadCastMAC: // see if we have a valid MAC if (msg.rgbData.Length >= 6) { MSG msgRet = new MSG(MSG.CMDMSG.Failed); BHE bhe = new BHE(); // we only care about the MAC address Array.Resize(ref msg.rgbData, 6); PhysicalAddress macAddress = new PhysicalAddress(msg.rgbData); IPEndPoint ipEP = (IPEndPoint) client.Client.RemoteEndPoint; Console.WriteLine("Request to broadcast MAC: {0} by IP: {1}.", macAddress.ToString(), ipEP.ToString()); bhe.IP = ipEP.Address; bhe.MAC = macAddress; bhe.UtcTime = DateTime.Now.ToUniversalTime(); ipInfo.bhs.AddBHE(bhe); // do the broadcast msg.cmdmsg = MSG.CMDMSG.Failed; if (BroadCast(macAddress)) { msgRet.cmdmsg = MSG.CMDMSG.Succeeded; } // write back our status code WriteMsgToStream(targetStrm, msgRet); } break; case MSG.CMDMSG.HistoryRequest: WriteMsgToStream(targetStrm, ipInfo.bhs.GetBroadcastHistoryReplyMsg()); break; case MSG.CMDMSG.HistoryReply: WriteMsgToStream(targetStrm, new MSG(MSG.CMDMSG.Failed)); break; case MSG.CMDMSG.Terminate: WriteMsgToStream(targetStrm, new MSG(MSG.CMDMSG.Succeeded)); terminate = 0xFFFFFFFF; break; default: break; } } // we have done our work, close the stream targetStrm.Close(); client.Close(); } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); terminate = 0; } } while (terminate != 0xFFFFFFFF); // just stop listening, we are done. tcpListener.Stop(); }