2

I wonder... what is the actual difference if you create a function that has a simple socket parameter and you do your basic instructions inside that function like setting different option to that socket (setsockopt()) and after the functions exist it will remain the option? Or should I make that parameter pointer to that socket in order to keep the actual changes that will happen to the socket.

sctp_enable_events( int socket, int ev_mask )
{
    struct sctp_event_subscribe ev;

    bzero(&ev, sizeof(ev));

    if (ev_mask & SCTP_SNDRCV_INFO_EV)
    ev.sctp_data_io_event = 1;

    /*code */

    if (setsockopt(socket,
           IPPROTO_SCTP,
           SCTP_EVENTS,
           SCTP_SET_EVENTS,
           (const char*)&ev,
           sizeof(ev)) != 0 ) {
      fprintf(where,
          "sctp_enable_event: could not set sctp events errno %d\n",
          errno);
      fflush(where);
      exit(1);
    }
}

Or like this?

 sctp_enable_events( int *socket, int ev_mask, struct sctp_event_subscribe *ev )
    {

        if (ev_mask & SCTP_SNDRCV_INFO_EV)
        ev->sctp_data_io_event = 1;

        /*code */

        if (setsockopt(*socket,
               IPPROTO_SCTP,
               SCTP_EVENTS,
               SCTP_SET_EVENTS,
               ev,
               sizeof(*ev)) != 0 ) {
          fprintf(where,
              "sctp_enable_event: could not set sctp events errno %d\n",
              errno);
          fflush(where);
          exit(1);
        }
    }

I know by passing pointer to struct,int,char etc. you cand modify the values after the function executes and without a pointer the modification will remain local in that function only ,but it will not change it globally.

But how with the setsockopt function?

2 Answers2

1

The setsockopt function can't tell how you come up with it's arguments. Therefore, it cannot act differently. Whether you write:

f(1);

or

int x = 1;
int* xPtr = &x;
f(*xPtr);

is not detectable by f.

This question really has nothing to do with setsockopt. I advise you to strengthen your C knowledge around pointers and values. Without this understanding you are going to make many mistakes in C.

usr
  • 168,620
  • 35
  • 240
  • 369
  • No! The question has to do with setsockopt(). If you call in a function setsockopt() it will remain those settings to that socket available just locally (in that function) or it will affect it globally? In order to affect it globally should i pass it like a pointer or should i pass it like a simple variable? –  Sep 19 '15 at 14:19
  • 2
    You do not have the choice whether to pass in a pointer or a non-pointer. The function signature demands that you pass in a pointer. setsockopt will access memory through that pointer. setsockopt **cannot tell** whether that pointer points to a local variable or to something else. Do you understand that? – usr Sep 19 '15 at 15:11
0

You should generally pass pointers to structs because of the efficiency and because you may want to use those structs (by dereferencing the pointer) in different contexts.

In the case of the socket parameter of the setsockopt(), it is an int, so it is small enough to be passed by value (also the signature of the setsockopt() function requires an int, not a pointer).

As for setsockopt() and other C system API functions, you should lookup its declarations/prototypes and provide the parameters of exactly the same type as the prototype requires (doing the casting if neccessary).

In the case of setsockopt() it would be:

int `setsockopt()` setsockopt(int sockfd, int level, int optname,
                              const void *optval, socklen_t optlen);
syntagma
  • 23,346
  • 16
  • 78
  • 134