[3ec149c] | 1 | // ******************************************************************************************************** |
---|
| 2 | // Servicio: ogAdmBoot |
---|
| 3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla |
---|
| 4 | // Fecha Creación: Julio-2010 |
---|
| 5 | // Fecha Última modificación: Julio-2010 |
---|
| 6 | // Nombre del fichero: ogAdmBoot.cpp |
---|
| 7 | // Descripción :Este fichero implementa el servicio dhcp y tftp propios del sistema |
---|
| 8 | // ******************************************************************************************************** |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <string.h> |
---|
| 12 | #include <ctype.h> |
---|
| 13 | #include <errno.h> |
---|
| 14 | #include <unistd.h> |
---|
| 15 | #include <time.h> |
---|
| 16 | #include <sys/types.h> |
---|
| 17 | #include <sys/socket.h> |
---|
| 18 | #include <netinet/in.h> |
---|
| 19 | #include <arpa/inet.h> |
---|
| 20 | #include </usr/include/mysql/mysql.h> |
---|
| 21 | #include <pthread.h> |
---|
| 22 | #include "Database.h" |
---|
| 23 | #include "ogAdmLib.h" |
---|
| 24 | // _____________________________________________________________________________________________________________ |
---|
| 25 | |
---|
| 26 | #define PUERTODHCPORIGEN 67 |
---|
| 27 | #define PUERTODHCPDESTINO 68 |
---|
| 28 | |
---|
| 29 | #define PUERTOBOOTPORIGEN 4011 |
---|
| 30 | #define PUERTOBOOTPDESTINO 68 |
---|
| 31 | |
---|
| 32 | #define PUERTOTFTPORIGEN 69 |
---|
| 33 | |
---|
| 34 | #define PUERTOMINUSER 20000 |
---|
| 35 | #define PUERTOMAXUSER 60000 |
---|
| 36 | |
---|
| 37 | #define MAX_INTERFACE_LIST 20 |
---|
| 38 | #define MAX_NUM_CSADDRS 20 |
---|
| 39 | // _____________________________________________________________________________________________________________ |
---|
| 40 | #define DHCP_UDP_OVERHEAD (20 + 8 ) // IP header + UDP header |
---|
| 41 | #define DHCP_SNAME_LEN 64 |
---|
| 42 | #define DHCP_FILE_LEN 128 |
---|
| 43 | #define DHCP_FIXED_NON_UDP 236 |
---|
| 44 | #define DHCP_FIXED_LEN (DHCP_FIXED_NON_UDP + DHCP_UDP_OVERHEAD) // Longitud de la trama sin las opciones |
---|
| 45 | #define DHCP_MTU_MAX 1500 |
---|
| 46 | #define DHCP_OPTION_LEN (DHCP_MTU_MAX - DHCP_FIXED_LEN) |
---|
| 47 | |
---|
| 48 | #define BOOTP_MIN_LEN 300 |
---|
| 49 | #define DHCP_MIN_LEN 548 |
---|
| 50 | |
---|
| 51 | struct dhcp_packet { |
---|
| 52 | unsigned char op; // Message opcode |
---|
| 53 | unsigned char htype; // Hardware addr type |
---|
| 54 | unsigned char hlen; // Hardware addr length |
---|
| 55 | unsigned char hops; // Number of relay agent hops from client |
---|
| 56 | unsigned long xid; // Transaction ID |
---|
| 57 | unsigned short secs; // Seconds since client started looking |
---|
| 58 | unsigned short flags; // Flag bits |
---|
| 59 | struct in_addr ciaddr; // Client IP address |
---|
| 60 | struct in_addr yiaddr; // Client IP address |
---|
| 61 | struct in_addr siaddr; // IP address of next server |
---|
| 62 | struct in_addr giaddr; // DHCP relay agent IP address |
---|
| 63 | unsigned char chaddr [16];// Client hardware address |
---|
| 64 | char sname[DHCP_SNAME_LEN]; // Server name |
---|
| 65 | char file[DHCP_FILE_LEN]; // Boot filename |
---|
| 66 | unsigned char magiccookie[4]; |
---|
| 67 | unsigned char options [DHCP_OPTION_LEN-4]; // Optional parameters. |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | // Estructura genrica de una opcin DHCP |
---|
| 71 | struct dhcp_opcion { |
---|
| 72 | unsigned char codop; |
---|
| 73 | unsigned char tam; |
---|
| 74 | unsigned char dato; |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | // Cdigo de las distintas opciones DHCP |
---|
| 78 | #define DHCP_PAD 0 |
---|
| 79 | #define DHCP_SUBNET_MASK 1 |
---|
| 80 | #define DHCP_TIME_OFFSET 2 |
---|
| 81 | #define DHCP_ROUTERS 3 |
---|
| 82 | #define DHCP_TIME_SERVERS 4 |
---|
| 83 | #define DHCP_NAME_SERVERS 5 |
---|
| 84 | #define DHCP_DOMAIN_NAME_SERVERS 6 |
---|
| 85 | #define DHCP_LOG_SERVERS 7 |
---|
| 86 | #define DHCP_COOKIE_SERVERS 8 |
---|
| 87 | #define DHCP_LPR_SERVERS 9 |
---|
| 88 | #define DHCP_IMPRESS_SERVERS 10 |
---|
| 89 | #define DHCP_RESOURCE_LOCATION_SERVERS 11 |
---|
| 90 | #define DHCP_HOST_NAME 12 |
---|
| 91 | #define DHCP_BOOT_SIZE 13 |
---|
| 92 | #define DHCP_MERIT_DUMP 14 |
---|
| 93 | #define DHCP_DOMAIN_NAME 15 |
---|
| 94 | #define DHCP_SWAP_SERVER 16 |
---|
| 95 | #define DHCP_ROOT_PATH 17 |
---|
| 96 | #define DHCP_EXTENSIONS_PATH 18 |
---|
| 97 | #define DHCP_IP_FORWARDING 19 |
---|
| 98 | #define DHCP_NON_LOCAL_SOURCE_ROUTING 20 |
---|
| 99 | #define DHCP_POLICY_FILTER 21 |
---|
| 100 | #define DHCP_MAX_DGRAM_REASSEMBLY 22 |
---|
| 101 | #define DHCP_DEFAULT_IP_TTL 23 |
---|
| 102 | #define DHCP_PATH_MTU_AGING_TIMEOUT 24 |
---|
| 103 | #define DHCP_PATH_MTU_PLATEAU_TABLE 25 |
---|
| 104 | #define DHCP_INTERFACE_MTU 26 |
---|
| 105 | #define DHCP_ALL_SUBNETS_LOCAL 27 |
---|
| 106 | #define DHCP_BROADCAST_ADDRESS 28 |
---|
| 107 | #define DHCP_PERFORM_MASK_DISCOVERY 29 |
---|
| 108 | #define DHCP_MASK_SUPPLIER 30 |
---|
| 109 | #define DHCP_ROUTER_DISCOVERY 31 |
---|
| 110 | #define DHCP_ROUTER_SOLICITATION_ADDRESS 32 |
---|
| 111 | #define DHCP_STATIC_ROUTES 33 |
---|
| 112 | #define DHCP_TRAILER_ENCAPSULATION 34 |
---|
| 113 | #define DHCP_ARP_CACHE_TIMEOUT 35 |
---|
| 114 | #define DHCP_IEEE802_3_ENCAPSULATION 36 |
---|
| 115 | #define DHCP_DEFAULT_TCP_TTL 37 |
---|
| 116 | #define DHCP_TCP_KEEPALIVE_INTERVAL 38 |
---|
| 117 | #define DHCP_TCP_KEEPALIVE_GARBAGE 39 |
---|
| 118 | #define DHCP_NIS_DOMAIN 40 |
---|
| 119 | #define DHCP_NIS_SERVERS 41 |
---|
| 120 | #define DHCP_NTP_SERVERS 42 |
---|
| 121 | #define DHCP_VENDOR_ENCAPSULATED_OPTIONS 43 |
---|
| 122 | #define DHCP_NETBIOS_NAME_SERVERS 44 |
---|
| 123 | #define DHCP_NETBIOS_DD_SERVER 45 |
---|
| 124 | #define DHCP_NETBIOS_NODE_TYPE 46 |
---|
| 125 | #define DHCP_NETBIOS_SCOPE 47 |
---|
| 126 | #define DHCP_FONT_SERVERS 48 |
---|
| 127 | #define DHCP_X_DISPLAY_MANAGER 49 |
---|
| 128 | #define DHCP_REQUESTED_ADDRESS 50 |
---|
| 129 | #define DHCP_LEASE_TIME 51 |
---|
| 130 | #define DHCP_OPTION_OVERLOAD 52 |
---|
| 131 | #define DHCP_MESSAGE_TYPE 53 |
---|
| 132 | #define DHCP_SERVER_IDENTIFIER 54 |
---|
| 133 | #define DHCP_PARAMETER_REQUEST_LIST 55 |
---|
| 134 | #define DHCP_MESSAGE 56 |
---|
| 135 | #define DHCP_MAX_MESSAGE_SIZE 57 |
---|
| 136 | #define DHCP_RENEWAL_TIME 58 |
---|
| 137 | #define DHCP_REBINDING_TIME 59 |
---|
| 138 | #define DHCP_CLASS_IDENTIFIER 60 |
---|
| 139 | #define DHCP_CLIENT_IDENTIFIER 61 |
---|
| 140 | #define DHCP_USER_CLASS_ID 77 |
---|
| 141 | #define DHCP_END 255 |
---|
| 142 | |
---|
| 143 | // DHCP message types. |
---|
| 144 | #define DHCPDISCOVER 1 |
---|
| 145 | #define DHCPOFFER 2 |
---|
| 146 | #define DHCPREQUEST 3 |
---|
| 147 | #define DHCPDECLINE 4 |
---|
| 148 | #define DHCPACK 5 |
---|
| 149 | #define DHCPNAK 6 |
---|
| 150 | #define DHCPRELEASE 7 |
---|
| 151 | #define DHCPINFORM 8 |
---|
| 152 | |
---|
| 153 | // Estructura para trabajar en cada hebra con el cliente en cuestion |
---|
| 154 | struct TramaDhcpBootp{ |
---|
| 155 | SOCKET sck; |
---|
| 156 | struct sockaddr_in cliente; |
---|
| 157 | socklen_t sockaddrsize; |
---|
| 158 | struct dhcp_packet pckDchp; |
---|
| 159 | char bdIP[16]; |
---|
| 160 | }; |
---|
| 161 | // _____________________________________________________________________________________________________________ |
---|
| 162 | |
---|
| 163 | #define MAXBLOCK 4096 |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | // TFTP Cdigos de operacin. |
---|
| 167 | #define TFTPRRQ 1 // Read request. |
---|
| 168 | #define TFTPWRQ 2 // Write request |
---|
| 169 | #define TFTPDATA 3 // Read or write the next block of data. |
---|
| 170 | #define TFTPACK 4 // Confirnacin de bloque procesado |
---|
| 171 | #define TFTPERROR 5 // Error message |
---|
| 172 | #define TFTPOACK 6 // Option acknowledgment |
---|
| 173 | |
---|
| 174 | // Paquete TFTP genrico |
---|
| 175 | struct tftp_packet |
---|
| 176 | { |
---|
| 177 | WORD opcode; |
---|
| 178 | char buffer[MAXBLOCK+2]; |
---|
| 179 | }; |
---|
| 180 | // Paquete TFTP tipo ACK |
---|
| 181 | struct tftppacket_ack |
---|
| 182 | { |
---|
| 183 | WORD opcode; |
---|
| 184 | WORD block; |
---|
| 185 | char buffer[MAXBLOCK]; |
---|
| 186 | }; |
---|
| 187 | // Paquete TFTP tipo ERROR packet |
---|
| 188 | struct tftppacket_error |
---|
| 189 | { |
---|
| 190 | WORD opcode; |
---|
| 191 | WORD errorcode; |
---|
| 192 | char errormessage[508]; |
---|
| 193 | }; |
---|
| 194 | // Estructura para trabajar en cada hebra con el cliente en cuestion |
---|
| 195 | struct TramaTftp{ |
---|
| 196 | SOCKET sck; |
---|
| 197 | struct sockaddr_in cliente; |
---|
| 198 | socklen_t sockaddrsize; |
---|
| 199 | struct tftp_packet pckTftp; |
---|
| 200 | FILE * fileboot; |
---|
| 201 | int bloquesize; |
---|
| 202 | int tsize; |
---|
| 203 | int interval; |
---|
| 204 | int numblock; |
---|
| 205 | unsigned short currentopCode; |
---|
| 206 | }; |
---|
| 207 | //______________________________________________________ |
---|
| 208 | static pthread_mutex_t guardia; // Controla acceso exclusivo de hebras |
---|
| 209 | //______________________________________________________ |
---|
| 210 | char IPlocal[LONPRM]; |
---|
| 211 | char usuario[LONPRM]; |
---|
| 212 | char pasguor[LONPRM]; |
---|
| 213 | char datasource[LONPRM]; |
---|
| 214 | char catalog[LONPRM]; |
---|
| 215 | char router[LONPRM]; |
---|
| 216 | char mascara[LONPRM]; |
---|
| 217 | |
---|
| 218 | char oProuter[LONPRM]; |
---|
| 219 | char oPmascara[LONPRM]; |
---|
| 220 | |
---|
| 221 | // Prototipo de funciones |
---|
| 222 | void RegistraLog(char *,int); |
---|
| 223 | int TomaParametrosReg(); |
---|
| 224 | |
---|
| 225 | LPVOID ServicioDHCP(LPVOID); |
---|
| 226 | LPVOID ServicioBOOTP(LPVOID); |
---|
| 227 | LPVOID ServicioTFTP(LPVOID); |
---|
| 228 | LPVOID GestionaServicioDHCP(LPVOID); |
---|
| 229 | LPVOID GestionaServicioBOOTP(LPVOID); |
---|
| 230 | LPVOID GestionaServicioTFTP(LPVOID); |
---|
| 231 | |
---|
| 232 | int ClienteExistente(struct TramaDhcpBootp *,char*,int); |
---|
| 233 | int OpcionesPresentes(unsigned char *); |
---|
| 234 | unsigned char * BuscaOpcion(dhcp_packet* ,unsigned char ); |
---|
| 235 | |
---|
| 236 | int OpcionPXEClient(dhcp_packet* ); |
---|
| 237 | void ProcesaTramaClientePXE(struct TramaDhcpBootp* trmInfo); |
---|
| 238 | void ProcesaTramaClienteDHCP(struct TramaDhcpBootp* trmInfo); |
---|
| 239 | void ProcesaTramaClienteBOOTP(struct TramaDhcpBootp* trmInfo); |
---|
| 240 | void ProcesaTramaClienteTFTP(struct TramaTftp * trmInfo); |
---|
| 241 | |
---|
| 242 | void RellenaIPCLiente(struct TramaDhcpBootp*); |
---|
| 243 | void RellenaIPServidor(struct TramaDhcpBootp*); |
---|
| 244 | void dhcpDISCOVER_PXE(struct TramaDhcpBootp*); |
---|
| 245 | void dhcpREQUEST_PXE(struct TramaDhcpBootp*); |
---|
| 246 | void bootpREQUEST_PXE(struct TramaDhcpBootp*); |
---|
| 247 | |
---|
| 248 | int PeticionFichero(struct TramaTftp*); |
---|
| 249 | |
---|
| 250 | void AdjDHCPOFFER(unsigned char**,int*); |
---|
| 251 | void AdjDHCPACK(unsigned char**,int*); |
---|
| 252 | void AdjROUTERS(unsigned char** ,int*); |
---|
| 253 | void AdjSUBNETMASK(unsigned char**,int*); |
---|
| 254 | void AdjCLASSIDENTIFIER(unsigned char** ,int*); |
---|
| 255 | void AdjSERVERIDENTIFIER(unsigned char** ,int*); |
---|
| 256 | void AdjLEASETIME(unsigned char** ,int*); |
---|
| 257 | void AdjBOOTSIZE(unsigned char** ,int*); |
---|
| 258 | |
---|
| 259 | SOCKET TomaSocketUser(); |
---|
| 260 | struct tm * TomaHora(); |
---|
| 261 | int split_parametros(char **,char *, char *); |
---|
| 262 | void duerme(unsigned int ); |
---|