The joys of mixing C and C++

Posted by bert hubert Thu, 12 Oct 2006 19:55:00 GMT

Many thanks to my brother who read my previous post and promptly offered to procure new disks for me, they are now in production. Thanks Jaap!

C & C++

One of the things that is easy to forget about C++ is that, while not (really) a superset of C, it does offer the ability to call C functions from C++, and makes some pretty strong statements about the abilty to exchange data between the two languages.

C++ does not come with a set of ‘foundation classes’, and while the “standard template library” is strong on data structures, and algorithms to manipulate them, nothing is offered in the way of network communications infrastructure.

Many attempts have been made to rectify this situation, but these tend to be somewhat heavy handed, or overly complex.

Enter the ComboAddress. This C++ union is laid out in memory just like the venerable struct sockaddr_in, and through its second member, also just like struct sockaddr_in6.

The upshot is that we have a C++ union with interesting methods, that allows us to specify destination addresses, either IPv4 or IPv6, with ease, but that can also be passed to the standard Berkeley C socket functions!

These functions promptly forget they are passed a C++ union, and interpret their argument as a struct sockaddr family member.

For example:

   int sock = socket(AF_INET, SOCK_STREAM, 0);
    ComboAddress ca("127.0.0.1", 6666);
    if (connect(sock, ca) < 0)
            unixDie("connecting to server");

‘unixDie()’ is a simple function that uses strerror to throw a runtime_error with a descriptive error message.

If you are really paying attention, you might have noticed that the ‘connection’ function above is not a real C function, and you would be right. It is a very thin wrapper that saves some typing:

  inline int connect(int fd, const ComboAddress& remote)
  {
          return connect(fd, (struct sockaddr*) &remote, remote.getSocklen());
  }

Another example:

  int fd = accept(sock, &ca);
  if(fd >= 0)
           cout << "Connection from " << ca <<endl;

The tiny bit of code that makes up the ComboAddress can be found in the PowerDNS Recursor source code. I find that it nicely bridges the vast power of the Berkeley sockets API, while taking a lot of the tedium out of calling the host of functions needed to convert between printable IP addresses, port numbers, and the actual stuff the sockets API expects.

And this is all possible because a bunch of guys with serious ‘Unix beards’ decided that C and C++ should remain family members. Thanks!

Posted in , ,  | 7 comments

Comments

  1. big naturals said 225 days later:
    Compilator for such programms like GenEngineCompile is impossible to install, why?
  2. my first sex teacher said 227 days later:
    Can anyone advice normal hosting provider? i don't need free like blogger, i need paid!
  3. teen models said 228 days later:
    Definetely try re-installing apache or try to trace path to server!
  4. tera patrick said 229 days later:
    I find this article useful for both beginners and skilled users, thank you!
  5. bang bus said 230 days later:
    Very nice article, thank you!
  6. we live<sp>together said 232 days later:
    I can assume this is most poweful blog system!
  7. bang bros said 233 days later:
    Very pleased to read this article!

Comments are disabled