diff options
Diffstat (limited to 'src/common')
-rwxr-xr-x | src/common/Makefile.am | 1 | ||||
-rwxr-xr-x | src/common/endianness.c | 89 | ||||
-rwxr-xr-x | src/common/endianness.h | 27 | ||||
-rw-r--r-- | src/common/net.c | 102 | ||||
-rw-r--r-- | src/common/net.h | 37 |
5 files changed, 256 insertions, 0 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index c883e02..391f35a 100755 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -9,6 +9,7 @@ libcommon_la_SOURCES = \ extstr.h extstr.c \ leb128.h leb128.c \ macros.h \ + net.h net.c \ xml.h xml.c libcommon_la_LDFLAGS = $(LIBGTK_LIBS) $(LIBXML_LIBS) diff --git a/src/common/endianness.c b/src/common/endianness.c index 0b7e192..15ca52a 100755 --- a/src/common/endianness.c +++ b/src/common/endianness.c @@ -25,6 +25,7 @@ #include <stdarg.h> +#include <string.h> @@ -323,6 +324,94 @@ bool read_u64(uint64_t *target, const bin_t *data, off_t *pos, off_t len, Source } + + + + + +/****************************************************************************** +* * +* Paramètres : value = source de la valeur à transcrire. * +* size = taille de cette source de données. * +* data = flux de données à modifier. [OUT] * +* pos = position courante dans ce flux. [OUT] * +* len = taille totale des données à analyser. * +* endian = ordre des bits dans la source. * +* * +* Description : Ecrit un nombre non signé sur n octets. * +* * +* Retour : Bilan de l'opération : true en cas de succès, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool _write_un(const bin_t *value, size_t size, bin_t *data, off_t *pos, off_t len, SourceEndian endian) +{ + size_t i; /* Boucle de parcours */ + + if (*pos < 0) return false; + if ((len - *pos) < size) return false; + + switch (endian) + { + case SRE_LITTLE: + +#if __BYTE_ORDER == __LITTLE_ENDIAN + + memcpy(&data[*pos], value, size); + (*pos) += size; + +#elif __BYTE_ORDER == __BIG_ENDIAN + + for (i = 0; i < size; i++, (*pos)++) + *(data + *pos) = *(value + size - i - 1); + +#else + +# error "TODO : PDP !" + +#endif + + break; + + case SRE_MIDDLE: + return false; /* TODO ! */ + break; + + case SRE_BIG: + +#if __BYTE_ORDER == __LITTLE_ENDIAN + + for (i = 0; i < size; i++, (*pos)++) + *(data + *pos) = *(value + size - i - 1); + +#elif __BYTE_ORDER == __BIG_ENDIAN + + memcpy(&data[*pos], value, size); + (*pos) += size; + +#else + +# error "TODO : PDP !" + +#endif + + break; + + } + + return true; + +} + + + + + + + + /****************************************************************************** * * * Paramètres : target = lieu d'enregistrement de la lecture. [OUT] * diff --git a/src/common/endianness.h b/src/common/endianness.h index 0664d6a..e4aca6e 100755 --- a/src/common/endianness.h +++ b/src/common/endianness.h @@ -65,6 +65,33 @@ bool read_u64(uint64_t *, const bin_t *, off_t *, off_t, SourceEndian); #define read_s64(target, data, pos, len, endian) read_u64((uint64_t *)target, data, pos, len, endian) + +/* Ecrit un nombre non signé sur n octets. */ +bool _write_un(const bin_t *, size_t, bin_t *, off_t *, off_t, SourceEndian); + + +#define write_un(value, data, pos, len, endian, type) \ + ({ \ + type __tmp; \ + (void)(value == &__tmp); \ + _write_un((bin_t *)value, sizeof(type), data, pos, len, endian); \ + }) + + +#define write_u8(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint8_t) +#define write_u16(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint16_t) +#define write_u32(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint32_t) +#define write_u64(value, data, pos, len, endian) write_un(value, data, pos, len, endian, uint64_t) + +#define write_s8(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint8_t) +#define write_s16(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint16_t) +#define write_s32(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint32_t) +#define write_s64(value, data, pos, len, endian) write_un(value, data, pos, len, endian, sint64_t) + + + + + /* Lit un nombre hexadécimal non signé sur deux octets. */ bool strtou8(uint8_t *, const char *, size_t *, size_t, SourceEndian); diff --git a/src/common/net.c b/src/common/net.c new file mode 100644 index 0000000..79a5883 --- /dev/null +++ b/src/common/net.c @@ -0,0 +1,102 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * net.c - fonctions complémentaires liées au réseau + * + * Copyright (C) 2010 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * OpenIDA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "net.h" + + +#include <stdio.h> +#include <string.h> +#include <unistd.h> + + + +/****************************************************************************** +* * +* Paramètres : server = nom ou adresse du serveur à contacter. * +* port = port de connexion. * +* addr = éventuelle remontée d'informations. [OUT] * +* * +* Description : Ouvre une connexion TCP à un serveur quelconque. * +* * +* Retour : Flux ouvert en lecture/écriture ou -1 en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int connect_via_tcp(const char *server, const char *port, struct sockaddr_in *addr) +{ + int result; /* Bilan à retourner */ + struct addrinfo hints; /* Type de connexion souhaitée */ + struct addrinfo *infos; /* Informations disponibles */ + int ret; /* Bilan d'un appel */ + struct addrinfo *iter; /* Boucle de parcours */ + + memset(&hints, 0, sizeof(struct addrinfo)); + + hints.ai_family = AF_UNSPEC; /* IPv4 ou IPv6 */ + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = 0; /* N'importe quel protocole */ + + ret = getaddrinfo(server, port, &hints, &infos); + if (ret != 0) + { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); + return -1; + } + + for (iter = infos; iter != NULL; iter = iter->ai_next) + { + result = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol); + if (result == -1) continue; + + ret = connect(result, iter->ai_addr, iter->ai_addrlen); + if (ret == 0) break; + + perror("connect"); + close(result); + + } + + freeaddrinfo(infos); + + if (iter == NULL) return -1; + + if (addr != NULL) + { + ret = getpeername(result, (struct sockaddr *)addr, + (socklen_t []){ sizeof(struct sockaddr_in) }); + + if (ret == -1) + { + perror("getpeername"); + close(result); + return -1; + } + + } + + return result; + +} diff --git a/src/common/net.h b/src/common/net.h new file mode 100644 index 0000000..fbfe6e0 --- /dev/null +++ b/src/common/net.h @@ -0,0 +1,37 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * net.h - prototypes pour les fonctions complémentaires liées au réseau + * + * Copyright (C) 2010 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * OpenIDA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _COMMON_NET_H +#define _COMMON_NET_H + + +#include <netdb.h> + + + +/* Ouvre une connexion TCP à un serveur quelconque. */ +int connect_via_tcp(const char *, const char *, struct sockaddr_in *); + + + +#endif /* _COMMON_NET_H */ |