1 /* Misc util functions */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
11 #include <linux/rtnetlink.h>
18 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
20 char *fmtaddr(in_addr_t addr
, int n
)
22 static char addrs
[4][16];
29 return strcpy(addrs
[n
], inet_ntoa(in
));
32 char *fmtMacAddr(uint8_t *pMacAddr
)
34 static char strMAC
[2*ETH_ALEN
];
36 sprintf(strMAC
, "%02X:%02X:%02X:%02X:%02X:%02X",
37 pMacAddr
[0], pMacAddr
[1], pMacAddr
[2],
38 pMacAddr
[3], pMacAddr
[4], pMacAddr
[5]);
43 void *shared_malloc(unsigned int size
)
46 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
55 extern int cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
58 pid_t
fork_and_close()
67 if (config
->scheduler_fifo
)
69 struct sched_param params
= {0};
70 params
.sched_priority
= 0;
71 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
73 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
74 LOG(0, 0, 0, "This is probably really really bad.\n");
78 signal(SIGPIPE
, SIG_DFL
);
79 signal(SIGCHLD
, SIG_DFL
);
80 signal(SIGHUP
, SIG_DFL
);
81 signal(SIGUSR1
, SIG_DFL
);
82 signal(SIGQUIT
, SIG_DFL
);
83 signal(SIGKILL
, SIG_DFL
);
84 signal(SIGTERM
, SIG_DFL
);
87 if (clifd
!= -1) close(clifd
);
88 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
89 if (tunfd
!= -1) close(tunfd
);
90 if (udpfd
!= -1) close(udpfd
);
91 if (controlfd
!= -1) close(controlfd
);
92 if (daefd
!= -1) close(daefd
);
93 if (snoopfd
!= -1) close(snoopfd
);
94 if (rand_fd
!= -1) close(rand_fd
);
95 if (epollfd
!= -1) close(epollfd
);
97 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
101 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
102 if (bgp_peers
[i
].sock
!= -1)
103 close(bgp_peers
[i
].sock
);
109 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
110 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
114 struct cmsghdr
*cmsg
;
118 memset(&msg
, 0, sizeof(msg
));
120 msg
.msg_namelen
= *fromlen
;
128 msg
.msg_control
= cbuf
;
129 msg
.msg_controllen
= sizeof(cbuf
);
131 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
135 *fromlen
= msg
.msg_namelen
;
137 memset(toaddr
, 0, sizeof(*toaddr
));
138 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
140 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
142 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
143 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
151 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
152 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
155 struct cmsghdr
*cmsg
;
157 struct in_pktinfo pktinfo
;
158 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
160 memset(&msg
, 0, sizeof(msg
));
161 msg
.msg_name
= (struct sockaddr
*) to
;
162 msg
.msg_namelen
= tolen
;
164 vec
.iov_base
= (void *) buf
;
170 msg
.msg_control
= cbuf
;
171 msg
.msg_controllen
= sizeof(cbuf
);
173 cmsg
= CMSG_FIRSTHDR(&msg
);
174 cmsg
->cmsg_level
= SOL_IP
;
175 cmsg
->cmsg_type
= IP_PKTINFO
;
176 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
178 memset(&pktinfo
, 0, sizeof(pktinfo
));
179 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
180 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
182 return sendmsg(s
, &msg
, flags
);