1 /* Misc util functions */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
17 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
19 char *fmtaddr(in_addr_t addr
, int n
)
21 static char addrs
[4][16];
28 return strcpy(addrs
[n
], inet_ntoa(in
));
31 char *fmtMacAddr(uint8_t *pMacAddr
)
33 static char strMAC
[2*ETH_ALEN
];
35 sprintf(strMAC
, "%02X:%02X:%02X:%02X:%02X:%02X",
36 pMacAddr
[0], pMacAddr
[1], pMacAddr
[2],
37 pMacAddr
[3], pMacAddr
[4], pMacAddr
[5]);
42 void *shared_malloc(unsigned int size
)
45 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
54 extern int cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
57 pid_t
fork_and_close()
66 if (config
->scheduler_fifo
)
68 struct sched_param params
= {0};
69 params
.sched_priority
= 0;
70 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
72 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
73 LOG(0, 0, 0, "This is probably really really bad.\n");
77 signal(SIGPIPE
, SIG_DFL
);
78 signal(SIGCHLD
, SIG_DFL
);
79 signal(SIGHUP
, SIG_DFL
);
80 signal(SIGUSR1
, SIG_DFL
);
81 signal(SIGQUIT
, SIG_DFL
);
82 signal(SIGKILL
, SIG_DFL
);
83 signal(SIGTERM
, SIG_DFL
);
86 if (clifd
!= -1) close(clifd
);
87 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
88 if (tunfd
!= -1) close(tunfd
);
89 if (udpfd
!= -1) close(udpfd
);
90 if (controlfd
!= -1) close(controlfd
);
91 if (daefd
!= -1) close(daefd
);
92 if (snoopfd
!= -1) close(snoopfd
);
93 if (rand_fd
!= -1) close(rand_fd
);
94 if (epollfd
!= -1) close(epollfd
);
96 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
100 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
101 if (bgp_peers
[i
].sock
!= -1)
102 close(bgp_peers
[i
].sock
);
108 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
109 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
113 struct cmsghdr
*cmsg
;
117 memset(&msg
, 0, sizeof(msg
));
119 msg
.msg_namelen
= *fromlen
;
127 msg
.msg_control
= cbuf
;
128 msg
.msg_controllen
= sizeof(cbuf
);
130 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
134 *fromlen
= msg
.msg_namelen
;
136 memset(toaddr
, 0, sizeof(*toaddr
));
137 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
139 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
141 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
142 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
150 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
151 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
154 struct cmsghdr
*cmsg
;
156 struct in_pktinfo pktinfo
;
157 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
159 memset(&msg
, 0, sizeof(msg
));
160 msg
.msg_name
= (struct sockaddr
*) to
;
161 msg
.msg_namelen
= tolen
;
163 vec
.iov_base
= (void *) buf
;
169 msg
.msg_control
= cbuf
;
170 msg
.msg_controllen
= sizeof(cbuf
);
172 cmsg
= CMSG_FIRSTHDR(&msg
);
173 cmsg
->cmsg_level
= SOL_IP
;
174 cmsg
->cmsg_type
= IP_PKTINFO
;
175 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
177 memset(&pktinfo
, 0, sizeof(pktinfo
));
178 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
179 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
181 return sendmsg(s
, &msg
, flags
);