/* * getnetbyname/addr - get entries from network database */ int dogetnet(const char **list) { struct netent *np; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { while ((np = getnetent()) != NULL) (void) putnetent(np, stdout); } else { for (; *list != NULL; list++) { long addr = inet_network(*list); if (addr != -1) np = getnetbyaddr(addr, AF_INET); else np = getnetbyname(*list); if (np == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putnetent(np, stdout); } } return (rc); }
static int networks(int argc, char *argv[]) { int i, rv = RV_OK; struct netent *ne; in_addr_t net; setnetent(1); if (argc == 2) { while ((ne = getnetent()) != NULL) networksprint(ne); } else { for (i = 2; i < argc; i++) { net = inet_network(argv[i]); if (net != INADDR_NONE) ne = getnetbyaddr(net, AF_INET); else ne = getnetbyname(argv[i]); if (ne != NULL) networksprint(ne); else { rv = RV_NOTFOUND; break; } } } endnetent(); return rv; }
struct netent *wp_getnetent (void) { struct netent *p; if ((p = getnetent ()) == NULL) wp_warning ("getnetent() error"); return p; }
static void test_network (void) { struct netent *nptr; u_int32_t ip; /* This test needs the following line in /etc/networks: loopback 127.0.0.0 */ nptr = getnetbyname ("loopback"); output_netent ("getnetbyname (\"loopback\")",nptr); nptr = getnetbyname ("LoopBACK"); output_netent ("getnetbyname (\"LoopBACK\")",nptr); ip = inet_network ("127.0.0.0"); nptr = getnetbyaddr (ip, AF_INET); output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr); setnetent (0); do { nptr = getnetent (); output_netent ("getnetent ()", nptr); } while (nptr != NULL); endnetent (); }
main() { setnetent(1); struct netent *net; while((net=getnetent())!=NULL) { printf("%s,%d,%u\n",net->n_name,net->n_addrtype,net->n_net); } endservent(); }
void ReadNetworksFile (const char *fname) { static int been_here = 0; if (!fname || !*fname) return; if (been_here) /* loading multiple network files */ { free (networkFname); fclose (networkFile); networkFile = NULL; } been_here = 1; networkFname = strdup (fname); if (!networkFname) return; setnetent (1); if (!networkFile) return; while (1) { struct _netent *n, *n2 = (struct _netent*) getnetent(); if (!n2 || (n = malloc(sizeof(*n))) == NULL) break; *n = *n2; n->next = network0; network0 = n; } rewind (networkFile); atexit (endnetent); #if 0 /* test */ { struct _netent *n; printf ("%s entries:\n", fname); for (n = network0; n; n = n->next) { int i; printf ("net = %-15.15s name = %-10.10s alias:", inet_ntoa(inet_makeaddr(n->n_net,0)), n->n_name); for (i = 0; n->n_aliases && n->n_aliases[i]; i++) printf (" %s", n->n_aliases[i]); puts (""); } } #endif }
struct netent *getnetbyaddr (uint32_t net, int type) { register struct netent *p; setnetent(_net_stayopen); while ((p = getnetent())) if (p->n_addrtype == type && p->n_net == net) break; if (!_net_stayopen) endnetent(); return (p); }
struct netent * getnetbyname(const char *name) { register struct netent *p; register char **cp; setnetent(_net_stayopen); while (p = getnetent()) { if (strcmp(p->n_name, name) == 0) break; for (cp = p->n_aliases; *cp != 0; cp++) if (strcmp(*cp, name) == 0) goto found; } found: if (!_net_stayopen) endnetent(); return (p); }
NET_R_RETURN getnetent_r(struct netent *nptr, NET_R_ARGS) { struct netent *ne = getnetent(); #ifdef NET_R_SETANSWER int n = 0; if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) *answerp = NULL; else *answerp = ne; if (ne == NULL) *h_errnop = h_errno; return (n); #else if (ne == NULL) return (NET_R_BAD); return (copy_netent(ne, nptr, NET_R_COPY)); #endif }
struct netent * nw_getnetent(void) { return ((struct netent *) getnetent()); }
void ReadNetworksFile (const char *fname) { static BOOL been_here = FALSE; if (!fname || !*fname) return; if (been_here) /* loading multiple network files */ { free (networkFname); fclose (networkFile); networkFile = NULL; } networkFname = strdup (fname); if (!networkFname) return; setnetent (1); if (!networkFile) return; been_here = TRUE; while (1) { struct netent *n = getnetent(); struct _netent *n2; int i; if (!n) break; n2 = (struct _netent*) calloc (sizeof(*n2), 1); if (!n2) { outs (networkFname); outsnl (_LANG(" too big!")); break; } for (i = 0; n->n_aliases[i]; i++) n2->n_aliases[i] = strdup (n->n_aliases[i]); n2->n_net = n->n_net; n2->n_addrtype = n->n_addrtype; n2->n_name = strdup (n->n_name); if (!n2->n_name) break; n2->n_next = network0; network0 = n2; } rewind (networkFile); RUNDOWN_ADD (endnetent, 251); #if 0 /* test */ { struct _netent *n; printf ("\n%s entries:\n", networkFname); for (n = network0; n; n = n->n_next) { int i; printf ("net %-15.15s name %-10.10s Aliases:", inet_ntoa(inet_makeaddr(n->n_net,0)), n->n_name); for (i = 0; n->n_aliases[i]; i++) printf (" %s,", n->n_aliases[i]); puts (""); } fflush (stdout); } #endif }