1 // RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s 2 // REQUIRES: !android 3 4 #include <assert.h> 5 #include <errno.h> 6 #include <netdb.h> 7 #include <stdio.h> 8 9 void print_protoent(protoent *curr_entry) { 10 fprintf(stderr, "%s (%d)\n", curr_entry->p_name, curr_entry->p_proto); 11 12 char **aliases = curr_entry->p_aliases; 13 while (char *alias = *aliases++) { 14 fprintf(stderr, " alias %s\n", alias); 15 } 16 } 17 18 void print_all_protoent() { 19 protoent entry; 20 char buf[1024]; 21 protoent *curr_entry; 22 23 while (getprotoent_r(&entry, buf, sizeof(buf), &curr_entry) != ENOENT && curr_entry) { 24 print_protoent(curr_entry); 25 } 26 } 27 28 void print_protoent_by_name(const char *name) { 29 protoent entry; 30 char buf[1024]; 31 protoent *curr_entry; 32 33 int res = getprotobyname_r(name, &entry, buf, sizeof(buf), &curr_entry); 34 assert(!res && curr_entry); 35 print_protoent(curr_entry); 36 } 37 38 void print_protoent_by_num(int num) { 39 protoent entry; 40 char buf[1024]; 41 protoent *curr_entry; 42 43 int res = getprotobynumber_r(num, &entry, buf, sizeof(buf), &curr_entry); 44 assert(!res && curr_entry); 45 print_protoent(curr_entry); 46 } 47 48 int main() { 49 // CHECK: ip (0) 50 // CHECK-NEXT: alias IP 51 // CHECK: ipv6 (41) 52 // CHECK-NEXT: alias IPv6 53 print_all_protoent(); 54 55 // CHECK: rdp (27) 56 // CHECK-NEXT: alias RDP 57 print_protoent_by_name("rdp"); 58 59 // CHECK: udp (17) 60 // CHECK-NEXT: alias UDP 61 print_protoent_by_num(17); 62 return 0; 63 } 64