(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
fd-manpage-getaddrinfo-client.c
       1  /* Example from getaddrinfo.3 manpage, which has this license:
       2  
       3  Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
       4  and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com>
       5  A few pieces of an earlier version remain:
       6  Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com>
       7  
       8  Permission is granted to make and distribute verbatim copies of this
       9  manual provided the copyright notice and this permission notice are
      10  preserved on all copies.
      11  
      12  Permission is granted to copy and distribute modified versions of this
      13  manual under the conditions for verbatim copying, provided that the
      14  entire resulting derived work is distributed under the terms of a
      15  permission notice identical to this one.
      16  
      17  Since the Linux kernel and libraries are constantly changing, this
      18  manual page may be incorrect or out-of-date.  The author(s) assume no
      19  responsibility for errors or omissions, or for damages resulting from
      20  the use of the information contained herein.  The author(s) may not
      21  have taken the same level of care in the production of this manual,
      22  which is licensed free of charge, as they might when working
      23  professionally.
      24  
      25  Formatted or processed versions of this manual, if unaccompanied by
      26  the source, must acknowledge the copyright and authors of this work.
      27  */
      28  
      29  /* { dg-require-effective-target sockets } */
      30  /* { dg-additional-options "-Wno-analyzer-too-complex" } */
      31  
      32  #include <sys/types.h>
      33  #include <sys/socket.h>
      34  #include <netdb.h>
      35  #include <stdio.h>
      36  #include <stdlib.h>
      37  #include <unistd.h>
      38  #include <string.h>
      39  
      40  #define BUF_SIZE 500
      41  
      42  int
      43  main(int argc, char *argv[])
      44  {
      45    struct addrinfo hints;
      46    struct addrinfo *result, *rp;
      47    int sfd, s;
      48    size_t len;
      49    ssize_t nread;
      50    char buf[BUF_SIZE];
      51  
      52    if (argc < 3) {
      53      fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
      54      exit(EXIT_FAILURE);
      55    }
      56  
      57    /* Obtain address(es) matching host/port. */
      58  
      59    memset(&hints, 0, sizeof(hints));
      60    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
      61    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
      62    hints.ai_flags = 0;
      63    hints.ai_protocol = 0;          /* Any protocol */
      64  
      65    s = getaddrinfo(argv[1], argv[2], &hints, &result);
      66    if (s != 0) {
      67      fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
      68      exit(EXIT_FAILURE);
      69    }
      70  
      71    /* getaddrinfo() returns a list of address structures.
      72       Try each address until we successfully connect(2).
      73       If socket(2) (or connect(2)) fails, we (close the socket
      74       and) try the next address. */
      75  
      76    for (rp = result; rp != NULL; rp = rp->ai_next) {
      77      sfd = socket(rp->ai_family, rp->ai_socktype,
      78  		 rp->ai_protocol);
      79      if (sfd == -1)
      80        continue;
      81  
      82      if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      83        break;                  /* Success */
      84  
      85      close(sfd);
      86    }
      87  
      88    freeaddrinfo(result);           /* No longer needed */
      89  
      90    if (rp == NULL) {               /* No address succeeded */
      91      fprintf(stderr, "Could not connect\n");
      92      exit(EXIT_FAILURE);
      93    }
      94  
      95    /* Send remaining command-line arguments as separate
      96       datagrams, and read responses from server. */
      97  
      98    for (int j = 3; j < argc; j++) {
      99      len = strlen(argv[j]) + 1;
     100      /* +1 for terminating null byte */
     101  
     102      if (len > BUF_SIZE) {
     103        fprintf(stderr,
     104  	      "Ignoring long message in argument %d\n", j);
     105        continue;
     106      }
     107  
     108      if (write(sfd, argv[j], len) != len) {
     109        fprintf(stderr, "partial/failed write\n");
     110        exit(EXIT_FAILURE);
     111      }
     112  
     113      nread = read(sfd, buf, BUF_SIZE);
     114      if (nread == -1) {
     115        perror("read");
     116        exit(EXIT_FAILURE);
     117      }
     118  
     119      printf("Received %zd bytes: %s\n", nread, buf);
     120    }
     121  
     122    exit(EXIT_SUCCESS);
     123  }