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