]>
Commit | Line | Data |
---|---|---|
9360d9b7 EM |
1 | /* |
2 | * Copyright (c) 2012 Ed Maste. All rights reserved. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "route-table.h" | |
20 | ||
21 | #include <sys/socket.h> | |
22 | #include <sys/types.h> | |
23 | ||
24 | #include <net/if.h> | |
25 | #include <net/route.h> | |
26 | #include <net/if_dl.h> | |
27 | #include <netinet/in.h> | |
28 | ||
29 | #include <string.h> | |
30 | #include <unistd.h> | |
31 | ||
47cd84a8 EM |
32 | #include "vlog.h" |
33 | ||
9360d9b7 EM |
34 | VLOG_DEFINE_THIS_MODULE(route_table); |
35 | ||
36 | static int pid; | |
37 | static unsigned int register_count = 0; | |
38 | ||
39 | bool | |
40 | route_table_get_name(ovs_be32 ip, char name[IFNAMSIZ]) | |
41 | { | |
42 | struct { | |
43 | struct rt_msghdr rtm; | |
44 | char space[512]; | |
45 | } rtmsg; | |
46 | ||
47 | struct rt_msghdr *rtm = &rtmsg.rtm; | |
48 | struct sockaddr_dl *ifp = NULL; | |
49 | struct sockaddr_in *sin; | |
50 | struct sockaddr *sa; | |
51 | static int seq; | |
52 | int i, len, namelen, rtsock; | |
53 | ||
54 | rtsock = socket(PF_ROUTE, SOCK_RAW, 0); | |
55 | if (rtsock < 0) | |
56 | return false; | |
57 | ||
58 | memset(&rtmsg, 0, sizeof(rtmsg)); | |
59 | ||
60 | rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in); | |
61 | rtm->rtm_version = RTM_VERSION; | |
62 | rtm->rtm_type = RTM_GET; | |
63 | rtm->rtm_addrs = RTA_DST | RTA_IFP; | |
64 | rtm->rtm_seq = ++seq; | |
65 | ||
66 | sin = (struct sockaddr_in *)(rtm + 1); | |
67 | sin->sin_len = len = sizeof(struct sockaddr_in); | |
68 | sin->sin_family = AF_INET; | |
69 | sin->sin_addr.s_addr = ip; | |
70 | ||
71 | if ((write(rtsock, (char *)&rtmsg, rtm->rtm_msglen)) < 0) { | |
72 | close(rtsock); | |
73 | return false; | |
74 | } | |
75 | ||
76 | do { | |
77 | len = read(rtsock, (char *)&rtmsg, sizeof(rtmsg)); | |
78 | } while (len > 0 && (rtmsg.rtm.rtm_seq != seq || | |
79 | rtmsg.rtm.rtm_pid != pid)); | |
80 | ||
81 | close(rtsock); | |
82 | ||
83 | if (len < 0) { | |
84 | return false; | |
85 | } | |
86 | ||
87 | sa = (struct sockaddr *)(rtm + 1); | |
88 | for (i = 1; i; i <<= 1) { | |
89 | if (rtm->rtm_addrs & i) { | |
90 | if (i == RTA_IFP && sa->sa_family == AF_LINK && | |
91 | ((struct sockaddr_dl *)sa)->sdl_nlen) { | |
92 | ifp = (struct sockaddr_dl *)sa; | |
93 | namelen = ifp->sdl_nlen; | |
94 | if (namelen > IFNAMSIZ - 1) | |
95 | namelen = IFNAMSIZ - 1; | |
96 | memcpy(name, ifp->sdl_data, namelen); | |
97 | name[namelen] = '\0'; | |
98 | return true; | |
99 | } | |
100 | sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa)); | |
101 | } | |
102 | } | |
103 | return false; | |
104 | } | |
105 | ||
106 | void | |
574b679c | 107 | route_table_register(void) |
9360d9b7 EM |
108 | { |
109 | if (!register_count) | |
110 | { | |
111 | pid = getpid(); | |
112 | } | |
113 | ||
114 | register_count++; | |
115 | } | |
116 | ||
117 | void | |
574b679c | 118 | route_table_unregister(void) |
9360d9b7 EM |
119 | { |
120 | register_count--; | |
121 | } | |
122 | ||
123 | void | |
124 | route_table_run(void) | |
125 | { | |
126 | } | |
127 | ||
128 | void | |
129 | route_table_wait(void) | |
130 | { | |
131 | } |