@@ -589,9 +589,156 @@ void Chain::delRuleList() {
589589}
590590
591591ChainInsertOutputJsonObject Chain::insert (ChainInsertInputJsonObject input) {
592- throw std::runtime_error (" Chain::ChainInsertOutput: Method not implemented" );
592+ ChainRuleJsonObject conf;
593+
594+ if (input.conntrackIsSet ()) {
595+ conf.setConntrack (input.getConntrack ());
596+ }
597+ if (input.srcIsSet ()) {
598+ conf.setSrc (input.getSrc ());
599+ }
600+ if (input.dstIsSet ()) {
601+ conf.setDst (input.getDst ());
602+ }
603+ if (input.sportIsSet ()) {
604+ conf.setSport (input.getSport ());
605+ }
606+ if (input.dportIsSet ()) {
607+ conf.setDport (input.getDport ());
608+ }
609+ if (input.tcpflagsIsSet ()) {
610+ conf.setTcpflags (input.getTcpflags ());
611+ }
612+ if (input.l4protoIsSet ()) {
613+ conf.setL4proto (input.getL4proto ());
614+ }
615+ if (input.actionIsSet ()) {
616+ conf.setAction (input.getAction ());
617+ } else {
618+ conf.setAction (ActionEnum::DROP);
619+ }
620+
621+ uint32_t id = 0 ;
622+ if (input.idIsSet ()) {
623+ id = input.getId ();
624+ }
625+
626+ if (id > rules_.size ()) {
627+ throw std::runtime_error (" id not allowed" );
628+ }
629+
630+ auto newRule = std::make_shared<ChainRule>(*this , conf);
631+
632+ ChainStatsJsonObject confStats;
633+ auto newStats = std::make_shared<ChainStats>(*this , confStats);
634+
635+ getStatsList ();
636+
637+ if (newRule == nullptr ) {
638+ // Totally useless, but it is needed to avoid the compiler making wrong
639+ // assumptions and reordering
640+ throw new std::runtime_error (" I won't be thrown" );
641+
642+ } else if (rules_.size () >= id && newRule != nullptr ) {
643+ rules_.resize (rules_.size () + 1 );
644+ counters_.resize (counters_.size () + 1 );
645+ }
646+
647+ // 0, 1, 2, 3
648+ // insert @2
649+ // 0, 1, 2*, 2->3, 3->4
650+
651+ // for rules before id
652+ // nothing
653+
654+ // for rules starting from id to rules size-1
655+ // move ahead i -> i+i
656+ // btw, better to start from the end of the array
657+ // for rules starting from size-1 to id
658+ // move ahead i -> i+i
659+
660+ int i = 0 ;
661+ int id_int = (int )id;
662+
663+ // ids are 0,1,2
664+ // size=3
665+ // id = 1 (insert)
666+
667+ // new size = 4
668+
669+ // from 1 to 2
670+ // move 2->3
671+ // move 1->2,
672+ // replace 1
673+
674+ for (i = rules_.size () - 2 ; i >= id_int; i--) {
675+ rules_[i + 1 ] = rules_[i];
676+ counters_[i + 1 ] = counters_[i];
677+ if (rules_[i + 1 ] != nullptr ) {
678+ rules_[i + 1 ]->id = i + 1 ;
679+ }
680+ if (counters_[i + 1 ] != nullptr ) {
681+ counters_[i + 1 ]->counter .setId (i + 1 );
682+ }
683+ }
684+
685+ rules_[id] = newRule;
686+ rules_[id]->id = id;
687+
688+ counters_[id] = newStats;
689+ counters_[id]->counter .setPkts (0 );
690+ counters_[id]->counter .setBytes (0 );
691+ counters_[id]->counter .setId (id);
692+
693+ if (parent_.interactive_ ) {
694+ updateChain ();
695+ }
696+
697+ // set fields for return object
698+ ChainInsertOutputJsonObject result;
699+ result.setId (id);
700+
701+ return result;
593702}
594703
595704void Chain::deletes (ChainDeleteInputJsonObject input) {
596- throw std::runtime_error (" Chain::: Method not implemented" );
705+ ChainRuleJsonObject conf;
706+
707+ if (input.conntrackIsSet ()) {
708+ conf.setConntrack (input.getConntrack ());
709+ }
710+ if (input.srcIsSet ()) {
711+ conf.setSrc (input.getSrc ());
712+ }
713+ if (input.dstIsSet ()) {
714+ conf.setDst (input.getDst ());
715+ }
716+ if (input.sportIsSet ()) {
717+ conf.setSport (input.getSport ());
718+ }
719+ if (input.dportIsSet ()) {
720+ conf.setDport (input.getDport ());
721+ }
722+ if (input.tcpflagsIsSet ()) {
723+ conf.setTcpflags (input.getTcpflags ());
724+ }
725+ if (input.l4protoIsSet ()) {
726+ conf.setL4proto (input.getL4proto ());
727+ }
728+ if (input.actionIsSet ()) {
729+ conf.setAction (input.getAction ());
730+ } else {
731+ conf.setAction (ActionEnum::DROP);
732+ }
733+
734+ for (int i = 0 ; i < rules_.size (); i++) {
735+ if (rules_[i] != nullptr ) {
736+ ChainRule c (*this , conf);
737+ if (rules_[i]->equal (c)) {
738+ delRule (i);
739+ return ;
740+ }
741+ }
742+ }
743+ throw std::runtime_error (" no matching rule to delete" );
597744}
0 commit comments