unsigned short * get_tcp_svcs(int * num) { struct nessus_service * ns = NULL; int len, num_svc; unsigned short * ret; int fd, i; struct stat st; if ((fd = open(NESSUS_SERVICES_TCP, O_RDONLY)) >= 0) { if (fstat(fd, &st) < 0) perror("fstat"); else { len = st.st_size; num_svc = len / sizeof(struct nessus_service); /*if ((ns = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0))== MAP_FAILED ) { perror("mmap"); ns = NULL; }*/ } } if (ns == NULL) { struct servent * ent; int n = 0; __try { ret = emalloc(sizeof(unsigned short) * 65537); endservent(); while ( (ent = getservent()) != NULL ) { if(strcmp(ent->s_proto, "tcp") == 0 && ntohs(ent->s_port)) { ret[n++] = ntohs(ent->s_port); if(n >= 65537)break; } } endservent(); if(num != NULL) *num = n; ret = erealloc(ret, sizeof(unsigned short) * (n+1)); ret[n] = 0; } __except (EXCEPTION_EXECUTE_HANDLER) { fprintf(stderr, "get_tcp_svcs() causes an exception.\n"); } return ret; } else {
static int read_services(void) { struct servent *se; struct service *item; setservent(1); while((se=getservent())) { /* Allocate a service entry. */ item = (struct service *) malloc(sizeof(struct service)); if (item == NULL) perror("netstat"); item->name = strdup(se->s_name); item->number = se->s_port; /* Fill it in. */ if (! strcmp(se->s_proto, "tcp")) { add2list(&tcp_name,item); } else if (! strcmp(se->s_proto, "udp")) { add2list(&udp_name,item); } else if (! strcmp(se->s_proto, "raw")) { add2list(&raw_name,item); } } endservent(); return(0); }
struct servent * getservbyport(int port, const char *proto) { struct servent *se; if (proto == NULL) { return NULL; } setservent(1); port = htons((unsigned short)port); while ((se = getservent()) != NULL) { /* same protocol? */ if (strcmp(proto, se->s_proto) != 0) { continue; /* not same. try again */ } /* same port? */ if (se->s_port == port) { break; } /* nope. try again */ } endservent(); return se; }
static int services(int argc, char *argv[]) { struct servent *se; int i, rv = RV_OK; setservent(1); if (argc == 2) { while ((se = getservent()) != NULL) SERVICESPRINT; } else { for (i = 2; i < argc; i++) { const char *err; long long id; char *proto = strchr(argv[i], '/'); if (proto != NULL) *proto++ = '\0'; id = strtonum(argv[i], 0, UINT_MAX, &err); if (!err) se = getservbyport(htons((u_short)id), proto); else se = getservbyname(argv[i], proto); if (se != NULL) SERVICESPRINT; else { rv = RV_NOTFOUND; break; } } } endservent(); return rv; }
struct servent * getservbyport( int port, const char *proto) { register struct servent *p; #ifdef YP extern int ___getservbyport_yp; extern char *___getservbyproto_yp; ___getservbyport_yp = port; ___getservbyproto_yp = (char *)proto; #endif setservent(_serv_stayopen); while ( (p = getservent()) ) { if (p->s_port != port) continue; if (proto == 0 || strcmp(p->s_proto, proto) == 0) break; } if (!_serv_stayopen) endservent(); #ifdef YP ___getservbyport_yp = 0; ___getservbyproto_yp = NULL; #endif return (p); }
void ServiceList::assignSystemServices(void) { struct servent *entry; clearServiceList(true); /* Open /etc/services */ entry = getservent(); while (entry != NULL) { /* Only show protocol tcp and udp. */ wxString prot = wxString::From8BitData(entry->s_proto); if (prot == wxT("tcp") || prot == wxT("udp")) { Service *service = new Service( wxString::From8BitData(entry->s_name), entry->s_port, (prot == wxT("tcp") ? Service::TCP : Service::UDP), false); addService(service); } entry = getservent(); } /* Close /etc/services */ endservent(); sizeChangeEvent(serviceList_.size()); }
/* libc_hidden_proto(getservbyname_r) */ int getservbyname_r(const char *name, const char *proto, struct servent * result_buf, char * buf, size_t buflen, struct servent ** result) { register char **cp; int ret; __UCLIBC_MUTEX_LOCK(mylock); setservent(serv_stayopen); while (!(ret=getservent_r(result_buf, buf, buflen, result))) { if (strcmp(name, result_buf->s_name) == 0) goto gotname; for (cp = result_buf->s_aliases; *cp; cp++) if (strcmp(name, *cp) == 0) goto gotname; continue; gotname: if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0) break; } if (!serv_stayopen) endservent(); __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; }
endservent_r() #endif { #ifdef SERV_R_ENT_UNUSED SERV_R_ENT_UNUSED; #endif endservent(); SERV_R_END_RESULT(SERV_R_OK); }
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(); }
pid_t ypldap_dns(int pipe_ntp[2], struct passwd *pw) { pid_t pid; struct event ev_sigint; struct event ev_sigterm; struct event ev_sighup; struct env env; switch (pid = fork()) { case -1: fatal("cannot fork"); break; case 0: break; default: return (pid); } setproctitle("dns engine"); close(pipe_ntp[0]); if (setgroups(1, &pw->pw_gid) || setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) fatal("can't drop privileges"); endservent(); if (pledge("stdio dns", NULL) == -1) fatal("pledge"); event_init(); signal_set(&ev_sigint, SIGINT, dns_sig_handler, NULL); signal_set(&ev_sigterm, SIGTERM, dns_sig_handler, NULL); signal_set(&ev_sighup, SIGHUP, dns_sig_handler, NULL); signal_add(&ev_sigint, NULL); signal_add(&ev_sigterm, NULL); signal_add(&ev_sighup, NULL); if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL) fatal(NULL); env.sc_iev->events = EV_READ; env.sc_iev->data = &env; imsg_init(&env.sc_iev->ibuf, pipe_ntp[1]); env.sc_iev->handler = dns_dispatch_imsg; event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events, env.sc_iev->handler, &env); event_add(&env.sc_iev->ev, NULL); event_dispatch(); dns_shutdown(); return (0); }
int getport(char *port) { struct servent *sv; int n; const char *errstr; sv = getservbyname(port, "tcp"); if (sv == NULL) { n = strtonum(port, 1, UINT16_MAX, &errstr); if (errstr != NULL) { endservent(); return (-1); } } else n = ntohs(sv->s_port); endservent(); return (n); }
/* The setservent() function opens and rewinds the `/etc/services' file. This file can be set from outside with an environment variable specifying the file name. */ void setservent (int stayopen) { char *file = NULL; endservent (); if ((file = getenv (ENVIRON_ETC_SERVICES)) != NULL) strcpy (scm_i_servent.file, file); else if ((file = scm_i_socket_filename (FILE_ETC_SERVICES)) != NULL) strcpy (scm_i_servent.file, file); scm_i_servent.fd = fopen (scm_i_servent.file, "rt"); }
/* Services are represented by the port number portion of the address. Each * service is offered on a unique, well-known port number. We can map a * service name to a port number with getservbyname(), map a port number to * a service name with getservbyport(), or scan the services database * sequentially with getservent(). * * #include <netdb.h> * void endservent(void); * struct servent *getservbyname(const char *name, const char *proto); * struct servent *getservbyport(int port, const char *proto); * struct servent *getservent(void): * void setservent(int stayopen); * * These functions shall retrieve information about network services. * * The setservent() function shall open a connection to the database, and * set the next entry to the first entry. If the stayopen argument is non- * zero, the net database shall not be closed after each call to the * getservent() function (either directly, or indirectly through one of the * other getserv*() functions). * * The getservent() function shall read the next entry of the database, * opending and closing a connection to the database as necessary. * * The getservbyname() function shall search the database from the beginning * and find the first entry for which the service name specified by name * matches the s_name member and the protocol name specified by proto * matches the s_proto mumber, opening and closing a connection to the * database necessary. If proto is a null pointer, any value of the s_proto * member shall be matched. 当 proto 是NULL指针时,将匹配任意的协议名,但是 * getservbyname()函数还是只返回第一项匹配的指针,不会返回多项.这里的"匹配 * 任意协议名"应该是指不判断协议名. * * The getservbyport() function shall search the database from the beginning * and find the first entry for which the port specified by port matches the * s_port member and the protocol name specified by proto matches the * s_proto member, opening and closing a connection to the database as * necessary. If proto is a null pointer, any value of the s_proto member * shall be matched. The port argument shall be a value obtained by * converting a uint16_t in network byte order to int. 即 port 参数的值 * 是网络字节序的,经过测试,要调用htons()函数来转换得到网络字节序的值,不能 * 使用htonl()函数, htonl(80) 得到的值是0x50000000, htons(80) 得到的值是 * 0x5000, 两者的结果是不同的. * * The endservent() function shall close the database, releasing any open * file descriptor. * * These functions need not be thread-safe. * * Upon successful completion, getservbyname(), getservbyport(), and * getservent() return a pointer to a servent structure if the requested * entry was found, and a null pointer if the end of the database was * reached or the requested entry was not found. Otherwise, a null pointer * is returned. No errors are defined. * * Linux 中, struct servent 结构体定义如下: * struct servent { * char *s_name; // official service name * char **s_aliases; // alias list, terminated by NULL * int s_port; // The port number for the service given * // in network byte order * char *s_proto; // protocol to use with this service * }; * * 查看执行结果,发现输出的服务名和 /etc/services 文件里面的服务名是一样的. */ int main(void) { struct servent *serv; int i, port; while ((serv = getservent()) != NULL) printservent(serv); endservent(); printf("========= getservbyname: name --> echo, proto --> tcp\n"); if ((serv = getservbyname("echo", "tcp")) != NULL) printservent(serv); /* 虽然 echo 服务可以使用tcp协议,也可以使用udp协议,但是下面的语句还是 * 只会返回其中一项的值,指定getservbyname()函数的第二个参数为NULL,并 * 不会两个echo服务 (使用tcp协议的echo和使用udp协议的echo). */ printf("========= getservbyname: name --> echo, proto --> NULL\n"); if ((serv = getservbyname("echo", NULL)) != NULL) printservent(serv); printf("========= getservbyprot: port --> 80, proto --> tcp\n"); port = htons(80); printf("port: host 80 --> network is: %#x\n", port); if ((serv = getservbyport(port, "tcp")) != NULL) printservent(serv); printf("========= setservent() =========\n"); if ((serv = getservent()) != NULL) printservent(serv); setservent(0); printf("========= after setservent() =========\n"); for (i = 0; i < 2; ++i) if ((serv = getservent()) != NULL) printservent(serv); endservent(); return 0; }
/* * getservbyname() - looks up a service by its name */ struct servent *getservbyname(const char *servname, const char *protname) { struct servent *s = NULL; int found = false; ncbi_ClearErrno(); if(servname == NULL){ ncbi_SetErrno(EINVAL); goto abort; } if( (strcmp( protname, "tcp") != 0) && ( strcmp( protname, "udp") != 0)){ ncbi_SetErrno(EINVAL); goto abort; } /* Make sure the database is closed */ endservent(); while(!found){ s = getservent(); // get the next entry if(s == NULL){ break; // did we reach the end? Guess we return NULL } // Do we have a match on the name? if(strcmp(s->s_name, servname) == 0){ // If protocol is "any" or an exact match, then we're done if( (protname == NULL) || (strcmp(s->s_proto, protname) == 0)){ found = true; } } } abort: endservent(); return(s); }
/* * getservbyport() - looks up a service by its port number */ struct servent *getservbyport(int port, const char *protname) { struct servent *s = NULL; int found = false; ncbi_ClearErrno(); if((strcmp(protname, "tcp") != 0) && (strcmp(protname, "udp") != 0)) { ncbi_SetErrno(EINVAL); goto abort; } /* Make sure the database is closed */ endservent(); while(!found) { s = getservent(); /* get the next entry */ /* did we reach the end? Guess we return NULL */ if(s == NULL) break; /* Do we have a match on the port? */ if(s->s_port == port) { if(protname == NULL) found = true; /* any protocol is okay */ else if(strcmp(s->s_proto, protname) == 0) found = true; /* protocol matches */ } } abort: endservent(); return(s); }
int main (void) { struct servent service; struct servent * retour; char buffer [256]; setservent(0); while(getservent_r(& service, buffer, 256, & retour) == 0) fprintf(stdout, "%s ", service.s_name); endservent(); fprintf(stdout, "\n"); return EXIT_SUCCESS; }
const char *starttls_proto_to_service(const char *app_proto) { struct servent *s; CANON_SERVICE(app_proto); s = getservbyname(app_proto, NULL); if (s != NULL) { return s->s_name; } endservent(); return "443"; }
int tx_open(char *address) { close(_tx_server); address = strdup(address); /* I want to be able to modify it */ char *p = strrchr(address, ':'); if (!p) { /* AF_UNIX address */ struct sockaddr_un sun; sun.sun_family = AF_UNIX; strncpy(sun.sun_path, address, sizeof sun.sun_path); _tx_server = socket(AF_UNIX, SOCK_STREAM, 0); if (_tx_server != -1 && connect(_tx_server, (struct sockaddr *)&sun, sizeof sun) == -1) { close(_tx_server); _tx_server = -1; } } else { /* AF_INET address */ struct sockaddr_in sin; _tx_server = -1; // cheap error handling bzero(&sin, sizeof(sin)); *p++ = 0; struct hostent *host = gethostbyname(address); if (!host) goto fail; bcopy(host->h_addr, &sin.sin_addr, host->h_length); sin.sin_family = host->h_addrtype; struct servent *service = getservbyname(p, "tcp"); sin.sin_port = service? service->s_port: htons(atoi(p)); if (!ntohs(sin.sin_port)) goto fail; _tx_server = socket(AF_INET, SOCK_STREAM, 0); if (_tx_server != -1 && connect(_tx_server, (struct sockaddr *)&sin, sizeof sin) == -1) { close(_tx_server); _tx_server = -1; } fail: endhostent(); endservent(); } free(address); return (_tx_server == -1) ? TX_FAIL : TX_OK; }
static int servent_fill_test_data(struct servent_test_data *td) { struct servent *serv; setservent(1); while ((serv = getservent()) != NULL) { if (servent_test_correctness(serv, NULL) == 0) TEST_DATA_APPEND(servent, td, serv); else return (-1); } endservent(); return (0); }
struct servent * getservbyport(int port, const char *proto) { register struct servent *p; setservent(_serv_stayopen); while (p = getservent()) { if (p->s_port != port) continue; if (proto == 0 || strcmp(p->s_proto, proto) == 0) break; } if (!_serv_stayopen) endservent(); return (p); }
int starttls_proto_to_port(const char *app_proto) { struct servent *s; CANON_SERVICE(app_proto); s = getservbyname(app_proto, NULL); if (s != NULL) { return ntohs(s->s_port); } endservent(); return 443; }
/** desetup parse */ void zonec_desetup_parser(void) { if(parser) { endservent(); endprotoent(); region_destroy(parser->rr_region); /* removed when parser->region(=db->region) is destroyed: * region_recycle(parser->region, (void*)error_dname, 1); * region_recycle(parser->region, (void*)error_domain, 1); */ /* clear memory for exit, but this is not portable to * other versions of lex. yylex_destroy(); */ #ifdef MEMCLEAN /* OS collects memory pages */ yylex_destroy(); #endif } }
int ttd_bind(char *hostname, int port, ttd_server server_type, ttd_conn *conn) { ttd_conn ttd; struct hostent *hp; struct servent *sp; ttd = (ttd_conn) malloc( sizeof( *ttd ) ); if (hostname == NULL) hostname = "localhost"; hp = gethostbyname( hostname ); if (hp == 0) return TTD_UnknownHostnameRC; ttd->addr.sin_family = hp->h_addrtype; bcopy( (char *) hp->h_addr, (char *) &ttd->addr.sin_addr, hp->h_length ); endhostent(); if (port != 0) ttd->addr.sin_port = htons( port ); else { sp = getservbyname( "ttd", "udp" ); if (sp != 0) ttd->addr.sin_port = sp->s_port; else ttd->addr.sin_port = htons( DEFAULTPORT ); } endservent(); ttd->socket = socket( AF_INET, SOCK_DGRAM, 0 ); if (ttd->socket < 0) return TTD_UnixErrorRC; if (connect( ttd->socket, (struct sockaddr *) &(ttd->addr), sizeof( struct sockaddr_in ) ) < 0) return TTD_UnixErrorRC; ttd->server = server_type; ttd->seq = time( (long *) 0 ) * 2; *conn = ttd; return TTD_SuccessRC; }
int main (void) { struct servent *pht; char *pct; int i; setservent (1); while ((pht = getservent ()) != NULL) { printf ("Официальное имя сервиса: %s\n", pht->s_name); printf ("Альтернативные имена:\n"); for (i = 0; (pct = pht->s_aliases [i]) != NULL; i++) { printf (" %s\n", pct); } printf ("Номер порта: %d\n", ntohs ((in_port_t) pht->s_port)); printf ("Имя протокола: %s\n\n", pht->s_proto); } if ((pht = getservbyport (htons ((in_port_t) 21), "udp")) != NULL) { printf ("Официальное имя сервиса: %s\n", pht->s_name); printf ("Альтернативные имена:\n"); for (i = 0; (pct = pht->s_aliases [i]) != NULL; i++) { printf (" %s\n", pct); } printf ("Номер порта: %d\n", ntohs ((in_port_t) pht->s_port)); printf ("Имя протокола: %s\n\n", pht->s_proto); } else { perror ("GETSERVBYPORT"); } if ((pht = getservbyport (htons ((in_port_t) 21), (char *) NULL)) != NULL) { printf ("Официальное имя сервиса: %s\n", pht->s_name); printf ("Альтернативные имена:\n"); for (i = 0; (pct = pht->s_aliases [i]) != NULL; i++) { printf (" %s\n", pct); } printf ("Номер порта: %d\n", ntohs ((in_port_t) pht->s_port)); printf ("Имя протокола: %s\n\n", pht->s_proto); } else { perror ("GETSERVBYPORT"); } endservent (); return 0; }
static int findHTTPhost(struct sockaddr_in *addr, char *hostname, int port) { struct servent *servent; int ret; pthread_mutex_lock(&host_lock); memset(addr,0,sizeof(*addr)); addr->sin_family = AF_INET; if ( port>=0 ) addr->sin_port = htons(port); else if (( servent = getservbyname("http","tcp"))!=NULL ) addr->sin_port = servent->s_port; /* Already in server byteorder */ else addr->sin_port = htons(80); endservent(); ret = findhost(addr,hostname); pthread_mutex_unlock(&host_lock); return( ret ); }
/* libc_hidden_proto(getservbyport_r) */ int getservbyport_r(int port, const char *proto, struct servent * result_buf, char * buf, size_t buflen, struct servent ** result) { int ret; __UCLIBC_MUTEX_LOCK(mylock); setservent(serv_stayopen); while (!(ret=getservent_r(result_buf, buf, buflen, result))) { if (result_buf->s_port != port) continue; if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0) break; } if (!serv_stayopen) endservent(); __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; }
void findservice(int port) { struct servent *sep; char **cpp; int hbo,found=0; setservent(1); while((sep=getservent())!=NULL) { hbo=ntohs(sep->s_port); if (hbo==port) { printf("%d/%s\t%s",hbo,sep->s_proto,sep->s_name); for(cpp=sep->s_aliases;*cpp;++cpp) printf(" %s",*cpp); putchar('\n'); found=1; } } endservent(); if (!found) fprintf(stderr,"%s: port %d has no name on this system.\n",progname,port); } /* void findservice(int port) */
static void test_services (void) { struct servent *sptr; sptr = getservbyname ("domain", "tcp"); output_servent ("getservbyname (\"domain\", \"tcp\")", sptr); sptr = getservbyname ("domain", "udp"); output_servent ("getservbyname (\"domain\", \"udp\")", sptr); sptr = getservbyname ("domain", NULL); output_servent ("getservbyname (\"domain\", NULL)", sptr); sptr = getservbyname ("not-existant", NULL); output_servent ("getservbyname (\"not-existant\", NULL)", sptr); /* This shouldn't return anything. */ sptr = getservbyname ("", ""); output_servent ("getservbyname (\"\", \"\")", sptr); sptr = getservbyname ("", "tcp"); output_servent ("getservbyname (\"\", \"tcp\")", sptr); sptr = getservbyport (htons(53), "tcp"); output_servent ("getservbyport (htons(53), \"tcp\")", sptr); sptr = getservbyport (htons(53), NULL); output_servent ("getservbyport (htons(53), NULL)", sptr); sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */ output_servent ("getservbyport (htons(1), \"udp\")", sptr); setservent (0); do { sptr = getservent (); output_servent ("getservent ()", sptr); } while (sptr != NULL); endservent (); }
/* parse a IP:port pair */ int ctdb_parse_address(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, const char *str, struct ctdb_address *address) { struct servent *se; setservent(0); se = getservbyname("ctdb", "tcp"); endservent(); address->address = talloc_strdup(mem_ctx, str); CTDB_NO_MEMORY(ctdb, address->address); if (se == NULL) { address->port = CTDB_PORT; } else { address->port = ntohs(se->s_port); } return 0; }
/* * services */ static int services(int argc, char *argv[]) { struct servent *se; unsigned long id; char *proto; int i, rv; assert(argc > 1); assert(argv != NULL); #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \ "%-16s %5d/%s", \ se->s_name, ntohs(se->s_port), se->s_proto) setservent(1); rv = RV_OK; if (argc == 2) { while ((se = getservent()) != NULL) SERVICESPRINT; } else { for (i = 2; i < argc; i++) { proto = strchr(argv[i], '/'); if (proto != NULL) *proto++ = '\0'; if (parsenum(argv[i], &id)) se = getservbyport(htons((u_short)id), proto); else se = getservbyname(argv[i], proto); if (se != NULL) SERVICESPRINT; else { rv = RV_NOTFOUND; break; } } } endservent(); return rv; }