[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

connect() question (solaris vs obsd (vs linux))


I have the following code (well, excerpt from), taken from "bindinfo.c" by

I modified the code slightly, so as to allow UID 0 users to send UDP
packets with src port 53.

The code excerpt is as follows.

-- begin excerpt --

    printf("This code is executing as root, assuming source port 53.\n");

    if((sd = socket(AF_INET,SOCK_DGRAM,0)) == -1) {
      perror("Unable to create socket()");

    gethostname(localhost, 128);
    printf("localhost: %s\n", localhost);
    if ((hp = gethostbyname(localhost)) == NULL) {
      perror("Couldn't resolve localhost");

    if(bind(sd,(struct sockaddr *)&sa,sizeof(sa)) == -1) {
      perror("Unable to bind()");
      if(sd != -1) {close(sd);}
    if(connect(sd,(struct sockaddr *)&ra,sizeof(ra)) == -1) {
      perror("Unable to connect()");
      if(sd != -1) {close(sd);}

-- end excerpt --

Basically, the change is that I use bind() to change the source address. I
know I should perhaps use raw sockets, or libnet, but I didn't want to
change the code too much.

The odd thing is this.

On Solaris, this code compiles cleanly, and behaves exactly as I want to.
On OpenBSD, the code compiles cleanly, but still uses random source port.
On Linux, the code compiles cleanly, but I get the error
	Unable to connect(): Invalid argument

Can anyone shine a light on this? Or perhaps, where should I read about
it? I need to make it run on OpenBSD (preferrably) or Linux.


Tor Houghton.