From: octo Date: Mon, 15 May 2006 20:56:32 +0000 (+0000) Subject: Implemented better handling of the DATA stuff.. Needs to be implemented still. X-Git-Tag: liboping-0.2.0~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b432731d23e20709a4d36ea797f981c460bad474;p=liboping.git Implemented better handling of the DATA stuff.. Needs to be implemented still. But for now data can be assigned to ping_objs, it's inherited by ping_hosts and can be retrieved by `ping_get_info'. --- diff --git a/src/liboping.c b/src/liboping.c index 3c63521..2892beb 100644 --- a/src/liboping.c +++ b/src/liboping.c @@ -106,6 +106,7 @@ struct pinghost int sequence; struct timeval *timer; double latency; + char *data; void *context; @@ -565,10 +566,7 @@ static int ping_send_one_ipv4 (pingobj_t *obj, pinghost_t *ph) icmp4->icmp_seq = htons (ph->sequence); buflen = 4096 - sizeof (struct icmp); - if (obj->data != NULL) - strncpy (data, obj->data, buflen); - else - strncpy (data, PING_DEF_DATA, buflen); + strncpy (data, ph->data, buflen); datalen = strlen (data); buflen = datalen + sizeof (struct icmp); @@ -609,15 +607,13 @@ static int ping_send_one_ipv6 (pingobj_t *obj, pinghost_t *ph) icmp6->icmp6_type = ICMP6_ECHO_REQUEST; icmp6->icmp6_code = 0; /* The checksum will be calculated by the TCP/IP stack. */ + /* FIXME */ icmp6->icmp6_cksum = 0; icmp6->icmp6_id = htons (ph->ident); icmp6->icmp6_seq = htons (ph->sequence); buflen = 4096 - sizeof (struct icmp6_hdr); - if (obj->data != NULL) - strncpy (data, obj->data, buflen); - else - strncpy (data, PING_DEF_DATA, buflen); + strncpy (data, ph->data, buflen); datalen = strlen (data); buflen = datalen + sizeof (struct icmp6_hdr); @@ -780,6 +776,9 @@ static void ping_free (pinghost_t *ph) if (ph->hostname != NULL) free (ph->hostname); + if (ph->data != NULL) + free (ph->data); + free (ph); } @@ -802,6 +801,7 @@ pingobj_t *ping_construct (void) obj->timeout = PING_DEF_TIMEOUT; obj->ttl = PING_DEF_TTL; obj->addrfamily = PING_DEF_AF; + obj->data = strdup (PING_DEF_DATA); return (obj); } @@ -821,6 +821,9 @@ void ping_destroy (pingobj_t *obj) current = next; } + if (obj->data != NULL) + free (obj->data); + free (obj); return; @@ -942,6 +945,15 @@ int ping_host_add (pingobj_t *obj, const char *host) return (-1); } + /* obj->data is not garuanteed to be != NULL */ + if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL) + { + dprintf ("Out of memory!\n"); + ping_set_error (obj, "strdup", strerror (errno)); + ping_free (ph); + return (-1); + } + if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0) { dprintf ("getaddrinfo failed\n"); @@ -1151,7 +1163,6 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, ret = 0; break; - /* FIXME Return the sequence as an unsigned int */ case PING_INFO_SEQUENCE: ret = ENOMEM; *buffer_len = sizeof (unsigned int); @@ -1169,6 +1180,15 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, *((uint16_t *) buffer) = (uint16_t) iter->ident; ret = 0; break; + + case PING_INFO_DATA: + ret = ENOMEM; + *buffer_len = strlen (iter->data); + if (orig_buffer_len < *buffer_len) + break; + strncpy ((char *) buffer, iter->data, orig_buffer_len); + ret = 0; + break; } return (ret); diff --git a/src/oping.h b/src/oping.h index 7463672..54d0310 100644 --- a/src/oping.h +++ b/src/oping.h @@ -77,6 +77,7 @@ pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter); #define PING_INFO_LATENCY 4 #define PING_INFO_SEQUENCE 5 #define PING_INFO_IDENT 6 +#define PING_INFO_DATA 7 int ping_iterator_get_info (pingobj_iter_t *iter, int info, void *buffer, size_t *buffer_len);