The program below, when run without arguments, prints connect failed, invalid
argumemt. Apparently connect() does not expect to be passed a full sized
sockaddr_storage.

1. This is annoying. The sin_len has already been filled in, etc. Why does it
care if some extra gets passed along?

2. The man page makes no mention of this error. In fact, it advises "namelen
indicates the amount of space pointed to by name, in bytes" which sounds like
it *should* be the full amount.

3. The only documented EINVAL for connect concerns TCP, which this is not, and
multicast addresses, which this is not.


#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/queue.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/wait.h>

#include <signal.h>
#include <syslog.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <unistd.h>
#include <assert.h>
#include <pwd.h>
#include <errno.h>
#include <getopt.h>
#include <stdarg.h>

int
main(int argc, char **argv)
{
        struct sockaddr_storage addr;
        struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
        socklen_t len = sizeof(addr);

        int s = socket(AF_INET, SOCK_DGRAM, 0);
        memset(&addr, 0, len);
        sin->sin_len = sizeof(*sin);
        sin->sin_family = AF_INET;
        sin->sin_port = htons(53);
        inet_aton("8.8.8.8", &sin->sin_addr);

        if (argv[1])
                len = sizeof(*sin);
        if (connect(s, (struct sockaddr *)&addr, len) == -1)
                err(1, "connect failed");
}

Reply via email to