/* * This function will read the recived data as a UDP * header structure and copy the relevant data fields into the packet structure * * @param p: Where to copy the data fields. * @param raw: Data stream, expected to be a UDP header at the first 8 bytes. */ void extract_udp_header(struct packet* p, void* raw ) { struct udphdr* head = raw; struct servent* servi; p->source_port = ntohs(head->source); p->destination_port= ntohs(head->dest); p->source_port_str = NULL; p->destination_port_str = NULL; if( p->source_port < 1024) { servi = getservbyport( head->source, "udp"); if(servi) p->source_port_str = servi->s_name; } if( p->destination_port < 1024) { servi = getservbyport( head->dest, "udp"); if(servi) p->destination_port_str = servi->s_name; } }
char * portname(int pr, int port) { static char buf[32]; struct protoent *p = NULL; struct servent *sv = NULL; struct servent *sv1 = NULL; if ((opts & OPT_NORESOLVE) == 0) { if (pr == -1) { if ((sv = getservbyport(htons(port), "tcp"))) { strncpy(buf, sv->s_name, sizeof(buf)-1); buf[sizeof(buf)-1] = '\0'; sv1 = getservbyport(htons(port), "udp"); sv = strncasecmp(buf, sv->s_name, strlen(buf)) ? NULL : sv1; } if (sv) return (buf); } else if ((pr != -2) && (p = getprotobynumber(pr))) { if ((sv = getservbyport(htons(port), p->p_name))) { strncpy(buf, sv->s_name, sizeof(buf)-1); buf[sizeof(buf)-1] = '\0'; return (buf); } } } (void) sprintf(buf, "%d", port); return (buf); }
/* * This function will read the recived data as a TCP * header structure and copy the relevant data fields into the packet structure * * @param p: Where to copy the data fields. * @param raw: Data stream, expected to be a TCP header at the first 20 bytes. */ void extract_tcp_header(struct packet* p, void* raw ) { struct tcphdr* head = raw; struct servent* servi; p->source_port = ntohs(head->source); p->destination_port= ntohs(head->dest); p->source_port_str = NULL; p->destination_port_str = NULL; if( p->source_port < 1024) { servi = getservbyport( head->source, "tcp"); if(servi) p->source_port_str = servi->s_name; } if( p->destination_port < 1024) { servi = getservbyport( head->dest, "tcp"); if(servi) p->destination_port_str = servi->s_name; } snprintf(p->tcp_flags, 12, "[%c%c%c%c%c%c]", (head->urg) ? 'U' : '-', (head->ack) ? 'A' : '-', (head->psh) ? 'P' : '-', (head->rst) ? 'R' : '-', (head->syn) ? 'S' : '-', (head->fin) ? 'F' : '-'); }
void print_report_target(void) { int y, x = 0, acl_flag=0, acl_start = 0, acl_stop = 0; struct servent *service; char buffer[1024]; printf("\nport\t\t\t service\n\n"); for (y = scanstart; y <= scanend; y++, x++) { if (acl_flag) { if ( *(port_state+x) == 0 ) { acl_stop = y; continue; } if ( acl_start+1 == y ) acl_stop = acl_start; sprintf(buffer, "%d - %d", acl_start, acl_stop); printf("%-25s*it could be an ACL*\n", buffer); acl_flag = 0; } if ( (*(port_state+x) == 0) && opt_uncoveracl && (scanmethod == SYNSCAN) ) { acl_start = y; acl_flag = 1; continue; } if ( (*(port_state+x) == 0) && (scanmethod == FINSCAN) ) { service = getservbyport(htons(y), "tcp"); printf("%-25d%s\n", y, (service) ? service->s_name : "unknonw"); } if ( *(port_state+x) == 'S' ) { service = getservbyport(htons(y), "tcp"); printf("%-25d%s\n", y, (service) ? service->s_name : "unknonw"); } } if (acl_flag) { sprintf(buffer, "%d - %d", acl_start, acl_stop); printf("%-25s*it could be an ACL*\n", buffer); } }
static char* port_to_service(int port, unsigned short proto) { struct servent *service; if (proto == IPPROTO_TCP && (service = getservbyport(htons(port), "tcp")) != NULL) return service->s_name; else if (proto == IPPROTO_UDP && (service = getservbyport(htons(port), "udp")) != NULL) return service->s_name; else return (char *) NULL; }
int getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, unsigned int flags) { int port; char *s; struct servent *se; if ((flags & ~NI_MASK) != 0) { return EAI_BADFLAGS; } switch (sa->sa_family) { case PF_INET: #ifdef CYGPKG_NET_INET6 case PF_INET6: #endif if (host != (char *)NULL) { s = _inet_ntop((struct sockaddr *)sa, host, hostlen); if (!s) { return EAI_FAIL; } } if (serv != (char *)NULL) { port = _inet_port((struct sockaddr *)sa); if (!port) { return EAI_FAIL; } se = (struct servent *)NULL; if ((flags & NI_NUMERICSERV) == 0) { if ((flags & NI_DGRAM) == 0) { se = getservbyport(port, "tcp"); } if (se == (struct servent *)NULL) { se = getservbyport(port, "ucp"); } } if (se != (struct servent *)NULL) { sprintf(serv, "%s/%s", se->s_name, se->s_proto); } else { sprintf(serv, "%d", port); } } break; default: return EAI_FAMILY; } return EAI_NONE; }
CAMLprim value unix_getservbyport(value port, value proto) { struct servent * entry; entry = getservbyport(htons(Int_val(port)), String_val(proto)); if (entry == (struct servent *) NULL) raise_not_found(); return alloc_service_entry(entry); }
static char *tcp_service_name(int port) { #ifdef __linux__ int r; struct servent result_buf; struct servent *result; char buf[2048]; r = getservbyport_r(htons(port), "tcp", &result_buf,buf, sizeof(buf), &result); /* ignore ERANGE - if the result can't fit in 2k, just return unknown */ if (r != 0 || result == NULL) return "unknown"; return strdup(result_buf.s_name); #else struct servent *result; result = getservbyport(htons((unsigned short)port), "tcp"); if (result == NULL) return "unknown"; return strdup(result->s_name); #endif }
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; }
static void sctp_print_address(union sctp_sockstore *address, int port, int num_port) { struct servent *sp = 0; char line[80], *cp; int width; switch (address->sa.sa_family) { #ifdef INET case AF_INET: sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr)); break; #endif #ifdef INET6 case AF_INET6: sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr)); break; #endif default: sprintf(line, "%.*s.", Wflag ? 39 : 16, ""); break; } cp = strchr(line, '\0'); if (!num_port && port) sp = getservbyport((int)port, "sctp"); if (sp || port == 0) sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); else sprintf(cp, "%d ", ntohs((u_short)port)); width = Wflag ? 45 : 22; printf("%-*.*s ", width, width, line); }
struct servent *wp_getservbyport (int prot, const char *proto) { struct servent *p; if ((p = getservbyport (prot, proto)) == NULL) wp_warning ("getservbyport() error"); return p; }
char * TnmGetIPPort(Tcl_Interp *interp, char *protocol, struct sockaddr_in *addr) { struct servent *serv; if (strcmp(protocol, "udp") != 0 && strcmp(protocol, "tcp") != 0) { if (interp) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "unknown IP protocol \"", protocol, "\"", (char *) NULL); } return NULL; } serv = getservbyport(addr->sin_port, protocol); if (! serv) { char buffer[20]; sprintf(buffer, "%d", ntohs(addr->sin_port)); if (interp) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "unknown ", protocol, " port \"", buffer, "\"", (char *) NULL); } return NULL; } return serv->s_name; }
PGPServiceEntry * PGPGetServiceByPort( PGPInt32 inPort, const char * inProtocol) { return (PGPServiceEntry *) getservbyport(inPort, inProtocol); }
static int getservicenameinfo(const struct sockaddr *sa, char *service, size_t servicelen, int flags) { int ret = -1; int port = ntohs(((struct sockaddr_in *)sa)->sin_port); if (!(flags & NI_NUMERICSERV)) { struct servent *se = getservbyport( port, (flags & NI_DGRAM) ? "udp" : "tcp"); if (se && se->s_name) { /* Service name looked up successfully. */ ret = snprintf(service, servicelen, "%s", se->s_name); if (ret < 0 || (size_t)ret >= servicelen) { return EAI_MEMORY; } return 0; } /* Otherwise just fall into the numeric service code... */ } ret = snprintf(service, servicelen, "%d", port); if (ret < 0 || (size_t)ret >= servicelen) { return EAI_MEMORY; } return 0; }
/* Get name of TCP service */ char *net_getservbyport(int port) { struct servent *entry; entry = getservbyport(htons((unsigned short) port), "tcp"); return entry == NULL ? NULL : entry->s_name; }
void print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, char *proto) { struct servent *s = getservbyport(p1, proto); p1 = ntohs(p1); p2 = ntohs(p2); printf("port "); if (op == PF_OP_IRG) printf("%u >< %u ", p1, p2); else if (op == PF_OP_XRG) printf("%u <> %u ", p1, p2); else if (op == PF_OP_EQ) { if (s != NULL) printf("= %s ", s->s_name); else printf("= %u ", p1); } else if (op == PF_OP_NE) { if (s != NULL) printf("!= %s ", s->s_name); else printf("!= %u ", p1); } else if (op == PF_OP_LT) printf("< %u ", p1); else if (op == PF_OP_LE) printf("<= %u ", p1); else if (op == PF_OP_GT) printf("> %u ", p1); else if (op == PF_OP_GE) printf(">= %u ", p1); }
static void set_protocol_by_port(char **protocol, int port) { struct servent *srv; srv = getservbyport(htons(port), NULL); if (srv) *protocol = srv->s_name; }
/** Translate the port number into a service name @param[in] Argc The number of arguments @param[in] Argv The argument value array @retval 0 The application exited normally. @retval Other An error occurred. **/ int main ( IN int Argc, IN char **Argv ) { int PortNumber; struct servent * pService; // Determine if the service name is specified if (( 2 != Argc ) || ( 1 != sscanf ( Argv[1], "%d", &PortNumber ))) { Print ( L"%a <port number>\r\n", Argv[0]); } else { // Translate the port number pService = getservbyport ( htons ( PortNumber ), NULL ); if ( NULL == pService ) { Print ( L"ERROR - service not found, errno: %d\r\n", errno ); } else { Print ( L"%a: %d, %a\r\n", pService->s_name, PortNumber, pService->s_proto ); } } // All done return errno; }
int main(void) { struct servent *servent; int port; char proto[] = "tcp"; char name[] = "http"; /* TODO what is a service? PING, HTTP, etc. application layer stuff? How to gete those working then? */ port = 80; servent = getservbyport(port, proto); if (servent == NULL) { fprintf(stderr, "getservbyport(%d, %s) failed\n", port, proto); } else { printf("getservbyport\n"); printf(" s_name = %s\n", servent->s_name); printf(" s_proto = %s\n", servent->s_proto); } servent = getservbyname(name, proto); if (servent == NULL) { fprintf(stderr, "getservbyname(%s, %s) failed\n", name, proto); } else { printf("getservbyname\n"); printf(" s_name = %s\n", servent->s_name); printf(" s_port = %d\n", servent->s_port); printf(" s_proto = %s\n", servent->s_proto); } return EXIT_SUCCESS; }
static const char * at_pr_port(struct sockaddr_at *sat) { static char mybuf[50]; struct servent *serv; switch(sat->sat_port) { case ATADDR_ANYPORT: return("*"); case 0xff: return "????"; default: if (numeric_port) { (void)snprintf(mybuf, sizeof(mybuf), "%d", (unsigned int)sat->sat_port); } else { serv = getservbyport(sat->sat_port, "ddp"); if (serv == NULL) (void)snprintf(mybuf, sizeof(mybuf), "%d", (unsigned int) sat->sat_port); else (void) snprintf(mybuf, sizeof(mybuf), "%s", serv->s_name); } } return mybuf; }
N_NIMCALL(void, getservbyport_511233)(NU16 port, NimStringDesc* proto, Servent509610* Result) { struct servent* s; nimfr("getServByPort", "rawsockets.nim") nimln(261, "rawsockets.nim"); s = getservbyport(((int) (((NI)(NU)(NU16)(((NI16)chckRange(port, ((NI16) -32768), ((NI16) 32767))))))), proto->data); nimln(262, "rawsockets.nim"); { Oserror3433* e_511416; NimStringDesc* LOC5; if (!(s == NIM_NIL)) goto LA3; e_511416 = 0; nimln(2265, "system.nim"); e_511416 = (Oserror3433*) newObj((&NTI116812), sizeof(Oserror3433)); (*e_511416).Sup.Sup.Sup.m_type = (&NTI3433); nimln(2266, "system.nim"); LOC5 = 0; LOC5 = (*e_511416).Sup.Sup.message; (*e_511416).Sup.Sup.message = copyStringRC1(((NimStringDesc*) &TMP10617)); if (LOC5) nimGCunrefNoCycle(LOC5); nimln(262, "rawsockets.nim"); raiseException((Exception*)e_511416, "OSError"); } LA3: ; nimln(263, "rawsockets.nim"); unsureAsgnRef((void**) (&(*Result).name), cstrToNimstr((*s).s_name)); nimln(264, "rawsockets.nim"); unsureAsgnRef((void**) (&(*Result).aliases), cstringarraytoseq_13843((*s).s_aliases)); nimln(265, "rawsockets.nim"); (*Result).port = ((NU16) ((*s).s_port)); nimln(266, "rawsockets.nim"); unsureAsgnRef((void**) (&(*Result).proto), cstrToNimstr((*s).s_proto)); popFrame(); }
int main(int argc, char*argv[]) { // if number of argument is not equal to 5, print error message and exit. if(argc != 5) { printf("%s\n", "ERROR1: This program expects 5 arguments. The program terminates..."); return 0; } //=================================================================== struct hostent *hp, *hp2; struct in_addr addr; hp = gethostbyname(argv[1]); if(!hp) printf("%s\n", "ERROR2: The machine name is NULL..."); else { printf("%s\n",hp->h_name); // should match with argv[1] printf("%d\n",hp->h_length); // should be 4 memcpy(&addr, hp->h_addr, hp->h_length); // copyh_addr to addr printf("%x\n",ntohl(addr.s_addr)); // network to host-long ?? printf("%s\n",inet_ntoa(addr)); // conver addr to ascii ?? } //=================================================================== // hp2 = gethostbyaddr(&addr, 4, AF_INET); { // { doesn't do anything...just here. struct servent *sp, *sp2; sp = getservbyname(argv[2], "tcp"); if(!sp) printf("%s\n", "ERROR2: The service name is NULL..."); else { printf("%s\n",sp->s_name); /* telnet */ printf("%d\n",ntohs(sp->s_port)); /* 23 */ printf("%s\n",sp->s_proto); /* tcp */ } sp2 = getservbyport(htons(atoi(argv[3])), "tcp"); if(!sp2) printf("%s\n", "ERROR3: The port number of service is NULL..."); else printf("%s\n", sp2->s_name); //if argv[3] is 80 then should print http } // } doesn't do anything...just here. //================================================================== addr.s_addr = inet_addr(argv[4]); if(addr.s_addr == INADDR_NONE) printf("%s\n", "ERROR4: The internet number is bad format..."); else hp2 = gethostbyaddr(&addr, 4, AF_INET); if(!hp2) printf("%s\n", "ERROR5: The internet number is NULL..."); else printf("%s\n", hp2->h_name); return 0; };
struct servent* _getservbyport(int port, const char* proto) { struct servent* serv; serv = getservbyport(port, proto); return serv; }
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; }
/* *********************************************************************** */ int NPSL_GetServByPortBasic(int serv_port, const char *serv_proto, NPSL_SERVENT *serv_ent_ptr, void *serv_ent_buffer_ptr, unsigned int serv_ent_buffer_length, unsigned int *required_length, char *error_text) { int return_code = NPSL_SUCCESS; NPSL_SERVENT *tmp_ent_ptr; if (required_length != NULL) *required_length = 0; if ((serv_proto != NULL) && (!(*serv_proto))) { if (error_text != NULL) strcpy(error_text, "The 'serv_proto' parameter is not 'NULL', but is an empty string."); return_code = NPSL_FAILURE; } else if ((return_code = NPSL_CheckServEntParams(serv_ent_ptr, serv_ent_buffer_ptr, serv_ent_buffer_length, error_text)) != NPSL_SUCCESS) ; else { int tmp_port = COMPAT_CAST_static(int, ntohs(COMPAT_CAST_static(unsigned short, serv_port))); #if NPSL_HAS_GETSERVBYPORT_R if ((tmp_ent_ptr = getservbyport_r(tmp_port, serv_proto, serv_ent_ptr, serv_ent_buffer_ptr, ((int) serv_ent_buffer_length))) == NULL) { if (error_text != NULL) { sprintf(error_text, "%s '%s()' for service port '%d', protocol '%-.127s': ", "Unable to get service entry with", "getservbyport_r", serv_port, (serv_proto != NULL) ? serv_proto : "*NULL*"); NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text); } return_code = NPSL_SUPP_MapLastError(); } #else if ((tmp_ent_ptr = getservbyport(tmp_port, serv_proto)) == NULL) { if (error_text != NULL) { sprintf(error_text, "%s '%s()' for service port '%d', protocol '%-.127s': ", "Unable to get service entry with", "getservbyport", serv_port, (serv_proto != NULL) ? serv_proto : "*NULL*"); NPSL_AppendLastErrorString(0, NPSL_MAX_ERROR_TEXT, error_text); } return_code = NPSL_SUPP_MapLastError(); } else return_code = NPSL_CopyServEntFlatBasic(tmp_ent_ptr, serv_ent_ptr, serv_ent_buffer_ptr, serv_ent_buffer_length, required_length, error_text); #endif // #if NPSL_HAS_GETSERVBYPORT_R } return(return_code); }
Variant HHVM_FUNCTION(getservbyport, int64_t port, const String& protocol) { Lock lock(NetworkMutex); struct servent *serv = getservbyport(htons(port), protocol.data()); if (serv == NULL) { return false; } return String(serv->s_name, CopyString); }
static const char *port_name(unsigned port) { struct servent *server; server = getservbyport(htons(port), NULL); if (server) return server->s_name; return "unknown"; }
Variant f_getservbyport(int port, CStrRef protocol) { Lock lock(NetworkMutex); struct servent *serv = getservbyport(htons(port), protocol.data()); if (serv == NULL) { return false; } return String(serv->s_name, CopyString); }
/** * sim_db_insert_host_service: * * Insert 'host service' in @databse */ void sim_db_insert_host_service (SimDatabase *database, SimInet *inet, gchar *date, gint port, gint protocol, SimInet *sensor, gchar *interface, gchar *service, gchar *application, SimUuid *context_id) { gchar *query; gint plugin_id; struct servent *temp_serv = NULL; struct protoent *temp_proto = NULL; g_return_if_fail (SIM_IS_DATABASE (database)); g_return_if_fail (SIM_IS_INET (inet)); g_return_if_fail (date); g_return_if_fail (port >= 0); /* Needed for ints */ g_return_if_fail (protocol >= 0); g_return_if_fail (sensor); g_return_if_fail (service); g_return_if_fail (application); temp_proto = getprotobynumber (protocol); if (temp_proto->p_name == NULL) return; /* Since we don't know the proto we wont insert a service without a protocol */ temp_serv = getservbyport (port, temp_proto->p_name); query = g_strdup_printf ("INSERT INTO host_services " "(id, date, port, protocol, service, service_type, version, origin, sensor, interface, ctx) " "SELECT id, '%s', %u, %u, '%s', '%s', '%s', 0, %s, '%s', %s " "FROM host WHERE ip = %s and ctx = %s LIMIT 1", date, port, protocol, (temp_serv != NULL) ? temp_serv->s_name : "unknown", service, application, sim_inet_get_db_string (sensor), interface, sim_uuid_get_db_string (context_id), sim_inet_get_db_string (inet), sim_uuid_get_db_string (context_id)); sim_db_execute_query (database, query); g_free (query); plugin_id = SIM_PLUGIN_SERVICE; sim_db_insert_host_plugin_sid (database, inet, plugin_id, port, context_id); }
static char * port_to_service(int port) { struct servent *service; if ((service = getservbyport(htons(port), "udp"))) return service->s_name; return NULL; }