📄 ec_inet_cygwin.c
字号:
if (!strcmp(iface, "list")) exit(0); if (i == j) return -1; DEBUG_MSG("Inet_CorrectIface -- [%d] [%d] [%d]", dev, i, j); _Inet_OpenAdapter(alist[dev].name); sprintf(lpa.name, alist[dev].name); sprintf(lpa.desc, alist[dev].desc); return 0;}int Inet_GetIfaceInfo(char *iface, int *MTU, char *MyMAC, unsigned long *IP, unsigned long *NetMask){#ifdef WIN9X PIP_ADAPTER_INFO pinfo=NULL; u_long len = 0;#endif if (MTU != NULL) *MTU = 1500; // XXX -- it is better to find the real one... if (MyMAC != NULL) { PACKET_OID_DATA *data; u_char buf[512]; data = (PACKET_OID_DATA *)buf; data->Oid = OID_802_3_CURRENT_ADDRESS; data->Length = 6; if (PacketRequest(lpa.recv.lpAdapter, FALSE, data) == TRUE) { memcpy(MyMAC, data->Data, 6); } } if (IP != NULL) {#ifdef WIN9X len = sizeof(IP_ADAPTER_INFO); pinfo = (PIP_ADAPTER_INFO)malloc(len); GetAdaptersInfo(pinfo,&len); *IP = inet_addr(pinfo->IpAddressList.IpAddress.String);#else u_long foo; PacketGetNetInfo(lpa.name, IP, &foo); *IP = ntohl(*IP);#endif if (*IP == 0) { HKEY ServicesKey; HKEY InterfaceKey; HKEY TcpIpKey; WCHAR String[32]; ULONG BufLen; DWORD RegType; DWORD DHCPEnabled; char *clsid = strdup(strchr(lpa.name, '_') + 1); DEBUG_MSG("Inet_GetIfaceInfo -- try to correct DHCP bug of PacketGetNetInfo()"); RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Services"), 0, KEY_READ, &ServicesKey); RegOpenKeyEx(ServicesKey, clsid, 0, KEY_READ, &InterfaceKey); free(clsid); RegOpenKeyEx(InterfaceKey, TEXT("Parameters\\TcpIp"), 0, KEY_READ, &TcpIpKey); BufLen = sizeof(DHCPEnabled); RegQueryValueEx(TcpIpKey, TEXT("EnableDHCP"), NULL, &RegType, (LPBYTE)&DHCPEnabled, &BufLen); if (DHCPEnabled) { BufLen = sizeof(String); RegQueryValueEx(TcpIpKey,TEXT("DHCPIPAddress"), NULL, &RegType, (LPBYTE)String, &BufLen); *IP = inet_addr((char *)String); DEBUG_MSG("Inet_GetIfaceInfo -- DHCP bug recovered with %s", inet_ntoa(*(struct in_addr *)IP)); } else { Error_msg("The Adapter has an invalid ip address"); } } } if (NetMask != NULL) {#ifdef WIN9X if (pinfo==NULL) { len = sizeof(IP_ADAPTER_INFO); pinfo = (PIP_ADAPTER_INFO)malloc(len); GetAdaptersInfo(pinfo,&len); } *NetMask = inet_addr(pinfo->IpAddressList.IpMask.String);#else u_long foo; PacketGetNetInfo(lpa.name, &foo, NetMask); *NetMask = ntohl(*NetMask);#endif if (strcmp(Options.netmask, "")) // specified on command line *NetMask = inet_addr(Options.netmask); }#ifdef WIN9X if (pinfo != NULL) free(pinfo);#endif return 0;}void Inet_SetNonBlock(int sock){ DEBUG_MSG("Inet_SetNonBlock fd = %d", sock); fcntl(sock, F_SETFL, O_NONBLOCK);}void Inet_CloseRawSock(int sock){ DEBUG_MSG("Inet_CloseRawSock \t wrappered to /dev/NULL"); close(sock);}int Inet_OpenRawSock(char *iface){ DEBUG_MSG("Inet_OpenRawSock \t WRAPPERED TO /dev/NULL"); return open("/dev/null", O_RDONLY, 0600 );}int Inet_GetRawPacket(int sock, char *buffer, int MTU, short *type){ int len = 0, pktlen = 0; u_char *bp, *ep; static char MyMAC[6]={0x65,0x74,0x74,0x65,0x72,0x63}; if (SocketBuffer == -1) // only the first time { SocketBuffer = Buffer_Create(1.0e5); // 100 K buffer DEBUG_MSG("Inet_GetRawPacket creates the buffer for the first time -- buf id = %d", SocketBuffer); } Buffer_Get(SocketBuffer, &pktlen, sizeof(u_int)); len = Buffer_Get(SocketBuffer, buffer, pktlen ); if (type != NULL) { if (!strncmp(MyMAC,"etterc",6)) // only the first time... Inet_GetIfaceInfo(Options.netiface, NULL, MyMAC, NULL, NULL); if (!memcmp(MyMAC,buffer,6)) *type = PACKET_HOST; else *type = !PACKET_HOST; } if (len > 0) return len; // there was pending fata.// DEBUG_MSG("Inet_GetRawPacket -- pre Receive" ); PacketReceivePacket(lpa.recv.lpAdapter, lpa.recv.lpPacket, TRUE); len = lpa.recv.lpPacket->ulBytesReceived;// DEBUG_MSG("Inet_GetRawPacket -- after receive %d", len ); bp = lpa.recv.lpPacket->Buffer; /* * Loop through each packet. */#define bhp ((struct bpf_hdr *)bp) ep = bp + len; while (bp < ep) { int caplen, hdrlen; caplen = bhp->bh_caplen; hdrlen = bhp->bh_hdrlen; Buffer_Put(SocketBuffer, &caplen, sizeof(u_int) ); Buffer_Put(SocketBuffer, bp + hdrlen, caplen ); bp += Packet_WORDALIGN(caplen + hdrlen); }#undef bhp Buffer_Get(SocketBuffer, &pktlen, sizeof(u_int)); len = Buffer_Get(SocketBuffer, buffer, pktlen ); if (type != NULL) { if (!memcmp(MyMAC,buffer,6)) *type = PACKET_HOST; else *type = !PACKET_HOST; } return len;}int Inet_SendRawPacket(int sock, char *buffer, int len){ LPPACKET lpPacket; if( (lpPacket = PacketAllocatePacket()) == NULL) ERROR_MSG("Failed to allocate the LPPACKET structure."); PacketInitPacket(lpPacket, buffer, len);// DEBUG_MSG("I'm going to send %d bytes", len); if ( PacketSendPacket(lpa.send.lpAdapter, lpPacket, TRUE) == FALSE) ERROR_MSG("Failed to write to the adapter");// DEBUG_MSG(" %d bytes sent", len); PacketFreePacket(lpPacket); return (len);}int Inet_SetPromisc(char *iface){ DEBUG_MSG("Inet_SetPromisc"); PacketSetHwFilter(lpa.recv.lpAdapter, NDIS_PACKET_TYPE_PROMISCUOUS); atexit(Inet_Restore_ifr); return 0;}void Inet_Restore_ifr(void){ DEBUG_MSG("Inet_Restore_ifr"); PacketSetHwFilter(lpa.recv.lpAdapter, NDIS_PACKET_TYPE_ALL_LOCAL);}void Inet_DisableForwarding(void){ HKEY IpForwardKey; long Status;#ifdef WIN9X DWORD dim = 2; char IpForwardSz[2];#else DWORD dim = 4;#endif DWORD value = 3; Status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,#ifdef WIN9X TEXT("SYSTEM\\CurrentControlSet\\Services\\VxD\\Mstcp"),#else TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),#endif 0, KEY_READ|KEY_SET_VALUE, &IpForwardKey);#ifdef WIN9X Status = RegQueryValueEx(IpForwardKey, TEXT("EnableRouting"), NULL, NULL, (LPBYTE)IpForwardSz, &dim); value = IpForwardSz[0]-'0';#else Status = RegQueryValueEx(IpForwardKey, TEXT("IPEnableRouter"), NULL, NULL, (LPBYTE)&value, &dim);#endif DEBUG_MSG("Inet_DisableForwarding -- previous value %d", value); if (value == 0) { RegCloseKey(IpForwardKey); return; // this is ok and don't need to be changed } IpForward_status = value; atexit(Inet_RestoreForwarding); value = 0;#ifdef WIN9X IpForwardSz[0]=value+'0'; IpForwardSz[1]='\0'; Status = RegSetValueEx(IpForwardKey, TEXT("EnableRouting"), 0, REG_SZ, (LPBYTE)IpForwardSz, dim);#else Status = RegSetValueEx(IpForwardKey, TEXT("IPEnableRouter"), 0, REG_DWORD, (LPBYTE)&value, dim);#endif if (Status != 0) { fprintf(stderr, "\n\nRegSetValueEx() | ERROR %ld\n\n", Status); fprintf(stderr, "Please manually disable ip forwarding\n");#ifdef WIN9X fprintf(stderr, "set to 0 the HKLM\\SYSTEM\\CurrentControlSet\\Services\\VxD\\Mstcp\\EnableRouting\n\n");#else fprintf(stderr, "set to 0 the HKLM\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\IPEnableRouter\n\n");#endif exit(-1); } RegCloseKey(IpForwardKey);}void Inet_RestoreForwarding(void){ HKEY IpForwardKey;#ifdef WIN9X DWORD dim = 2; char IpForwardSz[2];#else DWORD dim = 4;#endif RegOpenKeyEx(HKEY_LOCAL_MACHINE,#ifdef WIN9X TEXT("SYSTEM\\CurrentControlSet\\Services\\VxD\\Mstcp"),#else TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),#endif 0, KEY_READ|KEY_SET_VALUE, &IpForwardKey);#ifdef WIN9X IpForwardSz[0]=IpForward_status+'0'; IpForwardSz[1]='\0'; RegSetValueEx(IpForwardKey, TEXT("EnableRouting"), 0, REG_SZ, (LPBYTE)IpForwardSz, dim);#else RegSetValueEx(IpForwardKey, TEXT("IPEnableRouter"), 0, REG_DWORD, (LPBYTE)&IpForward_status, dim);#endif DEBUG_MSG("Inet_RestoreForwarding -- new value %d", IpForward_status); RegCloseKey(IpForwardKey);}char *Inet_MacFromIP(unsigned long ip){ HRESULT hr; static char mac[6]; ULONG ulLen; memset (mac, 0xff, sizeof(mac)); ulLen = 6; hr = SendARP_(ip, 0, (PULONG)mac, &ulLen);#ifdef DEBUG { char mac_string[17]; Inet_PutMACinString(mac_string, mac); DEBUG_MSG("Inet_MacFromIP -- %s \t%s", inet_ntoa(*(struct in_addr *)&ip), mac_string); }#endif return mac;}/* EOF */// vim:ts=3:expandtab
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -