bbf94ba3907898871137132d5b982be05c84dada
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
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
55 extern int pppoediscfd
, pppoesessfd
;
57 extern int udpfd
[MAX_UDPFD
+ 1];
59 pid_t
fork_and_close()
68 if (config
->scheduler_fifo
)
70 struct sched_param params
= {0};
71 params
.sched_priority
= 0;
72 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
74 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
75 LOG(0, 0, 0, "This is probably really really bad.\n");
79 signal(SIGPIPE
, SIG_DFL
);
80 signal(SIGCHLD
, SIG_DFL
);
81 signal(SIGHUP
, SIG_DFL
);
82 signal(SIGUSR1
, SIG_DFL
);
83 signal(SIGQUIT
, SIG_DFL
);
84 signal(SIGKILL
, SIG_DFL
);
85 signal(SIGTERM
, SIG_DFL
);
88 if (clifd
!= -1) close(clifd
);
89 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
90 if (tunfd
!= -1) close(tunfd
);
92 for (i
= 0; i
< config
->nbudpfd
; i
++)
94 if (udpfd
[i
] != -1) close(udpfd
[i
]);
97 if (pppoediscfd
!= -1) close(pppoediscfd
);
98 if (pppoediscfd
!= -1) close(pppoediscfd
);
99 if (controlfd
!= -1) close(controlfd
);
100 if (daefd
!= -1) close(daefd
);
101 if (snoopfd
!= -1) close(snoopfd
);
102 if (rand_fd
!= -1) close(rand_fd
);
103 if (epollfd
!= -1) close(epollfd
);
105 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
109 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
110 if (bgp_peers
[i
].sock
!= -1)
111 close(bgp_peers
[i
].sock
);
117 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
118 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
122 struct cmsghdr
*cmsg
;
126 memset(&msg
, 0, sizeof(msg
));
128 msg
.msg_namelen
= *fromlen
;
136 msg
.msg_control
= cbuf
;
137 msg
.msg_controllen
= sizeof(cbuf
);
139 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
143 *fromlen
= msg
.msg_namelen
;
145 memset(toaddr
, 0, sizeof(*toaddr
));
146 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
148 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
150 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
151 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
159 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
160 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
163 struct cmsghdr
*cmsg
;
165 struct in_pktinfo pktinfo
;
166 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
168 memset(&msg
, 0, sizeof(msg
));
169 msg
.msg_name
= (struct sockaddr
*) to
;
170 msg
.msg_namelen
= tolen
;
172 vec
.iov_base
= (void *) buf
;
178 msg
.msg_control
= cbuf
;
179 msg
.msg_controllen
= sizeof(cbuf
);
181 cmsg
= CMSG_FIRSTHDR(&msg
);
182 cmsg
->cmsg_level
= SOL_IP
;
183 cmsg
->cmsg_type
= IP_PKTINFO
;
184 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
186 memset(&pktinfo
, 0, sizeof(pktinfo
));
187 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
188 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
190 return sendmsg(s
, &msg
, flags
);