1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* tests/misc/test_getsockname.c */ 3 /* 4 * Copyright (C) 1995 by the Massachusetts Institute of Technology. 5 * All rights reserved. 6 * 7 * Export of this software from the United States of America may 8 * require a specific license from the United States Government. 9 * It is the responsibility of any person or organization contemplating 10 * export to obtain such a license before exporting. 11 * 12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 13 * distribute this software and its documentation for any purpose and 14 * without fee is hereby granted, provided that the above copyright 15 * notice appear in all copies and that both that copyright notice and 16 * this permission notice appear in supporting documentation, and that 17 * the name of M.I.T. not be used in advertising or publicity pertaining 18 * to distribution of the software without specific, written prior 19 * permission. Furthermore if you modify this software you must label 20 * your software as modified software and not distribute it in such a 21 * fashion that it might be confused with the original M.I.T. software. 22 * M.I.T. makes no representations about the suitability of 23 * this software for any purpose. It is provided "as is" without express 24 * or implied warranty. 25 */ 26 27 /* 28 * test_getsockname.c 29 * 30 * This routine demonstrates a bug in the socket emulation library of 31 * Solaris and other monstrosities that uses STREAMS. On other 32 * machines with a real networking layer, it prints the local 33 * interface address that is used to send a message to a specific 34 * host. On Solaris, it prints out 0.0.0.0. 35 */ 36 37 #include "autoconf.h" 38 #include <unistd.h> 39 #include <stdlib.h> 40 #include <errno.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <netinet/in.h> 44 #include <netdb.h> 45 #include <stdio.h> 46 #include <string.h> 47 48 int 49 main(argc, argv) 50 int argc; 51 char *argv[]; 52 { 53 int sock; 54 GETSOCKNAME_ARG3_TYPE i; 55 struct hostent *host; 56 struct sockaddr_in s_sock; /* server address */ 57 struct sockaddr_in c_sock; /* client address */ 58 59 char *hostname; 60 61 if (argc == 2) { 62 hostname = argv[1]; 63 } else { 64 fprintf(stderr, "Usage: %s hostname\n", argv[0]); 65 exit(1); 66 } 67 68 /* Look up server host */ 69 if ((host = gethostbyname(hostname)) == (struct hostent *) 0) { 70 fprintf(stderr, "%s: unknown host\n", hostname); 71 exit(1); 72 } 73 74 /* Set server's address */ 75 (void) memset(&s_sock, 0, sizeof(s_sock)); 76 77 memcpy(&s_sock.sin_addr, host->h_addr, sizeof(s_sock.sin_addr)); 78 #ifdef DEBUG 79 printf("s_sock.sin_addr is %s\n", inet_ntoa(s_sock.sin_addr)); 80 #endif 81 s_sock.sin_family = AF_INET; 82 s_sock.sin_port = htons(5555); 83 84 /* Open a socket */ 85 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 86 perror("socket"); 87 exit(1); 88 } 89 90 memset(&c_sock, 0, sizeof(c_sock)); 91 c_sock.sin_family = AF_INET; 92 93 /* Bind it to set the address; kernel will fill in port # */ 94 if (bind(sock, (struct sockaddr *)&c_sock, sizeof(c_sock)) < 0) { 95 perror("bind"); 96 exit(1); 97 } 98 99 /* "connect" the datagram socket; this is necessary to get a local address 100 properly bound for getsockname() below. */ 101 if (connect(sock, (struct sockaddr *)&s_sock, sizeof(s_sock)) == -1) { 102 perror("connect"); 103 exit(1); 104 } 105 106 /* Get my address */ 107 memset(&c_sock, 0, sizeof(c_sock)); 108 i = sizeof(c_sock); 109 if (getsockname(sock, (struct sockaddr *)&c_sock, &i) < 0) { 110 perror("getsockname"); 111 exit(1); 112 } 113 114 printf("My interface address is: %s\n", inet_ntoa(c_sock.sin_addr)); 115 116 exit(0); 117 } 118