Commits

Anonymous committed 9f1ee9e

TokenizerによるIP等のパースの実装。例外が生じる。。

  • Participants
  • Parent commits a9d2502

Comments (0)

Files changed (2)

File src/foo/sample/GetIP/ConnectionData.java

+package foo.sample.GetIP;
+
+import android.text.format.Formatter;
+import android.util.Log;
+
+public class ConnectionData {
+	private static final String[] STATES= {"ZERO error","SYN_SENT","SYN_RECV","FIN_EAIT1",
+		"FIN_WAIT2","TIME_WAIT","CLOSE","CLOSE_WAIT","LAST_ACK","LISTEN","CLOSING"};
+	public static final int TCP = 0;
+	public static final int TCP6 = 1;
+	public static final int UDP = 2;
+	public static final int UDP6 = 3;
+	public int connectionType,localIP,localPort,remoteIP,remotePort,state,uid;
+	
+	public ConnectionData() {
+		// TODO Auto-generated constructor stub
+		connectionType = localIP = localPort = remoteIP = remotePort = state = uid = -1;
+	}
+	
+	
+	public String getState()
+	{
+		if(state >= 1 && state <=11)
+		{
+			return STATES[state];
+		}
+		else
+		{
+			return "?";
+		}
+	}
+	public void debugInfo()
+	{
+		Log.d("debug","start:debugInfo()");
+		Log.d("debug","UID:"+uid);
+		Log.d("debug","stat:"+getState());
+		Log.d("debug","LocalIP:"+Formatter.formatIpAddress(localIP)+":"+localPort);
+		Log.d("debug","remoteIP:"+Formatter.formatIpAddress(remoteIP)+":"+remotePort);
+	}
+
+}

File src/foo/sample/GetIP/GetIP.java

 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.List;
+import java.util.StringTokenizer;
 
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManager.RunningAppProcessInfo;
-import android.app.ActivityManager.RunningTaskInfo;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.EditText;
+import android.widget.MultiAutoCompleteTextView.Tokenizer;
 
 public class GetIP extends Activity implements OnClickListener {
     /** Called when the activity is first created. */
 	EditText etNum = null;
 	BufferedReader br = null;
     String outStr = "";
-    private int pid = -1;
+    private int myPid = -1;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     public void onStart()
     {
     	super.onStart();
-    	pid = -1;
+    	myPid = getMyPid();
+    	if(myPid == -1)
+    	{
+    		Log.d("debug","fail to get myPid!!");
+    	}
+    	else
+    	{
+    		Log.d("debug","Succsess to get myPid:" + myPid);
+    	}
+    }
+    private int getMyPid()
+    {
+    	int ret = -1;
     	String myPackageName = getApplicationContext().getPackageName();
     	Log.d("debug","myPackage name is:"+myPackageName);
     	ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
     	{
     		if(app.processName.equals(myPackageName))
     		{
-    			pid = app.pid;
+    			ret = app.pid;
     		}
     	}
+    	return ret;
+    }
+    private void getNetInfo()
+    {
     	
+    	ConnectionData connectionData = new ConnectionData();
     	
-    	
-    }
-	@Override
-	public void onClick(View arg0) {
-		// TODO Auto-generated method stub
-		/*
-		int i = Integer.valueOf(etNum.getText().toString());
-				Log.d("debug","try to get proccess" + i);
+    	Log.d("debug","start:getNetInfo()");
             try {
+            	//int i = Integer.valueOf(etNum.getText().toString());
+				//Log.d("debug","try to get proccess" + i);
 				 proc = Runtime.getRuntime().exec(
-				     "/proc/" + Integer.toString(i)+ "/net/tcp");
-			} catch (IOException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
-			if(proc != null)
-			{
-				Log.d("debug",proc.getOutputStream().toString());
-			}
-			*/
-			getApplicationInfo().
-			g
-            try {
-            	int i = Integer.valueOf(etNum.getText().toString());
-				Log.d("debug","try to get proccess" + i);
-				 proc = Runtime.getRuntime().exec(
-				     "cat /proc/"+i+"/net/tcp6");
+				     "cat /proc/"+myPid+"/net/tcp6");
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			if(proc != null)
 			{
+				Log.d("debug","proc is succsessfylly worked");
                br = new BufferedReader(new InputStreamReader(proc.getInputStream()), 4056);
                try{
+            	   int counter = 0;
                while(true)
                {
 					outStr = br.readLine();
             	  if (outStr != null && outStr.length() != 0)
             		  {
-            		  	Log.d("debug","line:"+outStr);
+            		  	if(counter == 0)
+            		  		{
+            		  			counter++;
+            		  			continue;
+            		  		}
+            		  	
+            		  	Log.d("debug",outStr);
+            		  	StringTokenizer stk = new StringTokenizer(outStr, ": ");
+            		  	Log.d("debug","try to tokenize");
+            		  	int stkCoutner = 0;
+            		  	while(stk.hasMoreTokens())
+            		  	{
+            		  		if(stkCoutner == 1) connectionData.localIP = Integer.parseInt(stk.nextToken(),16);
+            		  		else if(stkCoutner == 2) connectionData.localPort = Integer.parseInt(stk.nextToken(),16);
+            		  		else if(stkCoutner == 3) connectionData.remoteIP = Integer.parseInt(stk.nextToken(),16);
+            		  		else if(stkCoutner == 4) connectionData.remotePort = Integer.parseInt(stk.nextToken(),16);
+            		  		else if(stkCoutner == 5) connectionData.state = Integer.parseInt(stk.nextToken(),16);
+            		  		//UID が16進数か10進数かよくわからない
+            		  		else if(stkCoutner == 11) connectionData.uid = Integer.parseInt(stk.nextToken(),16);
+            		  		else stk.nextToken();
+            		  		stkCoutner++;
+            		  	}
+            		  	connectionData.debugInfo();
             		  }
             	  else
             	  {
             		  break;
             	  }
+            	  counter ++;
                }
                }
                catch(IOException e)
             	  e.printStackTrace(); 
                }
 			}
-        	
+    }
+	@Override
+	public void onClick(View arg0) {
+		getNetInfo();
 	}
 }