📄 net.cc
字号:
if (err == host_errmap[i].e) break; return host_errmap[i].s;}/* exported as herror: BSD 4.3 */extern "C" voidcygwin_herror (const char *s){ if (s && check_null_str (s)) return; if (cygheap->fdtab.not_open (2)) return; if (s) { write (2, s, strlen (s)); write (2, ": ", 2); } const char *h_errstr = cygwin_hstrerror (h_errno); if (!h_errstr) switch (h_errno) { case NETDB_INTERNAL: h_errstr = "Resolver internal error"; break; case NETDB_SUCCESS: h_errstr = "Resolver error 0 (no error)"; break; default: h_errstr = "Unknown resolver error"; break; } write (2, h_errstr, strlen (h_errstr)); write (2, "\n", 1);}/* exported as getpeername: standards? */extern "C" intcygwin_getpeername (int fd, struct sockaddr *name, int *len){ int res; sig_dispatch_pending (0); sigframe thisframe (mainthread); fhandler_socket *fh = get (fd); if (check_null_invalid_struct_errno (len) || __check_null_invalid_struct_errno (name, (unsigned) *len) || !fh) res = -1; else res = fh->getpeername (name, len); syscall_printf ("%d = getpeername %d", res, (fh ? fh->get_socket () : -1)); return res;}/* exported as recv: standards? */extern "C" intcygwin_recv (int fd, void *buf, int len, int flags){ return cygwin_recvfrom (fd, buf, len, flags, NULL, NULL);}/* exported as send: standards? */extern "C" intcygwin_send (int fd, const void *buf, int len, int flags){ return cygwin_sendto (fd, buf, len, flags, NULL, 0);}/* getdomainname: standards? */extern "C" intgetdomainname (char *domain, size_t len){ /* * This works for Win95 only if the machine is configured to use MS-TCP. * If a third-party TCP is being used this will fail. * FIXME: On Win95, is there a way to portably check the TCP stack * in use and include paths for the Domain name in each ? * Punt for now and assume MS-TCP on Win95. */ sig_dispatch_pending (0); sigframe thisframe (mainthread); if (__check_null_invalid_struct_errno (domain, len)) return -1; reg_key r (HKEY_LOCAL_MACHINE, KEY_READ, (!wincap.is_winnt ()) ? "System" : "SYSTEM", "CurrentControlSet", "Services", (!wincap.is_winnt ()) ? "VxD" : "Tcpip", (!wincap.is_winnt ()) ? "MSTCP" : "Parameters", NULL); /* FIXME: Are registry keys case sensitive? */ if (r.error () || r.get_string ("Domain", domain, len, "") != ERROR_SUCCESS) { __seterrno (); return -1; } return 0;}/* Fill out an ifconf struct. *//* * IFCONF 98/ME, NTSP4, W2K: * Use IP Helper Library */static voidget_2k_ifconf (struct ifconf *ifc, int what){ int cnt = 0; int ethId = 0, pppId = 0, slpId = 0, tokId = 0; /* Union maps buffer to correct struct */ struct ifreq *ifr = ifc->ifc_req; DWORD ip_cnt, lip, lnp; DWORD siz_ip_table = 0; PMIB_IPADDRTABLE ipt; PMIB_IFROW ifrow; struct sockaddr_in *sa = NULL; struct sockaddr *so = NULL; typedef struct ifcount_t { DWORD ifIndex; size_t count; unsigned int enumerated; // for eth0:1 unsigned int classId; // for eth0, tok0 ... }; ifcount_t *iflist, *ifEntry; if (GetIpAddrTable (NULL, &siz_ip_table, TRUE) == ERROR_INSUFFICIENT_BUFFER && (ifrow = (PMIB_IFROW) alloca (sizeof (MIB_IFROW))) && (ipt = (PMIB_IPADDRTABLE) alloca (siz_ip_table)) && !GetIpAddrTable (ipt, &siz_ip_table, TRUE)) { iflist = (ifcount_t *) alloca (sizeof (ifcount_t) * (ipt->dwNumEntries + 1)); memset (iflist, 0, sizeof (ifcount_t) * (ipt->dwNumEntries + 1)); for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt) { ifEntry = iflist; /* search for matching entry (and stop at first free entry) */ while (ifEntry->count != 0) { if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex) break; ifEntry++; } if (ifEntry->count == 0) { ifEntry->count = 1; ifEntry->ifIndex = ipt->table[ip_cnt].dwIndex; } else { ifEntry->count++; } } // reset the last element. This is just the stopper for the loop. iflist[ipt->dwNumEntries].count = 0; /* Iterate over all configured IP-addresses */ for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt) { memset (ifrow, 0, sizeof (MIB_IFROW)); ifrow->dwIndex = ipt->table[ip_cnt].dwIndex; if (GetIfEntry (ifrow) != NO_ERROR) continue; ifcount_t *ifEntry = iflist; /* search for matching entry (and stop at first free entry) */ while (ifEntry->count != 0) { if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex) break; ifEntry++; } /* Setup the interface name */ switch (ifrow->dwType) { case MIB_IF_TYPE_TOKENRING: if (ifEntry->enumerated == 0) { ifEntry->classId = tokId++; __small_sprintf (ifr->ifr_name, "tok%u", ifEntry->classId); } else { __small_sprintf (ifr->ifr_name, "tok%u:%u", ifEntry->classId, ifEntry->enumerated - 1); } ifEntry->enumerated++; break; case MIB_IF_TYPE_ETHERNET: if (ifEntry->enumerated == 0) { ifEntry->classId = ethId++; __small_sprintf (ifr->ifr_name, "eth%u", ifEntry->classId); } else { __small_sprintf (ifr->ifr_name, "eth%u:%u", ifEntry->classId, ifEntry->enumerated - 1); } ifEntry->enumerated++; break; case MIB_IF_TYPE_PPP: if (ifEntry->enumerated == 0) { ifEntry->classId = pppId++; __small_sprintf (ifr->ifr_name, "ppp%u", ifEntry->classId); } else { __small_sprintf (ifr->ifr_name, "ppp%u:%u", ifEntry->classId, ifEntry->enumerated - 1); } ifEntry->enumerated++; break; case MIB_IF_TYPE_SLIP: if (ifEntry->enumerated == 0) { ifEntry->classId = slpId++; __small_sprintf (ifr->ifr_name, "slp%u", ifEntry->classId); } else { __small_sprintf (ifr->ifr_name, "slp%u:%u", ifEntry->classId, ifEntry->enumerated - 1); } ifEntry->enumerated++; break; case MIB_IF_TYPE_LOOPBACK: strcpy (ifr->ifr_name, "lo"); break; default: continue; } /* setup sockaddr struct */ switch (what) { case SIOCGIFCONF: case SIOCGIFADDR: sa = (struct sockaddr_in *) &ifr->ifr_addr; sa->sin_addr.s_addr = ipt->table[ip_cnt].dwAddr; sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFBRDADDR: sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;#if 0 /* Unfortunately, the field returns only crap. */ sa->sin_addr.s_addr = ipt->table[ip_cnt].dwBCastAddr;#else lip = ipt->table[ip_cnt].dwAddr; lnp = ipt->table[ip_cnt].dwMask; sa->sin_addr.s_addr = lip & lnp | ~lnp; sa->sin_family = AF_INET; sa->sin_port = 0;#endif break; case SIOCGIFNETMASK: sa = (struct sockaddr_in *) &ifr->ifr_netmask; sa->sin_addr.s_addr = ipt->table[ip_cnt].dwMask; sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFHWADDR: so = &ifr->ifr_hwaddr; for (UINT i = 0; i < IFHWADDRLEN; ++i) if (i >= ifrow->dwPhysAddrLen) so->sa_data[i] = '\0'; else so->sa_data[i] = ifrow->bPhysAddr[i]; so->sa_family = AF_INET; break; case SIOCGIFMETRIC: ifr->ifr_metric = 1; break; case SIOCGIFMTU: ifr->ifr_mtu = ifrow->dwMtu; break; } ++cnt; if ((caddr_t)++ ifr > ifc->ifc_buf + ifc->ifc_len - sizeof (struct ifreq)) goto done; } }done: /* Set the correct length */ ifc->ifc_len = cnt * sizeof (struct ifreq);}/* * IFCONF Windows NT < SP4: * Look at the Bind value in * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage\ * This is a REG_MULTI_SZ with strings of the form: * \Device\<Netcard>, where netcard is the name of the net device. * Then look under: * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<NetCard>\ * Parameters\Tcpip * at the IPAddress, Subnetmask and DefaultGateway values for the * required values. */static voidget_nt_ifconf (struct ifconf *ifc, int what){ HKEY key; unsigned long lip, lnp; struct sockaddr_in *sa = NULL; struct sockaddr *so = NULL; DWORD size; int cnt = 1; char *binding = (char *) 0; /* Union maps buffer to correct struct */ struct ifreq *ifr = ifc->ifc_req; if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\" "CurrentControlSet\\" "Services\\" "Tcpip\\" "Linkage", 0, KEY_READ, &key) == ERROR_SUCCESS) { if (RegQueryValueEx (key, "Bind", NULL, NULL, NULL, &size) == ERROR_SUCCESS) { binding = (char *) alloca (size); if (RegQueryValueEx (key, "Bind", NULL, NULL, (unsigned char *) binding, &size) != ERROR_SUCCESS) { binding = NULL; } } RegCloseKey (key); } if (binding) { char *bp, eth[2] = "/"; char cardkey[256], ipaddress[256], netmask[256]; for (bp = binding; *bp; bp += strlen (bp) + 1) { bp += strlen ("\\Device\\"); strcpy (cardkey, "SYSTEM\\CurrentControlSet\\Services\\"); strcat (cardkey, bp); strcat (cardkey, "\\Parameters\\Tcpip"); if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, cardkey, 0, KEY_READ, &key) != ERROR_SUCCESS) continue; if (RegQueryValueEx (key, "IPAddress", NULL, NULL, (unsigned char *) ipaddress, (size = 256, &size)) == ERROR_SUCCESS && RegQueryValueEx (key, "SubnetMask", NULL, NULL, (unsigned char *) netmask, (size = 256, &size)) == ERROR_SUCCESS) { char *ip, *np; char dhcpaddress[256], dhcpnetmask[256]; for (ip = ipaddress, np = netmask; *ip && *np; ip += strlen (ip) + 1, np += strlen (np) + 1) { if ((caddr_t)++ ifr > ifc->ifc_buf + ifc->ifc_len - sizeof (struct ifreq)) break; if (!strncmp (bp, "NdisWan", 7)) { strcpy (ifr->ifr_name, "ppp"); strcat (ifr->ifr_name, bp + 7); } else { ++*eth; strcpy (ifr->ifr_name, "eth"); strcat (ifr->ifr_name, eth); } memset (&ifr->ifr_addr, '\0', sizeof ifr->ifr_addr); if (cygwin_inet_addr (ip) == 0L && RegQueryValueEx (key, "DhcpIPAddress", NULL, NULL, (unsigned char *) dhcpaddress, (size = 256, &size)) == ERROR_SUCCESS && RegQueryValueEx (key, "DhcpSubnetMask", NULL, NULL, (unsigned char *) dhcpnetmask, (size = 256, &size)) == ERROR_SUCCESS) { switch (what) { case SIOCGIFCONF: case SIOCGIFADDR: sa = (struct sockaddr_in *) &ifr->ifr_addr; sa->sin_addr.s_addr = cygwin_inet_addr (dhcpaddress); sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFBRDADDR: lip = cygwin_inet_addr (dhcpaddress); lnp = cygwin_inet_addr (dhcpnetmask); sa = (struct sockaddr_in *) &ifr->ifr_broadaddr; sa->sin_addr.s_addr = lip & lnp | ~lnp; sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFNETMASK: sa = (struct sockaddr_in *) &ifr->ifr_netmask; sa->sin_addr.s_addr = cygwin_inet_addr (dhcpnetmask); sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFHWADDR: so = &ifr->ifr_hwaddr; memset (so->sa_data, 0, IFHWADDRLEN); so->sa_family = AF_INET; break; case SIOCGIFMETRIC: ifr->ifr_metric = 1; break; case SIOCGIFMTU: ifr->ifr_mtu = 1500; break; } } else { switch (what) { case SIOCGIFCONF: case SIOCGIFADDR: sa = (struct sockaddr_in *) &ifr->ifr_addr; sa->sin_addr.s_addr = cygwin_inet_addr (ip); sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFBRDADDR: lip = cygwin_inet_addr (ip); lnp = cygwin_inet_addr (np); sa = (struct sockaddr_in *) &ifr->ifr_broadaddr; sa->sin_addr.s_addr = lip & lnp | ~lnp; sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFNETMASK: sa = (struct sockaddr_in *) &ifr->ifr_netmask; sa->sin_addr.s_addr = cygwin_inet_addr (np); sa->sin_family = AF_INET; sa->sin_port = 0; break; case SIOCGIFHWADDR: so = &ifr->ifr_hwaddr; memset (so->sa_data, 0, IFHWADDRLEN); so->sa_family = AF_INET; break; case SIOCGIFMETRIC: ifr->ifr_metric = 1; break; case SIOCGIFMTU: ifr->ifr_mtu = 1500; break; } } ++cnt; } } RegCloseKey (key); } } /* Set the correct length */ ifc->ifc_len = cnt * sizeof (struct ifreq);}/* * IFCONF Windows 95: * HKLM/Enum/Network/MSTCP/"*" * -> Value "Driver" enth鋖t Subkey relativ zu * HKLM/System/CurrentControlSet/Class/ * -> In Subkey "Bindings" die Values aufz鋒len * -> Enth鋖t Subkeys der Form "VREDIR\*" * Das * ist ein Subkey relativ zu * HKLM/System/CurrentControlSet/Class/Net/ * HKLM/System/CurrentControlSet/Class/"Driver" * -> Value "IPAddress" * -> Value "IPMask" * HKLM/System/CurrentControlSet/Class/Net/"*"(aus "VREDIR\*") * -> Wenn Value "AdapterName" == "MS$PPP" -> ppp interface * -> Value "DriverDesc" enth鋖t den Namen * */static voidget_95_ifconf (struct ifconf *ifc, int what){ HKEY key; unsigned long lip, lnp; struct sockaddr_in *sa = NULL; struct sockaddr *so = NULL; FILETIME update; LONG res; DWORD size; int cnt = 1; char ifname[256]; char eth[2] = "/"; char ppp[2] = "/"; /* Union maps buffer to correct struct */ struct ifreq *ifr = ifc->ifc_req; if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Enum\\Network\\MSTCP", 0, KEY_READ, &key) != ERROR_SUCCESS) { /* Set the correct length */ ifc->ifc_len = cnt * sizeof (struct ifreq); return; } for (int i = 0; (res = RegEnumKeyEx (key, i, ifname, (size = sizeof ifname, &size), 0, 0, 0, &update)) != ERROR_NO_MORE_ITEMS; ++i) { HKEY ifkey, subkey; char driver[256], classname[256], netname[256]; char adapter[256], ip[256], np[256]; if (res != ERROR_SUCCESS || RegOpenKeyEx (key, ifname, 0, KEY_READ, &ifkey) != ERROR_SUCCESS) continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -