From owner-freebsd-net Fri Sep 14 12:24:19 2001 Delivered-To: freebsd-net@freebsd.org Received: from w250.z064001178.sjc-ca.dsl.cnc.net (w250.z064001178.sjc-ca.dsl.cnc.net [64.1.178.250]) by hub.freebsd.org (Postfix) with SMTP id C300537B411 for ; Fri, 14 Sep 2001 12:23:57 -0700 (PDT) Received: (qmail 50194 invoked by uid 1000); 14 Sep 2001 19:24:19 -0000 Date: Fri, 14 Sep 2001 12:23:57 -0700 From: Jos Backus To: freebsd-net@freebsd.org Subject: How does getsockname() work? Message-ID: <20010914122357.A49323@lizzy.bugworks.com> Reply-To: Jos Backus Mail-Followup-To: freebsd-net@freebsd.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="PEIAKu/WMn1b1Hv9" Content-Disposition: inline User-Agent: Mutt/1.3.22.1i Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline I am seeing the following difference between FreeBSD and Solaris when using the attached program, which uses getsockname(). I am using the following command to test this program: snmpwalk -p 1234 public being either goldenbytes or taiko. On FreeBSD 4 I see: goldenbytes:~/udp% ./udp & [1] 64015 goldenbytes:~/udp% received on 0.0.0.0:0 received 37 bytes from 157.57.212.23:1446 received on 0.0.0.0:0 received 37 bytes from 157.57.212.23:1446 received on 0.0.0.0:0 received 37 bytes from 157.57.212.23:1446 received on 0.0.0.0:0 received 37 bytes from 157.57.212.23:1446 On Solaris 7 I see: taiko:~/udp% ./udp & [1] 21129 taiko:~/udp% received on 0.0.0.0:1234 received 37 bytes from 157.57.212.23:1448 received on 0.0.0.0:1234 received 37 bytes from 157.57.212.23:1448 received on 0.0.0.0:1234 received 37 bytes from 157.57.212.23:1448 received on 0.0.0.0:1234 received 37 bytes from 157.57.212.23:1448 The 0.0.0.0 is caused by using INADDR_ANY as the binding address; what I don't understand is why Solaris shows the portnumber whereas FreeBSD doesn't. Surely I am doing something wrong, but what? Thanks, -- Jos Backus _/ _/_/_/ Santa Clara, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ josb@cncdsl.com _/_/ _/_/_/ use Std::Disclaimer; --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="udp.c" #include #include #include #include #include #include #include #include #define PDU_SIZE 1500 struct sock { int fport; int fsockfd; long sockbuflen; }; static int init_sock(ctx) struct sock *ctx; { struct sockaddr_in local_address; bzero(&local_address, sizeof local_address); local_address.sin_family = AF_INET; local_address.sin_addr.s_addr = htonl(INADDR_ANY); local_address.sin_port = htons(ctx->fport); if ((ctx->fsockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { fprintf(stderr, "socket(): %s\n", strerror(errno)); exit(1); } if (bind(ctx->fsockfd, (struct sockaddr *)&local_address, sizeof local_address) < 0) { fprintf(stderr, "bind(): %s\n", strerror(errno)); exit(1); } } static int receive(ctx) struct sock *ctx; { unsigned char fpdu[PDU_SIZE]; struct sockaddr_in remote_address; struct sockaddr_in local_address; int i, len, n, local_len; while (1) { len = sizeof remote_address; if ((n = recvfrom(ctx->fsockfd, (char *)fpdu, sizeof(fpdu), 0, (struct sockaddr *)&remote_address, &len)) == -1) { fprintf(stderr, "recvfrom(): %s\n", strerror(errno)); exit(1); } if (getsockname(ctx->fsockfd, (struct sockaddr *)&local_address, &local_len) == -1) { fprintf(stderr, "getsockname(): %s\n", strerror(errno)); exit(1); } if (n > PDU_SIZE) { fprintf(stderr, "Warning: %d excess bytes discarded\n", n - PDU_SIZE); n = PDU_SIZE; } if (len != sizeof remote_address) { fprintf(stderr, "recvfrom() return address length %d - expected %d\n", len, sizeof remote_address); exit(1); } fprintf(stderr, "received on %s:%d\n", inet_ntoa(local_address.sin_addr), (int)ntohs(local_address.sin_port)); fprintf(stderr, "received %d bytes from %s:%d\n", n, inet_ntoa(remote_address.sin_addr), (int)ntohs(remote_address.sin_port)); } } int main(int ac, char *av[]) { struct sock s; s.fport = 1234; s.sockbuflen = 65536; init_sock(&s); receive(&s); } --PEIAKu/WMn1b1Hv9-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message