|
| 1 | +/**************************************************************************** |
| 2 | + * apps/netutils/netlib/netlib_checkifconflict.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdbool.h> |
| 29 | +#include <unistd.h> |
| 30 | +#include <string.h> |
| 31 | +#include <errno.h> |
| 32 | +#include <debug.h> |
| 33 | + |
| 34 | +#include "netutils/netlib.h" |
| 35 | + |
| 36 | +#ifdef CONFIG_NET_ARP_ACD |
| 37 | + |
| 38 | +/**************************************************************************** |
| 39 | + * Pre-processor Definitions |
| 40 | + ****************************************************************************/ |
| 41 | + |
| 42 | +/* Determines the size of an intermediate buffer that must be large enough |
| 43 | + * to handle the longest line generated by this logic. |
| 44 | + */ |
| 45 | + |
| 46 | +#define PROCFS_LINELEN 80 |
| 47 | +#define PROCFS_NET_PATH "/proc/net/" |
| 48 | + |
| 49 | +/**************************************************************************** |
| 50 | + * Public Functions |
| 51 | + ****************************************************************************/ |
| 52 | + |
| 53 | +/**************************************************************************** |
| 54 | + * Name: netlib_check_ifconflict |
| 55 | + * |
| 56 | + * Description: |
| 57 | + * Check the network driver ip conflict |
| 58 | + * |
| 59 | + * Parameters: |
| 60 | + * ifname The name of the interface to use |
| 61 | + * |
| 62 | + * Return: |
| 63 | + * 0 on no conflict; a nagtive on failure; 1 on conflict |
| 64 | + * |
| 65 | + ****************************************************************************/ |
| 66 | + |
| 67 | +int netlib_check_ifconflict(FAR const char *ifname) |
| 68 | +{ |
| 69 | + int conflict = 0; |
| 70 | + char path[32]; |
| 71 | + char line[PROCFS_LINELEN]; |
| 72 | + FAR FILE *stream; |
| 73 | + |
| 74 | + if (ifname == NULL) |
| 75 | + { |
| 76 | + fprintf(stderr, "ERROR: ifname is NULL \n"); |
| 77 | + return -EINVAL; |
| 78 | + } |
| 79 | + |
| 80 | + snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname); |
| 81 | + ninfo("get dev statistics from %s \n", path); |
| 82 | + |
| 83 | + stream = fopen(path, "r"); |
| 84 | + if (stream == NULL) |
| 85 | + { |
| 86 | + fprintf(stderr, "ERROR: Failed to open path:%s \n", path); |
| 87 | + return -errno; |
| 88 | + } |
| 89 | + |
| 90 | + while (fgets(line, sizeof(line), stream) != NULL) |
| 91 | + { |
| 92 | + if (strstr(line, "conflict!") != NULL) |
| 93 | + { |
| 94 | + conflict = 1; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fclose(stream); |
| 100 | + return conflict; |
| 101 | +} |
| 102 | + |
| 103 | +#endif /* CONFIG_NET_ARP_ACD */ |
0 commit comments