1 /* 2 * Copyright 2011 Daniel Drown 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 * mtu.c - get interface mtu 17 */ 18 19 #include <string.h> 20 #include <stdlib.h> 21 #include <sys/types.h> 22 #include <sys/socket.h> 23 #include <sys/ioctl.h> 24 #include <net/if.h> 25 26 #include "mtu.h" 27 28 /* function: getifmtu 29 * returns the interface mtu or -1 on failure 30 * ifname - interface name 31 */ 32 int getifmtu(const char *ifname) { 33 int fd; 34 struct ifreq if_mtu; 35 36 fd = socket(AF_INET, SOCK_STREAM, 0); 37 if(fd < 0) { 38 return -1; 39 } 40 strncpy(if_mtu.ifr_name, ifname, IFNAMSIZ); 41 if_mtu.ifr_name[IFNAMSIZ - 1] = '\0'; 42 if(ioctl(fd, SIOCGIFMTU, &if_mtu) < 0) { 43 return -1; 44 } 45 return if_mtu.ifr_mtu; 46 } 47