PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/www/exec_cmd.inc

http://magnolia-network-emulator.googlecode.com/
PHP | 590 lines | 588 code | 2 blank | 0 comment | 148 complexity | 27ae2998bd58744a4a3aaedd647d98c2 MD5 | raw file
  1. <?php
  2. require_once("find_int.inc");
  3. require_once 'Net/IPv4.php';
  4. function exec_int_config_cmd($postrointprint, $rointconfig) {
  5. $name = $postrointprint[$rointconfig]['name'];
  6. $ipaddressvalue = $postrointprint[$rointconfig]['ipaddressvalue'];
  7. $ipsubnetvalue = $postrointprint[$rointconfig]['ipsubnetvalue'];
  8. if (($ipaddressvalue != "") && ($ipsubnetvalue != "")) {
  9. $tmp = shell_exec("sudo ifconfig $name $ipaddressvalue netmask $ipsubnetvalue");
  10. }
  11. return 0;
  12. }
  13. function restart_network() {
  14. $tmp = shell_exec("sudo /etc/init.d/networking restart");
  15. return 0;
  16. }
  17. function flush_route() {
  18. $tmp = shell_exec("sudo route del default");
  19. return 0;
  20. }
  21. function check_emuromode() {
  22. if (file_exists("/root/alm/switch")) {
  23. $tmp = shell_exec("sudo rm -rf /root/alm/switch");
  24. }
  25. restore_router_mode_lite();
  26. if (!file_exists("/root/alm/router")) {
  27. $tmp = shell_exec("sudo mkdir /root/alm/router");
  28. }
  29. }
  30. function check_emuswmode() {
  31. if (file_exists("/root/alm/router")) {
  32. $tmp = shell_exec("sudo rm -rf /root/alm/router");
  33. }
  34. restore_switch_mode_lite();
  35. if (!file_exists("/root/alm/switch")) {
  36. $tmp = shell_exec("sudo mkdir /root/alm/switch");
  37. }
  38. }
  39. function startupmag() {
  40. find_all_int($all_int);
  41. $tmp = shell_exec("sudo /usr/sbin/brctl addbr br0");
  42. $tmp = shell_exec("ifconfig br0 up");
  43. startup_all_int($all_int);
  44. }
  45. function restore_switch_mode() {
  46. find_all_int($all_int);
  47. $tmp = shell_exec("ifconfig br0 up");
  48. startup_all_int($all_int);
  49. set_router_mode($all_int);
  50. set_switch_mode($all_int);
  51. }
  52. function restore_router_mode() {
  53. find_all_int($all_int);
  54. $tmp = shell_exec("ifconfig br0 up");
  55. startup_all_int($all_int);
  56. set_switch_mode($all_int);
  57. set_router_mode($all_int);
  58. }
  59. function set_switch_mode($all_int) {
  60. foreach ($all_int as $int) {
  61. $tmp = shell_exec("sudo /usr/sbin/brctl addif br0 $int");
  62. }
  63. $tmp = shell_exec("sudo /sbin/ifconfig br0 192.168.250.1 netmask 255.255.255.0");
  64. }
  65. function set_router_mode($all_int) {
  66. foreach ($all_int as $int) {
  67. $tmp = shell_exec("sudo /usr/sbin/brctl delif br0 $int");
  68. }
  69. $k = 200;
  70. foreach ($all_int as $int) {
  71. $tmp = shell_exec("sudo /sbin/ifconfig $int 192.168." . "$k" . ".1 netmask 255.255.255.0");
  72. $k++;
  73. }
  74. $tmp = shell_exec("sudo echo 1 > /proc/sys/net/ipv4/ip_forward");
  75. }
  76. function restore_switch_mode_lite() {
  77. find_all_int($all_int);
  78. $tmp = shell_exec("ifconfig br0 up");
  79. startup_all_int($all_int);
  80. set_router_mode_lite($all_int);
  81. set_switch_mode_lite($all_int);
  82. }
  83. function restore_router_mode_lite() {
  84. find_all_int($all_int);
  85. $tmp = shell_exec("ifconfig br0 up");
  86. startup_all_int($all_int);
  87. set_switch_mode_lite($all_int);
  88. set_router_mode_lite($all_int);
  89. }
  90. function set_switch_mode_lite($all_int) {
  91. foreach ($all_int as $int) {
  92. $tmp = shell_exec("sudo /usr/sbin/brctl addif br0 $int");
  93. }
  94. }
  95. function set_router_mode_lite($all_int) {
  96. foreach ($all_int as $int) {
  97. $tmp = shell_exec("sudo /usr/sbin/brctl delif br0 $int");
  98. }
  99. $tmp = shell_exec("sudo echo 1 > /proc/sys/net/ipv4/ip_forward");
  100. }
  101. function startup_all_int($all_int) {
  102. foreach ($all_int as $startint) {
  103. $tmp = shell_exec("sudo ifconfig $startint up");
  104. }
  105. }
  106. function reset_magnolia() {
  107. find_all_int($all_int);
  108. foreach ($all_int as $int) {
  109. $tmp = shell_exec("sudo /sbin/tc qdisc del dev $int root");
  110. }
  111. }
  112. function check_speed($emurointconfig, &$ethspeednum) {
  113. $ethspeedinfo = shell_exec("sudo ethtool $emurointconfig | grep -i speed");
  114. if (isset($ethspeedmatch)) { unset($ethspeedmatch); }
  115. preg_match_all('/Speed: (.*)Mb\/s/i', $ethspeedinfo, $ethspeedmatch);
  116. if (isset($ethspeedmatch[1][0])) {
  117. $ethspeed = $ethspeedmatch[1][0];
  118. $ethspeednum = ((int)$ethspeed) * 1000;
  119. } else {
  120. $ethspeednum = 100000;
  121. }
  122. }
  123. function exec_emuroint_cmd($postemurointprint, $emurointconfig, $ethspeednum, $x) {
  124. $name = $postemurointprint[$emurointconfig]['name'];
  125. $bandwidth = $postemurointprint[$emurointconfig]['bandwidth'];
  126. $delay = $postemurointprint[$emurointconfig]['delay'];
  127. $jitter = $postemurointprint[$emurointconfig]['jitter'];
  128. $delaycorr = $postemurointprint[$emurointconfig]['delaycorr'];
  129. $delaydist = $postemurointprint[$emurointconfig]['delaydist'];
  130. $packetloss = $postemurointprint[$emurointconfig]['packetloss'];
  131. $packetlosscorr = $postemurointprint[$emurointconfig]['packetlosscorr'];
  132. $packetdup = $postemurointprint[$emurointconfig]['packetdup'];
  133. $packetdupcorr = $postemurointprint[$emurointconfig]['packetdupcorr'];
  134. $packetreord = $postemurointprint[$emurointconfig]['packetreord'];
  135. $packetreordcorr = $postemurointprint[$emurointconfig]['packetreordcorr'];
  136. $packetreordgap = $postemurointprint[$emurointconfig]['packetreordgap'];
  137. $sourceipsubnet = $postemurointprint[$emurointconfig]['sourceipsubnet'];
  138. $destipsubnet = $postemurointprint[$emurointconfig]['destipsubnet'];
  139. $portnum = $postemurointprint[$emurointconfig]['portnum'];
  140. $emulationtag = FALSE;
  141. if ($bandwidth!="" && ($delay!="" || $packetloss!="" || $packetdup!="" || $packetreord!="" || $packetreordgap!="")) {
  142. $emulationtag = TRUE;
  143. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent root handle 1:0 htb default 11";
  144. $final_cmd[] = $cmd;
  145. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:11 htb rate ".(strval($ethspeednum/10))."kbit";
  146. $final_cmd[] = $cmd;
  147. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:11 handle 11:0 sfq perturb 10";
  148. $final_cmd[] = $cmd;
  149. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:".$x." htb rate ".$bandwidth."kbit";
  150. $final_cmd[] = $cmd;
  151. $netem_cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:".$x." handle ".$x.":0 netem";
  152. if ($delay!="") {
  153. $netem_cmd = $netem_cmd . " " . "delay " . $delay . "ms";
  154. if ($jitter!="") {
  155. $netem_cmd = $netem_cmd . " " . $jitter . "ms";
  156. }
  157. if ($delaycorr!="") {
  158. $netem_cmd = $netem_cmd . " " . $delaycorr . "%";
  159. }
  160. if ($delaydist!="") {
  161. $netem_cmd = $netem_cmd . " " . "distribution " . $delaydist;
  162. }
  163. if ($packetreord!="") {
  164. $netem_cmd = $netem_cmd . " " . "reorder " . $packetreord . "%";
  165. if ($packetreordcorr!="") {
  166. $netem_cmd = $netem_cmd . " " . $packetreordcorr . "%";
  167. }
  168. }
  169. if ($packetreord=="" && $packetreordgap!="") {
  170. $netem_cmd = $netem_cmd . " " . "gap " . $packetreordgap;
  171. }
  172. }
  173. if ($packetloss!="") {
  174. $netem_cmd = $netem_cmd . " " . "loss " . $packetloss . "%";
  175. if ($packetlosscorr!="") {
  176. $netem_cmd = $netem_cmd . " " . $packetlosscorr . "%";
  177. }
  178. }
  179. if ($packetdup!="") {
  180. $netem_cmd = $netem_cmd . " " . "duplicate " . $packetdup . "%";
  181. if ($packetdupcorr!="") {
  182. $netem_cmd = $netem_cmd . " " . $packetdupcorr . "%";
  183. }
  184. }
  185. $final_cmd[] = $netem_cmd;
  186. } else if ($bandwidth!="" && ($delay=="" && $packetloss=="" && $packetdup=="" && $packetreord=="" && $packetreordgap=="")) {
  187. $emulationtag = TRUE;
  188. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent root handle 1:0 htb default 11";
  189. $final_cmd[] = $cmd;
  190. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:11 htb rate ".(strval($ethspeednum/10))."kbit";
  191. $final_cmd[] = $cmd;
  192. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:11 handle 11:0 sfq perturb 10";
  193. $final_cmd[] = $cmd;
  194. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:".$x." htb rate ".$bandwidth."kbit";
  195. $final_cmd[] = $cmd;
  196. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:".$x." handle ".$x.":0 sfq perturb 10";
  197. $final_cmd[] = $cmd;
  198. } else if ($bandwidth=="" && ($delay!="" || $packetloss!="" || $packetdup!="" || $packetreord!="" || $packetreordgap!="")) {
  199. $emulationtag = TRUE;
  200. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent root handle 1:0 htb default 11";
  201. $final_cmd[] = $cmd;
  202. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:11 htb rate ".(strval($ethspeednum/10))."kbit";
  203. $final_cmd[] = $cmd;
  204. $cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:11 handle 11:0 sfq perturb 10";
  205. $final_cmd[] = $cmd;
  206. $cmd = "sudo /sbin/tc class add dev $emurointconfig parent 1:0 classid 1:".$x." htb rate ".(strval($ethspeednum/2))."kbit";
  207. $final_cmd[] = $cmd;
  208. $netem_cmd = "sudo /sbin/tc qdisc add dev $emurointconfig parent 1:".$x." handle ".$x.":0 netem";
  209. if ($delay!="") {
  210. $netem_cmd = $netem_cmd . " " . "delay " . $delay . "ms";
  211. if ($jitter!="") {
  212. $netem_cmd = $netem_cmd . " " . $jitter . "ms";
  213. }
  214. if ($delaycorr!="") {
  215. $netem_cmd = $netem_cmd . " " . $delaycorr . "%";
  216. }
  217. if ($delaydist!="") {
  218. $netem_cmd = $netem_cmd . " " . "distribution " . $delaydist;
  219. }
  220. if ($packetreord!="") {
  221. $netem_cmd = $netem_cmd . " " . "reorder " . $packetreord . "%";
  222. if ($packetreordcorr!="") {
  223. $netem_cmd = $netem_cmd . " " . $packetreordcorr . "%";
  224. }
  225. }
  226. if ($packetreord=="" && $packetreordgap!="") {
  227. $netem_cmd = $netem_cmd . " " . "gap " . $packetreordgap;
  228. }
  229. }
  230. if ($packetloss!="") {
  231. $netem_cmd = $netem_cmd . " " . "loss " . $packetloss . "%";
  232. if ($packetlosscorr!="") {
  233. $netem_cmd = $netem_cmd . " " . $packetlosscorr . "%";
  234. }
  235. }
  236. if ($packetdup!="") {
  237. $netem_cmd = $netem_cmd . " " . "duplicate " . $packetdup . "%";
  238. if ($packetdupcorr!="") {
  239. $netem_cmd = $netem_cmd . " " . $packetdupcorr . "%";
  240. }
  241. }
  242. $final_cmd[] = $netem_cmd;
  243. } else {
  244. }
  245. if ($emulationtag == TRUE) {
  246. $filter_cmd = "sudo /sbin/tc filter add dev $emurointconfig protocol ip parent 1:0 prio 1 u32";
  247. if ($sourceipsubnet!="") {
  248. $filter_cmd = $filter_cmd . " " . "match ip src $sourceipsubnet";
  249. } else {
  250. $filter_cmd = $filter_cmd . " " . "match ip src 0.0.0.0/0";
  251. }
  252. if ($destipsubnet!="") {
  253. $filter_cmd = $filter_cmd . " " . "match ip dst $destipsubnet";
  254. } else {
  255. $filter_cmd = $filter_cmd . " " . "match ip dst 0.0.0.0/0";
  256. }
  257. if ($portnum!="") {
  258. $filter_cmd = $filter_cmd . " " . "match ip dport $portnum 0xffff";
  259. }
  260. $filter_cmd = $filter_cmd . " " . "flowid 1:" . $x;
  261. $final_cmd[] = $filter_cmd;
  262. }
  263. if ($emulationtag == TRUE) {
  264. foreach ($final_cmd as $magcommand) {
  265. $tmp = shell_exec($magcommand);
  266. }
  267. }
  268. }
  269. function get_emurovalue(&$emurointprint, $emurointconfig, $y) {
  270. $emurointprint[$emurointconfig]['name'] = $emurointconfig;
  271. $tcqdiscinfo = shell_exec("tc qdisc show dev $emurointconfig");
  272. $emutag = FALSE;
  273. if (strstr($tcqdiscinfo, 'qdisc htb 1: root')!=FALSE) {
  274. $tcclassinfo = shell_exec("tc class show dev $emurointconfig");
  275. if (strstr($tcclassinfo, ("class htb 1:".$y." root leaf ".$y.":"))!=FALSE) {
  276. preg_match_all(("/class htb 1:".$y."(.*) rate (.*)bit ceil/U"), $tcclassinfo, $tcclassmatch);
  277. if (isset($tcclassmatch[2][0])) {
  278. $emutag = TRUE;
  279. $tcclassmatchtext = $tcclassmatch[2][0];
  280. if (strstr($tcclassmatchtext, 'K')!=FALSE) {
  281. $speednumtxt = strstr($tcclassmatchtext, 'K', true);
  282. } else if (strstr($tcclassmatchtext, 'M')!=FALSE) {
  283. $tmpspeednumtxt = strstr($tcclassmatchtext, 'M', true);
  284. $tmpspeednum = (int)$tmpspeednumtxt;
  285. $tmpspeednumtxt = $tmpspeednum * 1000;
  286. $speednumtxt = strval($tmpspeednumtxt);
  287. } else {
  288. $tmpspeednum=(int)$tcclassmatchtext;
  289. $tmpspeednumtxt=$tmpspeednum/1000;
  290. $speednumtxt=strval($tmpspeednumtxt);
  291. }
  292. $emurointprint[$emurointconfig]['bandwidth'] = $speednumtxt;
  293. } else {
  294. $emurointprint[$emurointconfig]['bandwidth'] = "";
  295. }
  296. } else {
  297. $emurointprint[$emurointconfig]['bandwidth'] = "";
  298. }
  299. if (strstr($tcqdiscinfo, ("netem ".$y.": parent 1:".$y))!=FALSE) {
  300. $emutag = TRUE;
  301. $tcqdiscinfonetem = strstr($tcqdiscinfo, ("netem ".$y.": parent 1:".$y));
  302. preg_match_all(("/delay\s(([\d][\w\.%]*\s+)*)/"), $tcqdiscinfonetem, $tcqdiscmatchdelay);
  303. if (isset($tcqdiscmatchdelay[1][0])) {
  304. $tcqdiscmatchdelaytrim = trim($tcqdiscmatchdelay[1][0]);
  305. $tcqdiscmatchdelaytrim = preg_replace("/\s{2,}/", " ", $tcqdiscmatchdelaytrim);
  306. $tcqdiscmatchdelayarray = explode(" ", $tcqdiscmatchdelaytrim);
  307. if (isset($tcqdiscmatchdelayarray[0])) {
  308. if ((strstr($tcqdiscmatchdelayarray[0], 's')!=FALSE) && (strstr($tcqdiscmatchdelayarray[0], 'ms')==FALSE) && (strstr($tcqdiscmatchdelayarray[0], 'us')==FALSE)) {
  309. $tmpdelaynumtxt = strstr($tcqdiscmatchdelayarray[0], 's', true);
  310. $tmpdelaynum = (int)$tmpdelaynumtxt;
  311. $tmpdelaynumtxt = $tmpdelaynum * 1000;
  312. $delaynumtxt = strval($tmpdelaynumtxt);
  313. } elseif (strstr($tcqdiscmatchdelayarray[0], 'ms')!=FALSE) {
  314. $delaynumtxt = strstr($tcqdiscmatchdelayarray[0], 'ms', true);
  315. } else {
  316. $delaynumtxt = "";
  317. }
  318. $emurointprint[$emurointconfig]['delay'] = $delaynumtxt;
  319. } else {
  320. $emurointprint[$emurointconfig]['delay'] = "";
  321. }
  322. if (isset($tcqdiscmatchdelayarray[1])) {
  323. if ((strstr($tcqdiscmatchdelayarray[1], 's')!=FALSE) && (strstr($tcqdiscmatchdelayarray[1], 'ms')==FALSE) && (strstr($tcqdiscmatchdelayarray[1], 'us')==FALSE)) {
  324. $tmpjitternumtxt = strstr($tcqdiscmatchdelayarray[1], 's', true);
  325. $tmpjitternum = (int)$tmpjitternumtxt;
  326. $tmpjitternumtxt = $tmpjitternum * 1000;
  327. $jitternumtxt = strval($tmpjitternumtxt);
  328. } elseif (strstr($tcqdiscmatchdelayarray[1], 'ms')!=FALSE) {
  329. $jitternumtxt = strstr($tcqdiscmatchdelayarray[1], 'ms', true);
  330. } else {
  331. $jitternumtxt = "";
  332. }
  333. $emurointprint[$emurointconfig]['jitter'] = $jitternumtxt;
  334. } else {
  335. $emurointprint[$emurointconfig]['jitter'] = "";
  336. }
  337. if (isset($tcqdiscmatchdelayarray[2])) {
  338. $emurointprint[$emurointconfig]['delaycorr'] = strstr($tcqdiscmatchdelayarray[2], '%', true);
  339. } else {
  340. $emurointprint[$emurointconfig]['delaycorr'] = "";
  341. }
  342. } else {
  343. $emurointprint[$emurointconfig]['delay'] = "";
  344. $emurointprint[$emurointconfig]['jitter'] = "";
  345. $emurointprint[$emurointconfig]['delaycorr'] = "";
  346. }
  347. $emurointprint[$emurointconfig]['delaydist'] = "";
  348. preg_match_all(("/loss\s(([\d][\w\.%]*\s+)*)/"), $tcqdiscinfonetem, $tcqdiscmatchloss);
  349. if (isset($tcqdiscmatchloss[1][0])) {
  350. $tcqdiscmatchlosstrim = trim($tcqdiscmatchloss[1][0]);
  351. $tcqdiscmatchlosstrim = preg_replace("/\s{2,}/", " ", $tcqdiscmatchlosstrim);
  352. $tcqdiscmatchlossarray = explode(" ", $tcqdiscmatchlosstrim);
  353. if (isset($tcqdiscmatchlossarray[0])) {
  354. $emurointprint[$emurointconfig]['packetloss'] = strstr($tcqdiscmatchlossarray[0], '%', true);
  355. } else {
  356. $emurointprint[$emurointconfig]['packetloss'] = "";
  357. }
  358. if (isset($tcqdiscmatchlossarray[1])) {
  359. $emurointprint[$emurointconfig]['packetlosscorr'] = strstr($tcqdiscmatchlossarray[1], '%', true);
  360. } else {
  361. $emurointprint[$emurointconfig]['packetlosscorr'] = "";
  362. }
  363. } else {
  364. $emurointprint[$emurointconfig]['packetloss'] = "";
  365. $emurointprint[$emurointconfig]['packetlosscorr'] = "";
  366. }
  367. preg_match_all(("/duplicate\s(([\d][\w\.%]*\s+)*)/"), $tcqdiscinfonetem, $tcqdiscmatchduplicate);
  368. if (isset($tcqdiscmatchduplicate[1][0])) {
  369. $tcqdiscmatchduplicatetrim = trim($tcqdiscmatchduplicate[1][0]);
  370. $tcqdiscmatchduplicatetrim = preg_replace("/\s{2,}/", " ", $tcqdiscmatchduplicatetrim);
  371. $tcqdiscmatchduplicatearray = explode(" ", $tcqdiscmatchduplicatetrim);
  372. if (isset($tcqdiscmatchduplicatearray[0])) {
  373. $emurointprint[$emurointconfig]['packetdup'] = strstr($tcqdiscmatchduplicatearray[0], '%', true);
  374. } else {
  375. $emurointprint[$emurointconfig]['packetdup'] = "";
  376. }
  377. if (isset($tcqdiscmatchduplicatearray[1])) {
  378. $emurointprint[$emurointconfig]['packetdupcorr'] = strstr($tcqdiscmatchduplicatearray[1], '%', true);
  379. } else {
  380. $emurointprint[$emurointconfig]['packetdupcorr'] = "";
  381. }
  382. } else {
  383. $emurointprint[$emurointconfig]['packetdup'] = "";
  384. $emurointprint[$emurointconfig]['packetdupcorr'] = "";
  385. }
  386. preg_match_all(("/reorder\s(([\d][\w\.%]*\s+)*)/"), $tcqdiscinfonetem, $tcqdiscmatchreord);
  387. if (isset($tcqdiscmatchreord[1][0])) {
  388. $tcqdiscmatchreordtrim = trim($tcqdiscmatchreord[1][0]);
  389. $tcqdiscmatchreordtrim = preg_replace("/\s{2,}/", " ", $tcqdiscmatchreordtrim);
  390. $tcqdiscmatchreordarray = explode(" ", $tcqdiscmatchreordtrim);
  391. if (isset($tcqdiscmatchreordarray[0])) {
  392. $emurointprint[$emurointconfig]['packetreord'] = strstr($tcqdiscmatchreordarray[0], '%', true);
  393. } else {
  394. $emurointprint[$emurointconfig]['packetreord'] = "";
  395. }
  396. if (isset($tcqdiscmatchreordarray[1])) {
  397. $emurointprint[$emurointconfig]['packetreordcorr'] = strstr($tcqdiscmatchreordarray[1], '%', true);
  398. } else {
  399. $emurointprint[$emurointconfig]['packetreordcorr'] = "";
  400. }
  401. } else {
  402. $emurointprint[$emurointconfig]['packetreord'] = "";
  403. $emurointprint[$emurointconfig]['packetreordcorr'] = "";
  404. }
  405. preg_match_all(("/gap\s(([\d][\w\.%]*\s+)*)/"), $tcqdiscinfonetem, $tcqdiscmatchgap);
  406. if (isset($tcqdiscmatchgap[1][0])) {
  407. $tcqdiscmatchgaptrim = trim($tcqdiscmatchgap[1][0]);
  408. $tcqdiscmatchgaptrim = preg_replace("/\s{2,}/", " ", $tcqdiscmatchgaptrim);
  409. if (isset($tcqdiscmatchgaptrim)) {
  410. $emurointprint[$emurointconfig]['packetreordgap'] = $tcqdiscmatchgaptrim;
  411. } else {
  412. $emurointprint[$emurointconfig]['packetreordgap'] = "";
  413. }
  414. } else {
  415. $emurointprint[$emurointconfig]['packetreordgap'] = "";
  416. }
  417. } else {
  418. $emurointprint[$emurointconfig]['delay'] = "";
  419. $emurointprint[$emurointconfig]['jitter'] = "";
  420. $emurointprint[$emurointconfig]['delaycorr'] = "";
  421. $emurointprint[$emurointconfig]['delaydist'] = "";
  422. $emurointprint[$emurointconfig]['packetloss'] = "";
  423. $emurointprint[$emurointconfig]['packetlosscorr'] = "";
  424. $emurointprint[$emurointconfig]['packetdup'] = "";
  425. $emurointprint[$emurointconfig]['packetdupcorr'] = "";
  426. $emurointprint[$emurointconfig]['packetreord'] = "";
  427. $emurointprint[$emurointconfig]['packetreordcorr'] = "";
  428. $emurointprint[$emurointconfig]['packetreordgap'] = "";
  429. }
  430. if ($emutag == TRUE) {
  431. $ip_calc = new Net_IPv4();
  432. $tcfilterinfo = shell_exec("tc filter show dev $emurointconfig");
  433. if (strstr($tcfilterinfo, ("flowid 1:".$y))!=FALSE) {
  434. $tcfilterinfoflowid = strstr($tcfilterinfo, ("flowid 1:".$y));
  435. preg_match_all(("/match\s(\w*)\/(\w*)\sat\s12/U"), $tcfilterinfoflowid, $tcfilterinfomatchsrcip);
  436. preg_match_all(("/match\s(\w*)\/(\w*)\sat\s16/U"), $tcfilterinfoflowid, $tcfilterinfomatchdstip);
  437. preg_match_all(("/match\s(\w*)\/(\w*)\sat\s20/U"), $tcfilterinfoflowid, $tcfilterinfomatchdport);
  438. if (isset($tcfilterinfomatchsrcip[1][0]) && isset($tcfilterinfomatchsrcip[2][0])) {
  439. $tcfiltersrciptrim = trim($tcfilterinfomatchsrcip[1][0]);
  440. $tcfiltersrcsubnettrim = trim($tcfilterinfomatchsrcip[2][0]);
  441. $tcfiltersrcip = $ip_calc->htoa($tcfiltersrciptrim);
  442. $tcfiltersrcsubnetmask = $ip_calc->htoa($tcfiltersrcsubnettrim);
  443. $tcfiltersrcsubnetCIDR = mask2cidr($tcfiltersrcsubnetmask);
  444. $emurointprint[$emurointconfig]['sourceip'] = $tcfiltersrcip;
  445. $emurointprint[$emurointconfig]['sourceCIDR'] = $tcfiltersrcsubnetCIDR;
  446. } else {
  447. $emurointprint[$emurointconfig]['sourceip'] = "";
  448. $emurointprint[$emurointconfig]['sourceCIDR'] = "";
  449. }
  450. if (isset($tcfilterinfomatchdstip[1][0]) && isset($tcfilterinfomatchdstip[2][0])) {
  451. $tcfilterdstiptrim = trim($tcfilterinfomatchdstip[1][0]);
  452. $tcfilterdstsubnettrim = trim($tcfilterinfomatchdstip[2][0]);
  453. $tcfilterdstip = $ip_calc->htoa($tcfilterdstiptrim);
  454. $tcfilterdstsubnetmask = $ip_calc->htoa($tcfilterdstsubnettrim);
  455. $tcfilterdstsubnetCIDR = mask2cidr($tcfilterdstsubnetmask);
  456. $emurointprint[$emurointconfig]['destip'] = $tcfilterdstip;
  457. $emurointprint[$emurointconfig]['destCIDR'] = $tcfilterdstsubnetCIDR;
  458. } else {
  459. $emurointprint[$emurointconfig]['destip'] = "";
  460. $emurointprint[$emurointconfig]['destCIDR'] = "";
  461. }
  462. if (isset($tcfilterinfomatchdport[1][0])) {
  463. $tcfilterdporttrim = trim($tcfilterinfomatchdport[1][0]);
  464. $tcfilterdport = hexdec($tcfilterdporttrim);
  465. $emurointprint[$emurointconfig]['portnum'] = strval($tcfilterdport);
  466. } else {
  467. $emurointprint[$emurointconfig]['portnum'] = "";
  468. }
  469. } else {
  470. $emurointprint[$emurointconfig]['sourceip'] = "";
  471. $emurointprint[$emurointconfig]['sourceCIDR'] = "";
  472. $emurointprint[$emurointconfig]['destip'] = "";
  473. $emurointprint[$emurointconfig]['destCIDR'] = "";
  474. $emurointprint[$emurointconfig]['portnum'] = "";
  475. }
  476. } else {
  477. $emurointprint[$emurointconfig]['sourceip'] = "";
  478. $emurointprint[$emurointconfig]['sourceCIDR'] = "";
  479. $emurointprint[$emurointconfig]['destip'] = "";
  480. $emurointprint[$emurointconfig]['destCIDR'] = "";
  481. $emurointprint[$emurointconfig]['portnum'] = "";
  482. }
  483. } else {
  484. $emurointprint[$emurointconfig]['bandwidth'] = "";
  485. $emurointprint[$emurointconfig]['delay'] = "";
  486. $emurointprint[$emurointconfig]['jitter'] = "";
  487. $emurointprint[$emurointconfig]['delaycorr'] = "";
  488. $emurointprint[$emurointconfig]['delaydist'] = "";
  489. $emurointprint[$emurointconfig]['packetloss'] = "";
  490. $emurointprint[$emurointconfig]['packetlosscorr'] = "";
  491. $emurointprint[$emurointconfig]['packetdup'] = "";
  492. $emurointprint[$emurointconfig]['packetdupcorr'] = "";
  493. $emurointprint[$emurointconfig]['packetreord'] = "";
  494. $emurointprint[$emurointconfig]['packetreordcorr'] = "";
  495. $emurointprint[$emurointconfig]['packetreordgap'] = "";
  496. $emurointprint[$emurointconfig]['sourceip'] = "";
  497. $emurointprint[$emurointconfig]['sourceCIDR'] = "";
  498. $emurointprint[$emurointconfig]['destip'] = "";
  499. $emurointprint[$emurointconfig]['destCIDR'] = "";
  500. $emurointprint[$emurointconfig]['portnum'] = "";
  501. }
  502. }
  503. function mask2cidr($mask) {
  504. if ($mask == "0.0.0.0") {
  505. return 0;
  506. } else {
  507. $long = ip2long($mask);
  508. $base = ip2long('255.255.255.255');
  509. return 32-log(($long ^ $base)+1,2);
  510. }
  511. }
  512. function validate_input(&$postemurointprint, $emurointconfig) {
  513. $postemurointprint[$emurointconfig]['bandwidth'] = trim($postemurointprint[$emurointconfig]['bandwidth']);
  514. $postemurointprint[$emurointconfig]['delay'] = trim($postemurointprint[$emurointconfig]['delay']);
  515. $postemurointprint[$emurointconfig]['jitter'] = trim($postemurointprint[$emurointconfig]['jitter']);
  516. $postemurointprint[$emurointconfig]['delaycorr'] = trim($postemurointprint[$emurointconfig]['delaycorr']);
  517. $postemurointprint[$emurointconfig]['delaydist'] = trim($postemurointprint[$emurointconfig]['delaydist']);
  518. $postemurointprint[$emurointconfig]['packetloss'] = trim($postemurointprint[$emurointconfig]['packetloss']);
  519. $postemurointprint[$emurointconfig]['packetlosscorr'] = trim($postemurointprint[$emurointconfig]['packetlosscorr']);
  520. $postemurointprint[$emurointconfig]['packetdup'] = trim($postemurointprint[$emurointconfig]['packetdup']);
  521. $postemurointprint[$emurointconfig]['packetdupcorr'] = trim($postemurointprint[$emurointconfig]['packetdupcorr']);
  522. $postemurointprint[$emurointconfig]['packetreord'] = trim($postemurointprint[$emurointconfig]['packetreord']);
  523. $postemurointprint[$emurointconfig]['packetreordcorr'] = trim($postemurointprint[$emurointconfig]['packetreordcorr']);
  524. $postemurointprint[$emurointconfig]['packetreordgap'] = trim($postemurointprint[$emurointconfig]['packetreordgap']);
  525. $postemurointprint[$emurointconfig]['sourceip'] = trim($postemurointprint[$emurointconfig]['sourceip']);
  526. $postemurointprint[$emurointconfig]['sourcesubnet'] = trim($postemurointprint[$emurointconfig]['sourcesubnet']);
  527. $postemurointprint[$emurointconfig]['destip'] = trim($postemurointprint[$emurointconfig]['destip']);
  528. $postemurointprint[$emurointconfig]['destsubnet'] = trim($postemurointprint[$emurointconfig]['destsubnet']);
  529. $postemurointprint[$emurointconfig]['sourceipsubnet'] = $postemurointprint[$emurointconfig]['sourceip'] . "/" . $postemurointprint[$emurointconfig]['sourcesubnet'];
  530. $postemurointprint[$emurointconfig]['destipsubnet'] = $postemurointprint[$emurointconfig]['destip'] . "/" . $postemurointprint[$emurointconfig]['destsubnet'];
  531. $postemurointprint[$emurointconfig]['portnum'] = trim($postemurointprint[$emurointconfig]['portnum']);
  532. if (!preg_match("/^((\d){1,6})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['bandwidth'])) {
  533. $postemurointprint[$emurointconfig]['bandwidth'] = "";
  534. }
  535. if (!preg_match("/^((\d){1,6})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['delay'])) {
  536. $postemurointprint[$emurointconfig]['delay'] = "";
  537. }
  538. if (!preg_match("/^((\d){1,6})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['jitter'])) {
  539. $postemurointprint[$emurointconfig]['jitter'] = "";
  540. }
  541. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['delaycorr'])) {
  542. $postemurointprint[$emurointconfig]['delaycorr'] = "";
  543. }
  544. if (!preg_match("/^[a-z]{1,12}$/", $postemurointprint[$emurointconfig]['delaydist'])) {
  545. $postemurointprint[$emurointconfig]['delaydist'] = "";
  546. }
  547. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetloss'])) {
  548. $postemurointprint[$emurointconfig]['packetloss'] = "";
  549. }
  550. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetlosscorr'])) {
  551. $postemurointprint[$emurointconfig]['packetlosscorr'] = "";
  552. }
  553. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetdup'])) {
  554. $postemurointprint[$emurointconfig]['packetdup'] = "";
  555. }
  556. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetdupcorr'])) {
  557. $postemurointprint[$emurointconfig]['packetdupcorr'] = "";
  558. }
  559. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetreord'])) {
  560. $postemurointprint[$emurointconfig]['packetreord'] = "";
  561. }
  562. if (!preg_match("/^((\d){1,3})(\.[\d]{1,2})?$/", $postemurointprint[$emurointconfig]['packetreordcorr'])) {
  563. $postemurointprint[$emurointconfig]['packetreordcorr'] = "";
  564. }
  565. if (!preg_match("/^(\d){1,3}$/", $postemurointprint[$emurointconfig]['packetreordgap'])) {
  566. $postemurointprint[$emurointconfig]['packetreordgap'] = "";
  567. }
  568. if (!preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/([1-2]?[0-9]|3[0-2])$/", $postemurointprint[$emurointconfig]['sourceipsubnet'])) {
  569. $postemurointprint[$emurointconfig]['sourceipsubnet'] = "";
  570. }
  571. if (!preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/([1-2]?[0-9]|3[0-2])$/", $postemurointprint[$emurointconfig]['destipsubnet'])) {
  572. $postemurointprint[$emurointconfig]['destipsubnet'] = "";
  573. }
  574. if (!preg_match("/^(\d){1,5}$/", $postemurointprint[$emurointconfig]['portnum'])) {
  575. $postemurointprint[$emurointconfig]['portnum'] = "";
  576. }
  577. }
  578. function validate_intconfig(&$postrointprint, $rointconfig) {
  579. $postrointprint[$rointconfig]['ipaddressvalue'] = trim($postrointprint[$rointconfig]['ipaddressvalue']);
  580. $postrointprint[$rointconfig]['ipsubnetvalue'] = trim($postrointprint[$rointconfig]['ipsubnetvalue']);
  581. if (!preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $postrointprint[$rointconfig]['ipaddressvalue'])) {
  582. $postrointprint[$rointconfig]['ipaddressvalue'] = "";
  583. }
  584. if (!preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $postrointprint[$rointconfig]['ipsubnetvalue'])) {
  585. $postrointprint[$rointconfig]['ipsubnetvalue'] = "";
  586. }
  587. }
  588. ?>