* improved nl_open() to bind a sockaddr_nl to the socket
[nlq.git] / netlink.c
bloba8296a004c58016a4870abb031e7e4b5b1dc5712
1 /*
2 * Copyright (C) 2007 Alejandro Mery <amery@geeks.cl>
4 * More information can be found in the files COPYING and README.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 3 of the License. A copy of the
9 * GNU General Public License can be found in the file COPYING.
12 #include "netlink.h"
14 #include <unistd.h> // close()
15 #include <stdio.h> // perror()
17 int nl_open( link_t *self ) {
18 // initialize
19 memset(self, 0x00, sizeof(self));
21 // open the socket
22 if ( (self->fd = socket( AF_NETLINK, SOCK_RAW, NETLINK_ROUTE )) < 0 ) {
23 perror("socket");
24 return -1;
27 // bind it to the address
28 self->sa.nl_family = AF_NETLINK;
29 if ( bind( self->fd, (const struct sockaddr*) &self->sa, sizeof( self->sa ) ) < 0 ) {
30 perror("bind");
31 close( self->fd );
34 return self->fd;
37 inline int nl_close( link_t *self ) {
38 return close( self->fd );