d4dbd4f3fecb59c43f3fa47726ed1c75efb59b70
[l2tpns.git] / util.c
1 /* Misc util functions */
2
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sched.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <string.h>
10 #include <sys/mman.h>
11
12 #include "l2tpns.h"
13 #ifdef BGP
14 #include "bgp.h"
15 #endif
16
17 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
18 // to use
19 char *fmtaddr(in_addr_t addr, int n)
20 {
21 static char addrs[4][16];
22 struct in_addr in;
23
24 if (n < 0 || n >= 4)
25 return "";
26
27 in.s_addr = addr;
28 return strcpy(addrs[n], inet_ntoa(in));
29 }
30
31 char *fmtMacAddr(uint8_t *pMacAddr)
32 {
33 static char strMAC[2*ETH_ALEN];
34
35 sprintf(strMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
36 pMacAddr[0], pMacAddr[1], pMacAddr[2],
37 pMacAddr[3], pMacAddr[4], pMacAddr[5]);
38
39 return strMAC;
40 }
41
42 void *shared_malloc(unsigned int size)
43 {
44 void * p;
45 p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
46
47 if (p == MAP_FAILED)
48 p = NULL;
49
50 return p;
51 }
52
53 extern int forked;
54 extern int cluster_sockfd, tunfd, udpfd, controlfd, daefd, snoopfd, ifrfd, ifr6fd, rand_fd;
55 extern int *radfds;
56
57 pid_t fork_and_close()
58 {
59 pid_t pid = fork();
60 int i;
61
62 if (pid)
63 return pid;
64
65 forked++;
66 if (config->scheduler_fifo)
67 {
68 struct sched_param params = {0};
69 params.sched_priority = 0;
70 if (sched_setscheduler(0, SCHED_OTHER, &params))
71 {
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");
74 }
75 }
76
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);
84
85 // Close sockets
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);
95
96 for (i = 0; radfds && i < RADIUS_FDS; i++)
97 close(radfds[i]);
98
99 #ifdef BGP
100 for (i = 0; i < BGP_NUM_PEERS; i++)
101 if (bgp_peers[i].sock != -1)
102 close(bgp_peers[i].sock);
103 #endif /* BGP */
104
105 return pid;
106 }
107
108 ssize_t recvfromto(int s, void *buf, size_t len, int flags,
109 struct sockaddr *from, socklen_t *fromlen, struct in_addr *toaddr)
110 {
111 ssize_t r;
112 struct msghdr msg;
113 struct cmsghdr *cmsg;
114 struct iovec vec;
115 char cbuf[128];
116
117 memset(&msg, 0, sizeof(msg));
118 msg.msg_name = from;
119 msg.msg_namelen = *fromlen;
120
121 vec.iov_base = buf;
122 vec.iov_len = len;
123 msg.msg_iov = &vec;
124 msg.msg_iovlen = 1;
125 msg.msg_flags = 0;
126
127 msg.msg_control = cbuf;
128 msg.msg_controllen = sizeof(cbuf);
129
130 if ((r = recvmsg(s, &msg, flags)) < 0)
131 return r;
132
133 if (fromlen)
134 *fromlen = msg.msg_namelen;
135
136 memset(toaddr, 0, sizeof(*toaddr));
137 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
138 {
139 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO)
140 {
141 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
142 memcpy(toaddr, &i->ipi_addr, sizeof(*toaddr));
143 break;
144 }
145 }
146
147 return r;
148 }
149
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)
152 {
153 struct msghdr msg;
154 struct cmsghdr *cmsg;
155 struct iovec vec;
156 struct in_pktinfo pktinfo;
157 char cbuf[CMSG_SPACE(sizeof(pktinfo))];
158
159 memset(&msg, 0, sizeof(msg));
160 msg.msg_name = (struct sockaddr *) to;
161 msg.msg_namelen = tolen;
162
163 vec.iov_base = (void *) buf;
164 vec.iov_len = len;
165 msg.msg_iov = &vec;
166 msg.msg_iovlen = 1;
167 msg.msg_flags = 0;
168
169 msg.msg_control = cbuf;
170 msg.msg_controllen = sizeof(cbuf);
171
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));
176
177 memset(&pktinfo, 0, sizeof(pktinfo));
178 memcpy(&pktinfo.ipi_spec_dst, from, sizeof(*from));
179 memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
180
181 return sendmsg(s, &msg, flags);
182 }