• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't get BSD bind() to work.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't get BSD bind() to work.


  • Subject: Re: Can't get BSD bind() to work.
  • From: Ken Baer <email@hidden>
  • Date: Tue, 4 Oct 2005 17:01:21 -0700

Wow, that fixed it!
You know, I did a LOT of reading of docs and threads in this list, and did not see a mention of this limitation. The code I'm working with is ported from a Windows app that's using WinSock, which does not have this issue. It seems like this would burn a LOT of people porting from WinSock to Unix or Mac. Or, do most Windows coders also stay away from the lower port values?


Thanks again.

On Oct 4, 2005, at 4:27 PM, Josh Graessley wrote:


If you look up error #13 in /usr/include/sys/errno.h, you'll see that it's:

#define    EACCES        13        /* Permission denied */

The number 780 is less than the mystical number 1024. <sarcasm>Clearly
it's unsafe to have a process binding to a low numbered port unless it
runs as root. After all, shouldn't every process listening on the
network run as root at some point or another?</sarcasm>

What you're experiencing is part of the legacy of unix. You can't bind
to a port less than 1024 unless you're running as root. If you can
switch to a higher numbered port, that'd be swell. It will save you
other headaches. With Bonjour, you can bind to 0, the use getsockname to
figure out which port you actually got, then advertise the service using
Bonjour. If you really must use a low numbered port, you have a few
options. You can install a setuid binary, but that gets really messy. It
also leads to a number of potential security holes. MoreAuthSample has a
much better example of how to solve this problem.


The low numbered port thing is, in my personal opinion, stupid beyond
belief.

-josh

On Oct 4, 2005, at 4:11 PM, Ken Baer wrote:


I'm trying to create a simple listening socket in my app with BSD sockets. I am unable to get bind() to work when passing it the port I want to use, 780. It does work if I set the port to 0, but that's not what I want. I made a simple C app that I built in XCode that exhibits this problem. All the sample code examples I've found (which this code comes from) are virtually identical. Is there something I should do using setsockopt() that I'm missing? The following code results in bind() failing, and errno being set to 13.

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

#define MAX_BUF 100

int main(int argc, char* argv[])
{
  int sockd;
  int count;
  struct sockaddr_in serv_name;
  char buf[MAX_BUF];
  int status;

  /* create a socket */
  sockd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockd == -1)
  {
    perror("Socket creation");
    exit(1);
  }

  struct sockaddr_in my_name;

    /* server address  */
  my_name.sin_family = AF_INET;
  my_name.sin_addr.s_addr = INADDR_ANY;
  u_short port = 780;
  my_name.sin_port = htons(port);

  status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));
  int error = errno;
  if (status == -1)
  {
    perror("Binding error");
    exit(1);
  }

  return 0;
}

-Ken Baer.
Hash, Inc.
email@hidden <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list ( email@hidden
<mailto:email@hidden> )
Help/Unsubscribe/Update your Subscription:
jgraessley@apple
.com
jgraessley@appl
e.com>


This email sent to email@hidden <mailto:email@hidden>



-Ken Baer.
Hash, Inc.
email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Can't get BSD bind() to work.
  • Next by Date: How do I change the workgroup of Mac Computer
  • Previous by thread: Re: Can't get BSD bind() to work.
  • Next by thread: How do I change the workgroup of Mac Computer
  • Index(es):
    • Date
    • Thread