MGPT 2007 question no 1 ...
This is the first version that I made (232 lines)
import ncst.pgdst.*;
class IP{
int octet[],port;
IP(){
octet=new int[4];
}
void input(SimpleInput in)throws IOException{
String str=in.readWord();
int kk=-1;
do{
int k=str.indexOf(".");
if(k==-1){
octet[++kk]=Integer.parseInt(str);
break;
}
octet[++kk]=Integer.parseInt(str.substring(0,k));
str=str.substring(k+1);
}while(str.length() != 0);
port=Integer.parseInt(in.readWord());
}
public String print(){
String str="";
for(int i=0;i<4;i++){
str += octet[i] + ".";
}
str += " "+port;
return str;
}
}
class IPRange{
String octetRange[],portRange;
IPRange(){
octetRange=new String[4];
}
void input(SimpleInput in)throws IOException{
for(int i=0;i
octetRange[i]=in.readWord();
}
portRange=in.readWord();
}
}
class Rule{
IPRange ipRange;
Rule(){
ipRange=new IPRange();
}
void input(SimpleInput in)throws IOException{
ipRange.input(in);
}
}
class RejectRule extends Rule{
}
class AcceptRule extends Rule{
}
class ForwardRule extends Rule{
IP forwardTo;
void input(SimpleInput in)throws IOException{
super.input(in);
forwardTo=new IP();
forwardTo.input(in);
}
}
class Router{
RejectRule[] rejectRule;
AcceptRule[] acceptRule;
ForwardRule[] forwardRule;
int rejectRuleCount,acceptRuleCount,forwardRuleCount;
Router(){
rejectRule=new RejectRule[10];
rejectRuleCount=-1;
acceptRule=new AcceptRule[10];
acceptRuleCount=-1;
forwardRule=new ForwardRule[10];
forwardRuleCount=-1;
}
void inputRules(SimpleInput in)throws IOException{
int n=Integer.parseInt(in.readWord());
for(int i=0;i
String cmd=in.readWord();
if(cmd.equals("REJECT")){
rejectRule[++rejectRuleCount]=new RejectRule();
rejectRule[rejectRuleCount].input(in);
}else if(cmd.equals("ACCEPT")){
acceptRule[++acceptRuleCount]=new AcceptRule();
acceptRule[acceptRuleCount].input(in);
}else if(cmd.equals("FORWARD")){
forwardRule[++forwardRuleCount]=new ForwardRule();
forwardRule[forwardRuleCount].input(in);
}
}
}
void perform(SimpleInput in)throws IOException{
int n=Integer.parseInt(in.readWord());
for(int i=0;i
String ipStr=in.readWord();
IP ip=new IP();
int k=-1,kk=-1;
do{
k=ipStr.indexOf(".");
if(k==-1){
ip.octet[++kk]=Integer.parseInt(ipStr);
break;
}
ip.octet[++kk]=Integer.parseInt(ipStr.substring(0,k));
ipStr=ipStr.substring(k+1);
}while(ipStr.length() != 0);
ip.port=Integer.parseInt(in.readWord());
System.out.println(respond(ip));
}
}
String respond(IP ip){
for(int i=0;i<=rejectRuleCount;i++){
boolean go[]={false,false,false,false,false,true};
for(int j=0;j<4;j++){
String octetRange=rejectRule[i].ipRange.octetRange[j];
if(octetRange.equals("*")){
go[j]=true;
}else if(octetRange.indexOf("-")==-1){
if(Integer.parseInt(octetRange)==ip.octet[j])
go[j]=true;
}else{
int first=Integer.parseInt(octetRange.substring(0,octetRange.indexOf("-")));
int second=Integer.parseInt(octetRange.substring(octetRange.indexOf("-")+1));
if(ip.octet[j]>=first && ip.octet[j]<=second)
go[j]=true;
}
}
String portRange=rejectRule[i].ipRange.portRange;
if(portRange.equals("*")){
go[4]=true;
}else if(portRange.indexOf("-")==-1){
if(Integer.parseInt(portRange)==ip.port)
go[4]=true;
}else{
int first=Integer.parseInt(portRange.substring(0,portRange.indexOf("-")));
int second=Integer.parseInt(portRange.substring(portRange.indexOf("-")+1));
if(ip.port>=first && ip.port<=second)
go[4]=true;
}
for(int j=0;j<5;j++){
if(go[j]==false)
go[5]=false;
}
if(go[5])
return "REJECT";
}
for(int i=0;i<=acceptRuleCount;i++){
boolean go[]={false,false,false,false,false,true};
for(int j=0;j<4;j++){
String octetRange=acceptRule[i].ipRange.octetRange[j];
if(octetRange.equals("*")){
go[j]=true;
}else if(octetRange.indexOf("-")==-1){
if(Integer.parseInt(octetRange)==ip.octet[j])
go[j]=true;
}else{
int first=Integer.parseInt(octetRange.substring(0,octetRange.indexOf("-")));
int second=Integer.parseInt(octetRange.substring(octetRange.indexOf("-")+1));
if(ip.octet[j]>=first && ip.octet[j]<=second)
go[j]=true;
}
}
String portRange=acceptRule[i].ipRange.portRange;
if(portRange.equals("*")){
go[4]=true;
}else if(portRange.indexOf("-")==-1){
if(Integer.parseInt(portRange)==ip.port)
go[4]=true;
}else{
int first=Integer.parseInt(portRange.substring(0,portRange.indexOf("-")));
int second=Integer.parseInt(portRange.substring(portRange.indexOf("-")+1));
if(ip.port>=first && ip.port<=second)
go[4]=true;
}
for(int j=0;j<5;j++){
if(go[j]==false)
go[5]=false;
}
if(go[5])
return "ACCEPT";
}
for(int i=0;i<=forwardRuleCount;i++){
boolean go[]={false,false,false,false,false,true};
for(int j=0;j<4;j++){
String octetRange=forwardRule[i].ipRange.octetRange[j];
if(octetRange.equals("*")){
go[j]=true;
}else if(octetRange.indexOf("-")==-1){
if(Integer.parseInt(octetRange)==ip.octet[j])
go[j]=true;
}else{
int first=Integer.parseInt(octetRange.substring(0,octetRange.indexOf("-")));
int second=Integer.parseInt(octetRange.substring(octetRange.indexOf("-")+1));
if(ip.octet[j]>=first && ip.octet[j]<=second)
go[j]=true;
}
}
String portRange=forwardRule[i].ipRange.portRange;
if(portRange.equals("*")){
go[4]=true;
}else if(portRange.indexOf("-")==-1){
if(Integer.parseInt(portRange)==ip.port)
go[4]=true;
}else{
int first=Integer.parseInt(portRange.substring(0,portRange.indexOf("-")));
int second=Integer.parseInt(portRange.substring(portRange.indexOf("-")+1));
if(ip.port>=first && ip.port<=second)
go[4]=true;
}
for(int j=0;j<5;j++){
if(go[j]==false)
go[5]=false;
}
if(go[5])
return forwardRule[i].forwardTo.print();
}
return "REJECT";
}
public static void main(String[]args)throws IOException{
SimpleInput in=new SimpleInput();
Router router=new Router();
router.inputRules(in);
router.perform(in);
}
}
If you are unable to see the program in a proper format, please click this link
http://chandra-shekhar.spaces.live.com/default.aspx