#include "config.h" #include #ifdef HAVE_SYS_TYPES_H #include #endif /* HAVE_SYS_TYPES_H */ #ifdef HAVE_STRING_H #include #endif /* HAVE_STRING_H */ #include /* getservbyname */ #include /* tolower */ #include /* atoi */ #include /* inet_addr */ #include /* inet_addr */ #include "common.h" /* ----------------------------------------------------------------- * =NAME= getPort - get port number from string =SYNOPSIS= int getPort(char *pstr, WORD *port) pstr - string which contains port number or service name port - pointer to WORD buffer where port number is stored by network order. =RETURN= TRUE or FALSE FALSE -- specified service name is not registed in /etc/services. =DESCRIPTION= Analizing follow format string, get intefer number. := := | * ----------------------------------------------------------------- */ int getPort(char *pstr, WORD *port) { char c; c = *pstr; if (c >= '0' && c <= '9') { /* If it begin with number, it is port number */ *port = htons(atoi(pstr)); } else { /* or it is service name */ struct servent *se = getservbyname(pstr, 0); if (!se) { fprintf(stderr, "Unknown service: %s\n", pstr); return FALSE; } *port = se->s_port; } return TRUE; } /* ----------------------------------------------------------------- * =NAME= getHost - get IP address from string =SYNOPSIS= int getHost(char *host, DWORD *addr) host - string which contained hostname or IP address addr - pointer to DWORD buffer where IP address is stored by network order. =RETURN= TRUE or FALSE =DESCRIPTION= get IP adress from string. * ----------------------------------------------------------------- */ int getHost(char *host, DWORD *addr) { *addr = inet_addr(host); if (*addr == -1) { struct hostent *he; he = gethostbyname(host); if (!he) { fprintf(stderr, "Can't resolv name: %s\n", host); return FALSE; } *addr = *((DWORD *)(he->h_addr)); } return TRUE; } /* ----------------------------------------------------------------- * =NAME= STRNLOWCMP - compare two strings not more than len characters, case insensitive. =SYNOPSIS= int STRNLOWCMP(char *a, char *b, int len) a, b - string len - compare not more len characters =RETURN= -1 : no match 0 : match * ----------------------------------------------------------------- */ int STRNLOWCMP(char *a, char *b, int len) { while (len) { if (tolower(*a) != *b) return -1; a++; b++; len--; } return 0; } /* ----------------------------------------------------------------- * =NAME= getNetwork - get network number fom string =SYNOPSIS= int getNetwork(char *buf, DWORD *ip) buf - string which contained network number or network length =RETURN= TRUE or FALSE * ----------------------------------------------------------------- */ int getNetwork(char *buf, DWORD *ip) { DWORD netmask; if (STRCHR(buf, '.')) { netmask = htonl(inet_network(buf)); if (netmask == -1) return FALSE; } else { int net_len = atoi(buf); if (net_len < 1 || net_len > 31) return 0; netmask = 0xffffffffl >> (32 - net_len); } *ip = netmask; return TRUE; }