netlink_init.c 2.68 KB
Newer Older
1
/*! \file netlink_init.c
Cedric Roux's avatar
Cedric Roux committed
2
* \brief initiate the netlink socket for communication with nas dirver
3
4
* \author Navid Nikaein and Raymomd Knopp
* \date 2011
Cedric Roux's avatar
Cedric Roux committed
5
* \version 1.0
6
7
* \company Eurecom
* \email: navid.nikaein@eurecom.fr
Cedric Roux's avatar
Cedric Roux committed
8
*/
9
10
11
12
13
14
15
16
17
18

#include <sys/socket.h>
#include <linux/netlink.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
19
20
21
22
23
#include <errno.h>
#include "platform_constants.h"

char nl_rx_buf[NL_MAX_PAYLOAD];

24
struct sockaddr_nl nas_src_addr, nas_dest_addr;
25
26
27
28
struct nlmsghdr *nas_nlh_tx = NULL;
struct nlmsghdr *nas_nlh_rx = NULL;
struct iovec nas_iov_tx;
struct iovec nas_iov_rx = {nl_rx_buf, sizeof(nl_rx_buf)};
29
int nas_sock_fd;
30
31
struct msghdr nas_msg_tx;
struct msghdr nas_msg_rx;
32
33
34

#define GRAAL_NETLINK_ID 31

Cedric Roux's avatar
Cedric Roux committed
35
36
37
38
39
40
int netlink_init(void)
{
    int ret;

    nas_sock_fd = socket(PF_NETLINK, SOCK_RAW,GRAAL_NETLINK_ID);
    if (nas_sock_fd == -1) {
41
        printf("[NETLINK] Error opening socket %d (%d:%s)\n",nas_sock_fd,errno, strerror(errno));
42
//        exit(1);
Cedric Roux's avatar
Cedric Roux committed
43
44
45
    }
    printf("[NETLINK]Opened socket with fd %d\n",nas_sock_fd);

46
#if !defined(ENABLE_PDCP_NETLINK_FIFO)
Cedric Roux's avatar
Cedric Roux committed
47
    ret = fcntl(nas_sock_fd,F_SETFL,O_NONBLOCK);
48
    if (ret == -1) {
49
      printf("[NETLINK] Error fcntl (%d:%s)\n",errno, strerror(errno));
50
//      exit(1);
51
    }
52
#endif
Cedric Roux's avatar
Cedric Roux committed
53

54
55
56
57
58
    memset(&nas_src_addr, 0, sizeof(nas_src_addr));
    nas_src_addr.nl_family = AF_NETLINK;
    nas_src_addr.nl_pid = 1;//getpid();  /* self pid */
    nas_src_addr.nl_groups = 0;  /* not in mcast groups */
    ret = bind(nas_sock_fd, (struct sockaddr*)&nas_src_addr, sizeof(nas_src_addr));
Cedric Roux's avatar
Cedric Roux committed
59
60
61



62
63
64
65
    memset(&nas_dest_addr, 0, sizeof(nas_dest_addr));
    nas_dest_addr.nl_family = AF_NETLINK;
    nas_dest_addr.nl_pid = 0;   /* For Linux Kernel */
    nas_dest_addr.nl_groups = 0; /* unicast */
66
  
67
68
69
70
71
72
73
    // TX PART
    nas_nlh_tx=(struct nlmsghdr *)malloc(NLMSG_SPACE(NL_MAX_PAYLOAD));
    memset(nas_nlh_tx, 0, NLMSG_SPACE(NL_MAX_PAYLOAD));
    /* Fill the netlink message header */
    nas_nlh_tx->nlmsg_len = NLMSG_SPACE(NL_MAX_PAYLOAD);
    nas_nlh_tx->nlmsg_pid = 1;//getpid();  /* self pid */
    nas_nlh_tx->nlmsg_flags = 0;
74
  
75
76
77
78
79
80
81
    nas_iov_tx.iov_base = (void *)nas_nlh_tx;
    nas_iov_tx.iov_len = nas_nlh_tx->nlmsg_len;
    memset(&nas_msg_tx,0,sizeof(nas_msg_tx));
    nas_msg_tx.msg_name = (void *)&nas_dest_addr;
    nas_msg_tx.msg_namelen = sizeof(nas_dest_addr);
    nas_msg_tx.msg_iov = &nas_iov_tx;
    nas_msg_tx.msg_iovlen = 1;
82
83
  
  
84
85
86
87
88
89
    // RX PART
    memset(&nas_msg_rx,0,sizeof(nas_msg_rx));
    nas_msg_rx.msg_name = (void *)&nas_src_addr;
    nas_msg_rx.msg_namelen = sizeof(nas_src_addr);
    nas_msg_rx.msg_iov = &nas_iov_rx;
    nas_msg_rx.msg_iovlen = 1;
Cedric Roux's avatar
Cedric Roux committed
90
91

    return(nas_sock_fd);
92
}