1 /* knocker version 0.8.0 2 * Release date: 28 December 2020 3 * 4 * Project homepage: https://knocker.sourceforge.io 5 * 6 * Copyright 2001,2020 Gabriele Giorgetti <g.giorgetti@gmail.com> 7 * 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #ifndef _KNOCKER_CORE_H_ 25 #define _KNOCKER_CORE_H_ 26 27 #ifdef HAVE_CONFIG_H 28 # include <config.h> 29 #endif 30 31 #ifndef PACKAGE 32 #define PACKAGE "knocker" 33 #endif 34 #ifndef VERSION 35 #define VERSION _KNOCKER_CORE_VERSION_ 36 #endif 37 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 43 #ifdef __WIN32__ /* Use Winsock Version 1.1 */ 44 /* To be linked with -lwsock32 */ 45 #include <windows.h> 46 #include <winsock.h> 47 48 #elif defined __WIN32_WINSOCK2__ /* Use Winsock Version 2.0 */ 49 /* To be linked with -lws2_32 */ 50 #define __WIN32__ 51 #include <windows.h> 52 #include <winsock2.h> 53 54 #else 55 56 #include <netdb.h> 57 #include <sys/types.h> 58 #include <sys/socket.h> 59 #include <netinet/in.h> 60 #include <arpa/inet.h> 61 #include <unistd.h> 62 #include <fcntl.h> 63 64 #endif /* __WIN32__ */ 65 66 67 /* If in trouble .... :-) */ 68 /* #define DEBUG */ 69 /* and run knocker in quiet mode to se debug output only */ 70 71 #ifndef _KNOCKER_CORE_VERSION_ 72 #define _KNOCKER_CORE_VERSION_ "0.8.0" 73 #endif 74 #ifndef _KNOCKER_CORE_RELEASE_DATE_ 75 #define _KNOCKER_CORE_RELEASE_DATE_ "28 December 2020" 76 #endif 77 #ifndef _KNOCKER_CORE_HOMEPAGE_ 78 #define _KNOCKER_CORE_HOMEPAGE_ "https://knocker.sourceforge.io" 79 #endif 80 #ifndef _KNOCKER_CORE_AUTHOR_ 81 #define _KNOCKER_CORE_AUTHOR_ "Gabriele Giorgetti" 82 #endif 83 #ifndef _KNOCKER_CORE_AUTHOR_EMAIL_ 84 #define _KNOCKER_CORE_AUTHOR_EMAIL_ "<g.giorgetti@gmail.com>" 85 #endif 86 87 #define KNOCKER_DEFAULT_PORT_RANGE 1024 /* by default scan from 1 to 1024 */ 88 #define KNOCKER_MAX_PORT_NUMBER 65535 /* max allowed port number */ 89 90 91 #ifndef FALSE 92 enum 93 { FALSE, TRUE }; 94 #endif 95 96 #ifndef EXIT_FAILURE 97 enum 98 { EXIT_SUCCESS, EXIT_FAILURE }; 99 #endif 100 101 enum 102 { PORT_NONE, PORT_CLOSED, PORT_OPEN }; 103 104 enum 105 { PROTO_NONE, PROTO_UDP, PROTO_TCP }; 106 107 #ifdef __WIN32__ 108 #define KNOCKER_SOCKET_ERROR INVALID_SOCKET 109 #else 110 #define KNOCKER_SOCKET_ERROR -1 /* in UNIX socket errors are returned with -1 */ 111 #endif /* __WIN32__ */ 112 113 typedef struct { 114 #ifdef __WIN32__ 115 SOCKET fd; /* Winsocket socket */ 116 #else 117 int fd; /* socket file descriptor */ 118 #endif /* __WIN32__ */ 119 int active; /* if socket is connected or not, TRUE or FALSE */ 120 int protocol; /* protocol PROTO_NONE, PROTO_UDP, PROTO_TCP */ 121 } knocker_core_socket_t; 122 123 typedef struct { 124 unsigned int number; /* port number */ 125 char *service; /* associated service name */ 126 int status; /* port status, PORT_NONE PORT_OPEN or PORT_CLOSED */ 127 int protocol; /* protocol PROTO_NONE, PROTO_UDP, PROTO_TCP */ 128 } knocker_core_port_t; 129 130 /* 131 typedef struct { 132 char *servname; 133 char *servinfo; 134 } knocker_core_services_db_t; 135 */ 136 137 typedef struct { 138 struct hostent *info; /* hostent structure */ 139 struct sockaddr_in sockaddr_in; /* sockaddr_in structure */ 140 char *name; /* hostname string */ 141 char *ip; /* host IP address string */ 142 } knocker_core_host_t; 143 144 typedef struct { 145 knocker_core_socket_t socket; 146 knocker_core_port_t port[KNOCKER_MAX_PORT_NUMBER]; 147 knocker_core_host_t host; 148 } knocker_core_portscan_data_t; 149 150 151 #ifdef __WIN32__ 152 WSADATA knocker_core_wsadata; 153 #endif /* __WIN32__ */ 154 155 #define knocker_socket_t knocker_core_socket_t 156 #define knocker_port_t knocker_core_port_t 157 #define knocker_host_t knocker_core_host_t 158 #define knocker_portscan_data_t knocker_core_portscan_data_t 159 160 static const char knocker_core_version[] = _KNOCKER_CORE_VERSION_; 161 static const char knocker_core_release_date[] = _KNOCKER_CORE_RELEASE_DATE_; 162 static const char knocker_core_homepage[] = _KNOCKER_CORE_HOMEPAGE_; 163 static const char knocker_core_author[] = _KNOCKER_CORE_AUTHOR_; 164 static const char knocker_core_author_email[] = _KNOCKER_CORE_AUTHOR_EMAIL_; 165 166 /* allocated and deallocated within knocker_core_init, knocker_core_quit */ 167 extern char *knocker_core_last_hostip; /* string of the last resolved host ip address */ 168 extern char *knocker_core_last_hostname; /* string of the last used host name */ 169 extern char *knocker_core_last_service; /* string of the last service */ 170 171 172 int knocker_core_init ( void ); 173 void knocker_core_quit ( void ); 174 175 176 int knocker_core_portscan_tcp_connnect (knocker_core_portscan_data_t *data, unsigned int port); 177 178 /* Initialize the knocker_core_portscan_data_t structure data */ 179 int knocker_core_init_portscan_data (knocker_core_portscan_data_t *data); 180 181 /* Frees the the knocker_core_portscan_data_t structure data */ 182 void knocker_core_free_portscan_data (knocker_core_portscan_data_t *data); 183 184 /* Returns FALSE if port is > than KNOCKER_MAX_PORT_NUMBER and < 1 */ 185 int knocker_core_validate_port_number (unsigned int port); 186 187 /* returns host ip address on success, NULL on failure */ 188 char *knocker_core_resolve_host (knocker_core_portscan_data_t *data, const char *hostname); 189 190 /* return the hostname string from the structure */ 191 char *knocker_core_get_hostname (knocker_core_portscan_data_t *data); 192 193 /* return the host ip string from the structure */ 194 char *knocker_core_get_hostip (knocker_core_portscan_data_t *data); 195 196 /* return the service name string using getservbyport */ 197 char *knocker_core_get_service (unsigned int port, int protocol); 198 199 #endif /* _KNOCKER_CORE_H_ */ 200 201