0

I am trying to code a server using the following example code, but I'm having trouble with these statements in particular.

int Accept(int s, struct sockaddr *addr, socklen_t *addrlen) 
{
     int rc;

     if ((rc = accept(s, addr, addrlen)) < 0)
          unix_error("Accept error");
      return rc;
}
...

socklen_t clientlen = sizeof(struct sockaddr_storage); 
int connectFd = Accept(listenfd, (SA *)&clientaddr, &clientlen)

If I were to use SA, it would pull up an error saying the following:

server.c:175:36: error: ‘SA’ undeclared (first use in this function)  
   connectFd = accept(listenFd, ( SA *  )&clientaddr, &c);  

server.c:175:36: note: each undeclared identifier is reported only once for   each function it appears in   server.c:175:40: error:
expected expression before ‘)’ token  
   connectFd = accept(listenFd, (SA * ) &clientaddr, &c);  
server.c:175:18: error: too few arguments to function ‘accept’  
   connectFd = accept(listenFd, (SA *)&clientaddr, &c);

Is there any way to solve this?

user207421
  • 305,947
  • 44
  • 307
  • 483
user287474
  • 325
  • 1
  • 5
  • 19
  • 3
    I've never seen `SA` as a data type in either POSIX or C standard context. Apparently it's from the `csapp.h` header file. How is it defined? Is it a macro? – cadaniluk May 08 '16 at 10:25
  • Because of the example of the link, clientaddr's datatype is "struct sockaddr_storage". Would casting it be safe? – user287474 May 08 '16 at 10:28
  • Oh I see, let me do some digging. – user287474 May 08 '16 at 10:28
  • [This](http://stackoverflow.com/questions/16010622/reasoning-behind-c-sockets-sockaddr-and-sockaddr-storage) might help. – cadaniluk May 08 '16 at 10:29
  • @Downvoter : I have, also, never heard of it. The op needs to put that information here if he really wish to get some help.. – sjsam May 08 '16 at 10:30
  • jfc, in csapp.h it's "typedef struct sockaddr SA;" Thank you so much for helping me come to that realization, I appreciate it. – user287474 May 08 '16 at 10:31
  • @user287474: yes, it is safe to type-cast a `struct sockaddr_storage*` pointer wherever a `struct sockaddr*` pointer is expected. `sockaddr_storage` is specifically designed for use with `sockaddr`-based APIs. `sockaddr_storage` is large enough in size to hold all known `sockaddr_...` structure types (`sockaddr_in`, `sockaddr_in6`, `sockaddr_un`, etc), where the `ss_family` member specifies which one is being used. – Remy Lebeau May 08 '16 at 14:37

1 Answers1

1

The type SA is not defined in your program, the compiler could not find it, you might want to add following typedef to your program:

typedef struct sockaddr SA;
fluter
  • 13,238
  • 8
  • 62
  • 100