Ticket #3884: 0001-resolved-shorthand-ipv4-addresses-for-getaddrinfo-vi.patch

File 0001-resolved-shorthand-ipv4-addresses-for-getaddrinfo-vi.patch, 2.9 KB (added by a-star, 7 years ago)

Modi

  • src/kits/network/netresolv/inet/inet_pton.c

    From a785922c6bfc0d72df36a88b982134ef26bc8bb0 Mon Sep 17 00:00:00 2001
    From: A-star-ayush <myselfthebest@yahoo.com>
    Date: Tue, 7 Mar 2017 21:52:48 +0530
    Subject: [PATCH] resolved shorthand ipv4 addresses for getaddrinfo via
     inet_pton.c
    
    ---
     src/kits/network/netresolv/inet/inet_pton.c   | 13 +++++---
     src/tests/kits/net/netresolv/inet/inet_pton.c | 48 +++++++++++++++++++++++++++
     2 files changed, 56 insertions(+), 5 deletions(-)
     create mode 100644 src/tests/kits/net/netresolv/inet/inet_pton.c
    
    diff --git a/src/kits/network/netresolv/inet/inet_pton.c b/src/kits/network/netresolv/inet/inet_pton.c
    index 0683c6b..0d51cec 100644
    a b static int inet_pton6(const char *src, u_char *dst);  
    6363 */
    6464int
    6565inet_pton(int af, const char *src, void *dst)
    66 {
    67 
     66{       
    6867    switch (af) {
    6968    case AF_INET:
    7069        return (inet_pton4(src, dst, 1));
    inet_pton4(const char *src, u_char *dst, int pton)  
    162161     * the number of parts specified.
    163162     */
    164163    n = pp - parts + 1;
    165     /* inet_pton() takes dotted-quad only.  it does not take shorthand. */
    166     if (pton && n != 4)
    167         return (0);
     164   
     165    /* inet_pton() takes dotted-quad only.  It does not take shorthand. To disable them uncomment the
     166       the 2 lines that follow  */
     167
     168    /*if (pton && n != 4)
     169        return (0);*/
     170   
    168171    switch (n) {
    169172
    170173    case 0:
  • new file src/tests/kits/net/netresolv/inet/inet_pton.c

    diff --git a/src/tests/kits/net/netresolv/inet/inet_pton.c b/src/tests/kits/net/netresolv/inet/inet_pton.c
    new file mode 100644
    index 0000000..dc8922f
    - +  
     1#include <stdio.h>
     2#include <stdlib.h>
     3#include <arpa/inet.h>
     4#include <string.h>
     5#include <assert.h>
     6
     7int main(int argc, char *argv[])
     8{   
     9    char* afi4 = "AF_INET";
     10    char* afi6 = "AF_INET6";
     11    int af = -1;
     12    int rt = 0;
     13    char buf1[INET6_ADDRSTRLEN];
     14    char buf2[INET6_ADDRSTRLEN];
     15
     16    if(argc < 3){
     17        printf("usuage: %s <address_family> <ip_address>\n", argv[0]);
     18        exit(EXIT_FAILURE);
     19    }
     20
     21    if(strcasecmp(afi4, argv[1])==0) af = AF_INET;
     22    else if(strcasecmp(afi6, argv[1])==0) af = AF_INET6;
     23    else{
     24        printf("invalid address family: must either be AF_INET or AF_INET6\n");
     25        exit(EXIT_FAILURE);
     26    }
     27
     28    rt = inet_pton(af, argv[2], buf1);
     29    if(rt==0){
     30        printf("invalid ip address\n");
     31        exit(EXIT_FAILURE);
     32    }
     33    else if(rt < 0){
     34        perror("inet_pton");
     35        exit(EXIT_FAILURE);
     36    }
     37
     38    // the following assertion should hold if there isn't a problem with either of inet_pton and inet_ntop
     39    assert(inet_ntop(af, buf1, buf2, INET6_ADDRSTRLEN)!=NULL);
     40
     41    // The two must be "semantically" similar
     42    // Not exactly similar since ipv6 addresses can be abbreviated (use of "::")
     43    // There also exists multiple classes for ipv4 addresses (a, a.b, a.b.c and a.b.c.d)
     44    printf("input to inet_pton: %s\n", argv[2]);
     45    printf("output of inet_ntop: %s\n", buf2);
     46
     47    return EXIT_SUCCESS;
     48}