*socket raw not work. Error in creation socket. how to make a raw socket in
android?*
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.LOCATION_HARDWARE"/>
public class MainActivity extends Activity implements OnClickListener {
public native String InvokeNativeFunction();
static {
System.loadLibrary("Cping");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button=findViewById(R.id.button2);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
TextView texto=(TextView)findViewById(R.id.textView1);
String textoAmostrar="";
textoAmostrar=InvokeNativeFunction();
if (v.getId()==findViewById(R.id.button2).getId()){
texto.setText(textoAmostrar);
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <netinet/ip_icmp.h>
#include <string.h>
#include <jni.h>
#include <android/log.h>
#define DEFAULT_INIT_PORT 1 /* puerto inicial del escaneo por defecto */
#define DEFAULT_END_PORT 1024 /* puerto final del escaneo por defecto */
#define DEFAULT_ICMP_PORT 8765 /* puerto por defecto para mandar los
paquetes ICMP */
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_exadddmple_pingconc_MainActivity_InvokeNativeFunction(JNIEnv *
env, jobject obj);
};
JNIEXPORT jstring JNICALL
Java_com_exadddmple_pingconc_MainActivity_InvokeNativeFunction(JNIEnv *
env, jobject obj)
{
int sockfd,numbytes,flags,listen; //descriptor del socket, número de bytes
enviados, flags y resultado del connect()
int init_port, end_port; //variables de inicio y fin del escaneo
int result,fromlen; //resultado del select, y tamaño del ICMP de respuesta
struct hostent *he; //variable de tipo hostent donde se guardará
información del Host a escanear
struct sockaddr_in their_addr; // almacenarán la direccion IP del Host
objetivo, y sus datos
static u_char paquete_salida[64]; //paquete de salida ICMP
register struct icmp *cabecera_icmp = (struct icmp *) paquete_salida;
//formato de paquete ICMP
fd_set fd_leer,fd_escribir; //descriptores a controlar por select
struct timeval tv; //temporizador para el uso de select
char recv_ICMP[512],shost[100];
struct servent *bar; //guardará el nombre del servicio que corre en cada
puerto
int i,val,len=sizeof(val); //contador, resultado getsockopt
init_port= DEFAULT_INIT_PORT;
end_port= DEFAULT_END_PORT;
if ((sockfd = socket(AF_INET, SOCK_RAW,1)) < 0)
{
return env->NewStringUTF("Error socket");
//exit(1);
}
if ((he= gethostbyname("www.google.com")) == NULL)
{
return env->NewStringUTF("Error gethostbyname");
//exit(1);
}
/* Creamos el socket */
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(DEFAULT_ICMP_PORT); /* network byte order */
their_addr.sin_addr = *((struct in_addr *)he->h_addr); // dirección IP
bzero(&(their_addr.sin_zero), 8);
sprintf(shost,"%d.%d.%d.%d",(unsigned char)he->h_addr_list[0][0],(unsigned
char)he->h_addr_list[0][1],(unsigned char)he->h_addr_list[0][2],\
(unsigned char)he->h_addr_list[0][3]);
/* configuración cabecera mensaje ICMP */
cabecera_icmp->icmp_type = ICMP_ECHO; // Tipo de mensajes ICMP
cabecera_icmp->icmp_code = 0; // Código de mensaje ICMP
cabecera_icmp->icmp_cksum = 0; // Checksum del paquete ICMP
cabecera_icmp->icmp_seq =1; // Numero de secuencia
cabecera_icmp->icmp_id = 0x1111; // ID de paquete
/* Transmitimos el paquete ICMP */
if ((numbytes=sendto(sockfd, &paquete_salida, 64, 0, (struct sockaddr
*)&their_addr, sizeof(struct sockaddr))) == -1)
{
return env->NewStringUTF("Error sendto");
}
do {
FD_ZERO(&fd_leer); // pone a 0 todos los bits de fd_var.
FD_SET(sockfd, &fd_leer); //activa en fd_leer el bit correspondiente al
descriptor sockfd
tv.tv_sec=2; // 2 segundos de tiempo de espera para recibir la respuesta
al ICMP
tv.tv_usec=0;
result = select(sockfd +1, &fd_leer, NULL, NULL, &tv);
} while (result == -1 && errno == EINTR);
if (result > 0) {
if (FD_ISSET(sockfd, &fd_leer)) {
/* El socket tiene datos para leer */
result = recvfrom(sockfd, recv_ICMP, sizeof recv_ICMP, 0, NULL,
&fromlen);
if (result == 0) {
/* Conexión cerrada por el host */
close(sockfd);
return env->NewStringUTF("El host ha cerrado la conexión.");
}
else {
close(sockfd);
return env->NewStringUTF("El host está activo,procederemos a chequear los
servicios...");
}
}//FIN_MAIN
}
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/a1d89005-fb8d-4e26-bd13-f415b020c124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.