Hi,

When disconnect a UDP socket, Linux kernel set local port to zero if the  port
number comes from a implicit bind, but on FreeBSD, it doesn't set
local port to zero.
Here's my test program:

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

#define SERV_PORT 12345

void print_local_addr(int s);

int main(int argc, char** argv)
{
    int sockfd;
    struct sockaddr_in servaddr, cliaddr;

    if (argc != 2) {
        printf("Usage: disconnect_udp <ipaddress>");
        exit(0);
    }

    // creat a UDP socket which binds to a local address
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&cliaddr, sizeof(cliaddr));
    cliaddr.sin_family = AF_INET;
    if (inet_pton(AF_INET, argv[1], &cliaddr.sin_addr) != 1) {
        perror("inet_pton failed");
    }
    bind(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));

    // connect this UDP socket
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) != 1) {
        perror("inet_pton failed");
    }
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
        perror("connect failed");
    }
    print_local_addr(sockfd);

    // disconnect it
    servaddr.sin_family = AF_UNSPEC;
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
        perror("connect failed");
    }
    print_local_addr(sockfd);

    close(sockfd);
}

void print_local_addr(int s)
{
    struct sockaddr_in localaddr;
    socklen_t len = 0;
    char temp[INET_ADDRSTRLEN];

    len = sizeof(localaddr);
    if (getsockname(s, (struct sockaddr *)&localaddr, &len) != 0) {
        perror("getsockname failed");
    }

    inet_ntop(AF_INET, &localaddr.sin_addr, temp, INET_ADDRSTRLEN);
    printf("Local binding: address=%s, port=%d\n",
           temp, ntohs(localaddr.sin_port));
}

In Linux:
Local binding: address=192.168.1.30, port=42610
Local binding: address=192.168.1.30, port=0

In FreeBSD:
Local binding: address=192.168.1.30, port=35133
connect failed: Address family not supported by protocol family
Local binding: address=0.0.0.0, port=0

Any idea? Thank you.

Regards,
Dave.
_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

Reply via email to