1 /* Misc util functions */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
11 #include <linux/rtnetlink.h>
19 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
21 char *fmtaddr(in_addr_t addr
, int n
)
23 static char addrs
[4][16];
30 return strcpy(addrs
[n
], inet_ntoa(in
));
33 char *fmtMacAddr(uint8_t *pMacAddr
)
35 static char strMAC
[2*ETH_ALEN
];
37 sprintf(strMAC
, "%02X:%02X:%02X:%02X:%02X:%02X",
38 pMacAddr
[0], pMacAddr
[1], pMacAddr
[2],
39 pMacAddr
[3], pMacAddr
[4], pMacAddr
[5]);
44 void *shared_malloc(unsigned int size
)
47 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
56 extern int cluster_sockfd
, tunfd
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
57 extern int pppoediscfd
, pppoesessfd
;
59 extern int udpfd
[MAX_UDPFD
+ 1];
61 pid_t
fork_and_close()
70 if (config
->scheduler_fifo
)
72 struct sched_param params
= {0};
73 params
.sched_priority
= 0;
74 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
76 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
77 LOG(0, 0, 0, "This is probably really really bad.\n");
81 signal(SIGPIPE
, SIG_DFL
);
82 signal(SIGCHLD
, SIG_DFL
);
83 signal(SIGHUP
, SIG_DFL
);
84 signal(SIGUSR1
, SIG_DFL
);
85 signal(SIGQUIT
, SIG_DFL
);
86 signal(SIGKILL
, SIG_DFL
);
87 signal(SIGTERM
, SIG_DFL
);
90 if (clifd
!= -1) close(clifd
);
91 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
92 if (tunfd
!= -1) close(tunfd
);
94 for (i
= 0; i
< config
->nbudpfd
; i
++)
96 if (udpfd
[i
] != -1) close(udpfd
[i
]);
99 if (pppoediscfd
!= -1) close(pppoediscfd
);
100 if (pppoediscfd
!= -1) close(pppoediscfd
);
101 if (controlfd
!= -1) close(controlfd
);
102 if (daefd
!= -1) close(daefd
);
103 if (snoopfd
!= -1) close(snoopfd
);
104 if (rand_fd
!= -1) close(rand_fd
);
105 if (epollfd
!= -1) close(epollfd
);
107 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
111 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
112 if (bgp_peers
[i
].sock
!= -1)
113 close(bgp_peers
[i
].sock
);
119 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
120 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
124 struct cmsghdr
*cmsg
;
128 memset(&msg
, 0, sizeof(msg
));
130 msg
.msg_namelen
= *fromlen
;
138 msg
.msg_control
= cbuf
;
139 msg
.msg_controllen
= sizeof(cbuf
);
141 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
145 *fromlen
= msg
.msg_namelen
;
147 memset(toaddr
, 0, sizeof(*toaddr
));
148 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
150 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
152 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
153 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
161 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
162 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
165 struct cmsghdr
*cmsg
;
167 struct in_pktinfo pktinfo
;
168 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
170 memset(&msg
, 0, sizeof(msg
));
171 msg
.msg_name
= (struct sockaddr
*) to
;
172 msg
.msg_namelen
= tolen
;
174 vec
.iov_base
= (void *) buf
;
180 msg
.msg_control
= cbuf
;
181 msg
.msg_controllen
= sizeof(cbuf
);
183 cmsg
= CMSG_FIRSTHDR(&msg
);
184 cmsg
->cmsg_level
= SOL_IP
;
185 cmsg
->cmsg_type
= IP_PKTINFO
;
186 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
188 memset(&pktinfo
, 0, sizeof(pktinfo
));
189 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
190 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
192 return sendmsg(s
, &msg
, flags
);