-
Notifications
You must be signed in to change notification settings - Fork 5
/
bird.c
59 lines (56 loc) · 1.92 KB
/
bird.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* This file is part of BIRD-RTRlib-CLI.
*
* BIRD-RTRlib-CLI is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* BIRD-RTRlib-CLI is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BIRD-RTRlib-CLI; see the file COPYING.
*
* written by smlng and Mehmet Ceyran, in cooperation with:
* CST group, Freie Universitaet Berlin
* Website: https://github.com/rtrlib/bird-rtrlib-cli
*/
#include <string.h>
#include <syslog.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "bird.h"
int bird_connect(const char *socket_path)
{
// Result value containing the socket to the BIRD.
int bird_socket = -1;
// Socket address to the BIRD.
struct sockaddr_un addr;
// Check socket path length.
if (strlen(socket_path) >= sizeof addr.sun_path) {
syslog(LOG_EMERG, "Socket path too long");
return -1;
}
// Create socket and bail out on error.
bird_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (bird_socket < 0) {
syslog(LOG_EMERG, "Socket creation error: %m");
return -1;
}
// Create socket address.
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, socket_path);
// Try to connect to BIRD.
if (connect(bird_socket, (struct sockaddr *) &addr, sizeof addr) == -1) {
syslog(LOG_EMERG, "BIRD connection to %s failed: %m", socket_path);
close(bird_socket);
return -1;
}
// Return socket.
return bird_socket;
}