Código
/* Print all the available information on the given interface */ void ifprint(pcap_if_t *d) { pcap_addr_t *a; char ip6str[128]; /* Name */ printf("%s\n",d->name); /* Description */ if (d->description) printf("\tDescription: %s\n",d->description); /* Loopback Address*/ printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no"); /* IP addresses */ for(a=d->addresses;a;a=a->next) { printf("\tAddress Family: #%d\n",a->addr->sa_family); switch(a->addr->sa_family) { case AF_INET: printf("\tAddress Family Name: AF_INET\n"); if (a->addr) printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr)); if (a->netmask) printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr)); if (a->broadaddr) printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr)); if (a->dstaddr) printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr)); break; case AF_INET6: printf("\tAddress Family Name: AF_INET6\n"); if (a->addr) printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str))); break; default: printf("\tAddress Family Name: Unknown\n"); break; } } printf("\n"); }
Lo que he conseguido traducir a C#:
Código
public void GetDeviceInf(pcap_if dev) { String inf=""; pcap_addr addresses; IntPtr ip6str;//char ip6str[128]; sockaddr addr; inf += "Nombre de la interfaz: " + dev.name + Environment.NewLine; if (dev.description!=null) inf += "Descripción de la interfaz: " + dev.description + Environment.NewLine; //inf += "Loopback: " + dev.flags + PCAP_IF_LOOPBACK ",(d.flags & PCAP_IF_LOOPBACK)?"yes":"no"); do { addresses=dev.addresses; inf += "ADDR.SA_DATA: " + addr.sa_data + Environment.NewLine; inf += "Address Family:: " + addr.sa_family + Environment.NewLine; Console.WriteLine(inf); switch(addr.sa_family) { //case AF_INET: // Console.WriteLine("Address Family Name: AF_INET\n"); /*//if (addresses) Console.WriteLine("Address: %s\n",iptos((addr.sin_addr.s_addr)); if (addresses.netmask!=null) //Console.WriteLine("Netmask: %s\n",((struct sockaddr_in *)addresses.netmask).sin_addr.s_addr); if (addresses.broadaddr!=null) //Console.WriteLine("Broadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr)); if (addresses.dstaddr!=null) Console.WriteLine("Destination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr)); break; case AF_INET6: Console.WriteLine("\tAddress Family Name: AF_INET6\n"); if (a->addr) Console.WriteLine("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str))); break; default: printf("\tAddress Family Name: Unknown\n"); break;*/ } Console.WriteLine(inf); inf=""; }while(addresses.next!=IntPtr.Zero); }
Addresses.addr es de tipo IntPtr y apunta a una estructura de tipo pcap_addr hasta ahí todo bien pues lo igualo sin problemas. Luego el puntero addresses.addr que es un puntero de tipo IntPtr que apunta a una estructura de tipo sockaddr trato de pasarlo a estrcutura con la siguiente linea de codigo:
Código
Y esto falla, ¿porque se produce este error?
Pues el objetivo final no es mas que sacar datos de la interfaz de red.
He leído esto y he buscado pero no consigo superarlo, ayuda y gracias de antemano.
Edito: me estoy mirando esto a ver si me lo saben resolver o al menos lo entiendo xD y funciona.
Saludos socios.