IP协议校验和计算(发送方)
1.界面展示
2.源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] ipHead = new string[10];
string allSum;
Dictionary<string, int> tlp;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tlp = new Dictionary<string, int>();
tlp.Add("1-ICMP", 1);
tlp.Add("2-IGMP", 2);
tlp.Add("3-GGP", 3);
tlp.Add("4-IP", 4);
tlp.Add("6-TCP", 6);
tlp.Add("8-EGP", 8);
tlp.Add("17-UDP", 17);
tlp.Add("88-IGRP", 88);
tlp.Add("89-OSPF", 89);
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (!infoCheck())
{
return;
}
ipHead[5] = "0000";
CacuSum(ipHead[0], 1);
string overflow = "";
if (allSum.Length > 4)
{
for (int i = 0; i < allSum.Length - 4; i++)
{
overflow += allSum[i];
}
}
allSum = allSum.Remove(0, allSum.Length - 4);
string checksum2 = ConvertHexToBin(TwoSum(overflow, allSum));
string cumsum = Cumulativesum(TwoSum(overflow, allSum));
textBox9.Text = checksum2;
textBox11.Text = cumsum;
}
private bool infoCheck()
{
DialogResult result;
string Source = textBox8.Text;
string Destination = textBox10.Text;
if (!IsIP(Source) || !IsIP(Destination))
{
MessageBoxButtons buttons = MessageBoxButtons.OK;
string message = "ip地址不合法";
string caption = "提示";
result = MessageBox.Show(this, message, caption, buttons);
return false;
}
string Version, HeaderLength;
string SerType, TotalLength, TimeToLive, Protocol;
try
{
this.comboBox1.SelectedIndex = 0;
Version = comboBox1.Text;
HeaderLength = textBox1.Text;
SerType = Convert.ToString(Convert.ToInt32(comboBox3.Text), 16).PadLeft(2, '0');
ipHead[0] = Version + HeaderLength + SerType;
TotalLength = Convert.ToString(Convert.ToInt32(textBox3.Text), 16).PadLeft(4, '0');
ipHead[1] = TotalLength;
ipHead[2] = Convert.ToString(Convert.ToInt32(textBox4.Text), 16).PadLeft(4, '0'); //标识
int data = Convert.ToInt32(Convert.ToString(Convert.ToInt32(textBox5.Text), 2).PadLeft(3, '0') + Convert.ToString(Convert.ToInt32(textBox6.Text), 2).PadLeft(13, '0'), 2);//标志
ipHead[3] = Convert.ToString(data, 16).PadLeft(4, '0');
TimeToLive = Convert.ToString(Convert.ToInt32(textBox7.Text), 16).PadLeft(2, '0');
Protocol = Convert.ToString(tlp[comboBox2.Text], 16).PadLeft(2, '0');
ipHead[4] = TimeToLive + Protocol;
}
catch (Exception)
{
Version = "";
HeaderLength = "";
SerType = "";
string message = "输入不合法";
string caption = "提示";
result = MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
return false;
}
string[] ipSource = Source.Split('.');
string[] ipDestination = Destination.Split('.');
for (int i = 0; i < 4; i++)
{
ipSource[i] = Convert.ToString(Convert.ToInt32(ipSource[i]), 16).PadLeft(2, '0'); //十进制转十六进制
}
for (int i = 0; i < 4; i++)
{
ipDestination[i] = Convert.ToString(Convert.ToInt32(ipDestination[i]), 16).PadLeft(2, '0'); //十进制转十六进制
}
ipHead[6] = ipSource[0] + ipSource[1];
ipHead[7] = ipSource[2] + ipSource[3];
ipHead[8] = ipDestination[0] + ipDestination[1];
ipHead[9] = ipDestination[2] + ipDestination[3];
return true;
}
/*ip正则校验*/
private bool IsIP(string ip)
{
//判断是否为IP,加上是否有划分校验(\/[012]?\d|3[0-2])?
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?(\/[012]?\d|\/3[0-2])?)$");
}
/*递归计算ip头部所有十六进制数的和*/
private string CacuSum(string a, int i)
{
string tmp = "";
tmp = TwoSum(a, ipHead[i]);
if (i + 1 == 10)
{
allSum = tmp;
return tmp;
}
CacuSum(tmp, i + 1);
return "";
}
/*计算两个十六进制数的和*/
private string TwoSum(string a, string b)
{
int aint = 0, bint = 0;
int flag = a.Length;
for (int i = 0; i < a.Length; i++)
{
flag--;
aint += JudgeLoc(a[i]) * Convert.ToInt32(Math.Pow(16, flag));
}
flag = b.Length;
for (int i = 0; i < b.Length; i++)
{
flag--;
bint += JudgeLoc(b[i]) * Convert.ToInt32(Math.Pow(16, flag));
}
string Sum = Convert.ToString(aint + bint, 16).PadLeft(4, '0');//存和的十六进制
return Sum;
}
/*找出十六进制字符对应的整型值*/
private int JudgeLoc(char c)
{
if (c > '0' && c <= '9')
{
return c - '0';
}
else if (c >= 'a' && c <= 'f')
{
return c - 'a' + 10;
}
return 0;
}
/*十六进制转二进制,取反*/
private string ConvertHexToBin(string hex)
{
int aint = 0;
int flag = hex.Length;
for (int i = 0; i < hex.Length; i++)
{
flag--;
aint += JudgeLoc(hex[i]) * Convert.ToInt32(Math.Pow(16, flag));
}
string bina = Convert.ToString(aint, 2).PadLeft(16, '0'); //十六进制转二进制
//二进制按位取反
string confBina = "";
for (int i = 0; i < bina.Length; i++)
{
char sigBina = bina[i];
if (sigBina == '0')
{
sigBina = '1';
}
else
{
sigBina = '0';
}
confBina += sigBina;
}
return confBina;
}
private string Cumulativesum(string hex)
{
int aint = 0;
int flag = hex.Length;
for (int i = 0; i < hex.Length; i++)
{
flag--;
aint += JudgeLoc(hex[i]) * Convert.ToInt32(Math.Pow(16, flag));
}
string bina = Convert.ToString(aint, 2).PadLeft(16, '0');
return bina;
}
private void button3_Click(object sender, EventArgs e)//转换
{
if (!infoCheck())
{
return;
}
ipHead[5] = "0000";
CacuSum(ipHead[0], 1);
string overflow = "";
if (allSum.Length > 4)
{
for (int i = 0; i < allSum.Length - 4; i++)
{
overflow += allSum[i];
}
}
allSum = allSum.Remove(0, allSum.Length - 4);
string checksum = ConvertHexToBin(TwoSum(overflow, allSum));
checksum = "0x" + string.Format("{0:x}", Convert.ToInt32(checksum, 2)).PadLeft(4, '0');//二进制转十六进制
textBox12.Text = checksum;
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter("C:\\Users\\Administrator\\Desktop\\11.txt", true);
sw.Write(label1.Text + comboBox1.Text + "\r\n" + label2.Text + "\r\r\r" + textBox1.Text + "\r\n"
+ label3.Text + "\r\r\r" + comboBox3.Text + "\r\n" + label4.Text + "\r\r\r" + textBox3.Text + "\r\n"
+ label5.Text + "\r\r\r" + textBox4.Text + "\r\n" + label6.Text + "\r\r\r" + textBox5.Text + "\r\n"
+ label7.Text + "\r\r\r" + textBox6.Text + "\r\n" + label8.Text + "\r\r\r" + textBox7.Text + "\r\n"
+ label9.Text + "\r\r\r" + comboBox2.Text + "\r\n" + label11.Text + "\r\r\r" + textBox8.Text + "\r\n"
+ label2.Text + "\r\r\r" + textBox10.Text + "\r\n" + label10.Text + "\r\r\r" + textBox12.Text + "\r\n");
try
{
string message = "保存成功!";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
catch (Exception)
{
string message = "保存失败!";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
sw.Flush();//文件流
sw.Close();//最后要关闭写入状态
}
private void button4_Click(object sender, EventArgs e)
{
int c = 20;
int datalength = int.Parse((textBox2.Text.ToString()));//把文本框里的值int转换后赋给a
int mut = int.Parse((textBox13.Text.ToString())) - c;
string TotalLength = Convert.ToString(datalength + c, 10);
textBox3.Text = TotalLength;
if (datalength < mut)
{
textBox14.Text = Convert.ToString(1, 10);
}
else
{
textBox14.Text = Math.Ceiling((double)datalength / mut).ToString();// Math.Ceiling取整,有余数就进1
}
}
private void button5_Click(object sender, EventArgs e)
{
int c = 20;
int mut = int.Parse((textBox13.Text.ToString())) - c;
int newph = int.Parse((comboBox4.Text.ToString()));
int s = int.Parse((textBox14.Text.ToString()));
if (s == 1)
{
if (newph == 1)
{
textBox6.Text = Convert.ToString(0, 10);
}
else
{
string message = "超出数据报最大分片数!";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
}
else if (s == 2)
{
if (newph == 1)
{
textBox6.Text = Convert.ToString(0, 10);
}
else if (newph == 2)
{
textBox6.Text = Math.Floor((double)mut / 8).ToString();// Math.Floor取整,余数都舍去
}
else
{
string message = "超出数据报最大分片数!";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
}
else if (s == 3)
{
if (newph == 1)
{
textBox6.Text = Convert.ToString(0, 10);
}
else if (newph == 2)
{
textBox6.Text = Math.Floor((double)mut / 8).ToString();// Math.Floor取整,余数都舍去
}
else if (newph == 3)
{
textBox6.Text = Math.Floor((double)mut * 2 / 8).ToString();// Math.Floor取整,余数都舍去
}
else
{
string message = "超出数据报最大分片数";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
}
else
{
string message = "当前仅支持划分三片数据报片";
string caption = "提示";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK);
}
}
private void button6_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("cmd.exe", "/k ping 222.31.142.14");
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox11_TextChanged(object sender, EventArgs e)
{
}
private void label14_Click(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox14_TextChanged(object sender, EventArgs e)
{
}
}
}
3.源项目连接
下载解压可用!
https://download.csdn.net/download/weixin_43488742/12116300