Path: blob/main/crypto/krb5/src/tests/misc/test_getsockname.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/misc/test_getsockname.c */2/*3* Copyright (C) 1995 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* test_getsockname.c28*29* This routine demonstrates a bug in the socket emulation library of30* Solaris and other monstrosities that uses STREAMS. On other31* machines with a real networking layer, it prints the local32* interface address that is used to send a message to a specific33* host. On Solaris, it prints out 0.0.0.0.34*/3536#include "autoconf.h"37#include <unistd.h>38#include <stdlib.h>39#include <errno.h>40#include <sys/types.h>41#include <sys/socket.h>42#include <netinet/in.h>43#include <netdb.h>44#include <stdio.h>45#include <string.h>4647int48main(argc, argv)49int argc;50char *argv[];51{52int sock;53GETSOCKNAME_ARG3_TYPE i;54struct hostent *host;55struct sockaddr_in s_sock; /* server address */56struct sockaddr_in c_sock; /* client address */5758char *hostname;5960if (argc == 2) {61hostname = argv[1];62} else {63fprintf(stderr, "Usage: %s hostname\n", argv[0]);64exit(1);65}6667/* Look up server host */68if ((host = gethostbyname(hostname)) == (struct hostent *) 0) {69fprintf(stderr, "%s: unknown host\n", hostname);70exit(1);71}7273/* Set server's address */74(void) memset(&s_sock, 0, sizeof(s_sock));7576memcpy(&s_sock.sin_addr, host->h_addr, sizeof(s_sock.sin_addr));77#ifdef DEBUG78printf("s_sock.sin_addr is %s\n", inet_ntoa(s_sock.sin_addr));79#endif80s_sock.sin_family = AF_INET;81s_sock.sin_port = htons(5555);8283/* Open a socket */84if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {85perror("socket");86exit(1);87}8889memset(&c_sock, 0, sizeof(c_sock));90c_sock.sin_family = AF_INET;9192/* Bind it to set the address; kernel will fill in port # */93if (bind(sock, (struct sockaddr *)&c_sock, sizeof(c_sock)) < 0) {94perror("bind");95exit(1);96}9798/* "connect" the datagram socket; this is necessary to get a local address99properly bound for getsockname() below. */100if (connect(sock, (struct sockaddr *)&s_sock, sizeof(s_sock)) == -1) {101perror("connect");102exit(1);103}104105/* Get my address */106memset(&c_sock, 0, sizeof(c_sock));107i = sizeof(c_sock);108if (getsockname(sock, (struct sockaddr *)&c_sock, &i) < 0) {109perror("getsockname");110exit(1);111}112113printf("My interface address is: %s\n", inet_ntoa(c_sock.sin_addr));114115exit(0);116}117118119