github.com/afumu/libc@v0.0.6/musl/src/network/lookup_serv.c (about)

     1  #include <sys/socket.h>
     2  #include <netinet/in.h>
     3  #include <netdb.h>
     4  #include <ctype.h>
     5  #include <string.h>
     6  #include <stdlib.h>
     7  #include <fcntl.h>
     8  #include <errno.h>
     9  #include "lookup.h"
    10  #include "stdio_impl.h"
    11  
    12  int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags)
    13  {
    14  	char line[128];
    15  	int cnt = 0;
    16  	char *p, *z = "";
    17  	unsigned long port = 0;
    18  
    19  	switch (socktype) {
    20  	case SOCK_STREAM:
    21  		switch (proto) {
    22  		case 0:
    23  			proto = IPPROTO_TCP;
    24  		case IPPROTO_TCP:
    25  			break;
    26  		default:
    27  			return EAI_SERVICE;
    28  		}
    29  		break;
    30  	case SOCK_DGRAM:
    31  		switch (proto) {
    32  		case 0:
    33  			proto = IPPROTO_UDP;
    34  		case IPPROTO_UDP:
    35  			break;
    36  		default:
    37  			return EAI_SERVICE;
    38  		}
    39  	case 0:
    40  		break;
    41  	default:
    42  		if (name) return EAI_SERVICE;
    43  		buf[0].port = 0;
    44  		buf[0].proto = proto;
    45  		buf[0].socktype = socktype;
    46  		return 1;
    47  	}
    48  
    49  	if (name) {
    50  		if (!*name) return EAI_SERVICE;
    51  		port = strtoul(name, &z, 10);
    52  	}
    53  	if (!*z) {
    54  		if (port > 65535) return EAI_SERVICE;
    55  		if (proto != IPPROTO_UDP) {
    56  			buf[cnt].port = port;
    57  			buf[cnt].socktype = SOCK_STREAM;
    58  			buf[cnt++].proto = IPPROTO_TCP;
    59  		}
    60  		if (proto != IPPROTO_TCP) {
    61  			buf[cnt].port = port;
    62  			buf[cnt].socktype = SOCK_DGRAM;
    63  			buf[cnt++].proto = IPPROTO_UDP;
    64  		}
    65  		return cnt;
    66  	}
    67  
    68  	if (flags & AI_NUMERICSERV) return EAI_NONAME;
    69  
    70  	size_t l = strlen(name);
    71  
    72  	abort(); //TODO-
    73  	// unsigned char _buf[1032];
    74  	// FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
    75  	// if (!f) switch (errno) {
    76  	// case ENOENT:
    77  	// case ENOTDIR:
    78  	// case EACCES:
    79  	// 	return EAI_SERVICE;
    80  	// default:
    81  	// 	return EAI_SYSTEM;
    82  	// }
    83  
    84  	abort(); //TODO-
    85  	// while (fgets(line, sizeof line, f) && cnt < MAXSERVS) {
    86  	// 	if ((p=strchr(line, '#'))) *p++='\n', *p=0;
    87  
    88  	// 	/* Find service name */
    89  	// 	for(p=line; (p=strstr(p, name)); p++) {
    90  	// 		if (p>line && !isspace(p[-1])) continue;
    91  	// 		if (p[l] && !isspace(p[l])) continue;
    92  	// 		break;
    93  	// 	}
    94  	// 	if (!p) continue;
    95  
    96  	// 	/* Skip past canonical name at beginning of line */
    97  	// 	for (p=line; *p && !isspace(*p); p++);
    98  
    99  	// 	port = strtoul(p, &z, 10);
   100  	// 	if (port > 65535 || z==p) continue;
   101  	// 	if (!strncmp(z, "/udp", 4)) {
   102  	// 		if (proto == IPPROTO_TCP) continue;
   103  	// 		buf[cnt].port = port;
   104  	// 		buf[cnt].socktype = SOCK_DGRAM;
   105  	// 		buf[cnt++].proto = IPPROTO_UDP;
   106  	// 	}
   107  	// 	if (!strncmp(z, "/tcp", 4)) {
   108  	// 		if (proto == IPPROTO_UDP) continue;
   109  	// 		buf[cnt].port = port;
   110  	// 		buf[cnt].socktype = SOCK_STREAM;
   111  	// 		buf[cnt++].proto = IPPROTO_TCP;
   112  	// 	}
   113  	// }
   114  	// __fclose_ca(f);
   115  	// return cnt > 0 ? cnt : EAI_SERVICE;
   116  	abort(); //TODO-
   117  }