0

I am creating a socket with the parameters as below-

fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

But for some reason socket creation is failing and fd is set to -1.

  1. I wanted to understand for what and all reason can socket creation get failed?
  2. How do I figure out for what reason the socket creation would be failing in my case?

Note: And this is consistently reproducible, we retry the socket creation for consecutive 5 times with a gap of 500msec and it fails all the 5 times.

Darshan L
  • 824
  • 8
  • 30

1 Answers1

1

As Eugene Sh / WeatherVane say, to see the error :

#include <errno.h>
#include <stdio.h>
#include <string.h>
...
fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
if (fd == -1) {
  printf("error cannot create socket, errno = %d : %s\n",
         errno, strerror(errno));
  ...return/exit etc
}
...

Am I wrong or the domain must be PF_INET, then socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); must be socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); ?

sctp(7) - Linux man page

Edit : under raspbian (Linux raspberrypi 4.14.79) I can do both


Is the error also occurs if you run as root ? Specially if you are under Android ( see what can cause a socket() “Permission denied” error? )

bruno
  • 32,421
  • 7
  • 25
  • 37
  • @EugeneSh. oh yes, I forget it, I always grep in the includes files to find the name of the error from its number ^^ – bruno Jan 28 '19 at 18:24
  • 1
    It is `strerror( errno )` that provides the descriptive message. – Weather Vane Jan 28 '19 at 18:26
  • @WeatherVane it's too practical, it makes you want to make mistakes just to use it lol – bruno Jan 28 '19 at 18:27
  • Thank you @bruno. I just checked for the error type. I get EACCES (13) - Permission denied. For what reason would the socket creation fails for "permission denied" cause? – Darshan L Jan 29 '19 at 04:48
  • Furthermore, I tried changing the family type to PF_INET as well; still the same error. – Darshan L Jan 29 '19 at 05:51
  • @DarshanL may be your OS doesn't support SCTP protocol ? On which OS are you ? – bruno Jan 29 '19 at 08:57
  • This is proprietary OS built on top of Linux kernel. I have some more data. What I see is, if there is another configuration set then socket creation is failing. If we remove it then it works. Looks like it is holding up some resource and causing this problem. But don't have clue on what dependant resource that the other configuration would be holding up – Darshan L Jan 29 '19 at 11:45
  • @DarshanL I hope you are not limited to only one socket (`ulimit -a`) ^^ – bruno Jan 29 '19 at 11:47
  • @bruno: Figured out that the sctp stack was not initialised, yet; hence socket creation was failing. – Darshan L Jan 30 '19 at 06:17