1 /* Misc util functions */
3 char const *cvs_id_util
= "$Id: util.c,v 1.14 2006-04-05 01:45:57 bodea Exp $";
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.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 void *shared_malloc(unsigned int size
)
36 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
45 extern int cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
48 pid_t
fork_and_close()
57 if (config
->scheduler_fifo
)
59 struct sched_param params
= {0};
60 params
.sched_priority
= 0;
61 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
63 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
64 LOG(0, 0, 0, "This is probably really really bad.\n");
68 signal(SIGPIPE
, SIG_DFL
);
69 signal(SIGCHLD
, SIG_DFL
);
70 signal(SIGHUP
, SIG_DFL
);
71 signal(SIGUSR1
, SIG_DFL
);
72 signal(SIGQUIT
, SIG_DFL
);
73 signal(SIGKILL
, SIG_DFL
);
74 signal(SIGTERM
, SIG_DFL
);
77 if (clifd
!= -1) close(clifd
);
78 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
79 if (tunfd
!= -1) close(tunfd
);
80 if (udpfd
!= -1) close(udpfd
);
81 if (controlfd
!= -1) close(controlfd
);
82 if (daefd
!= -1) close(daefd
);
83 if (snoopfd
!= -1) close(snoopfd
);
84 if (ifrfd
!= -1) close(ifrfd
);
85 if (ifr6fd
!= -1) close(ifr6fd
);
86 if (rand_fd
!= -1) close(rand_fd
);
87 if (epollfd
!= -1) close(epollfd
);
89 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
93 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
94 if (bgp_peers
[i
].sock
!= -1)
95 close(bgp_peers
[i
].sock
);
101 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
102 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
106 struct cmsghdr
*cmsg
;
110 memset(&msg
, 0, sizeof(msg
));
112 msg
.msg_namelen
= *fromlen
;
120 msg
.msg_control
= cbuf
;
121 msg
.msg_controllen
= sizeof(cbuf
);
123 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
127 *fromlen
= msg
.msg_namelen
;
129 memset(toaddr
, 0, sizeof(*toaddr
));
130 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
132 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
134 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
135 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
143 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
144 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
147 struct cmsghdr
*cmsg
;
149 struct in_pktinfo pktinfo
;
150 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
152 memset(&msg
, 0, sizeof(msg
));
153 msg
.msg_name
= (struct sockaddr
*) to
;
154 msg
.msg_namelen
= tolen
;
156 vec
.iov_base
= (void *) buf
;
162 msg
.msg_control
= cbuf
;
163 msg
.msg_controllen
= sizeof(cbuf
);
165 cmsg
= CMSG_FIRSTHDR(&msg
);
166 cmsg
->cmsg_level
= SOL_IP
;
167 cmsg
->cmsg_type
= IP_PKTINFO
;
168 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
170 memset(&pktinfo
, 0, sizeof(pktinfo
));
171 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
172 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
174 return sendmsg(s
, &msg
, flags
);