win32/oshw.c
Go to the documentation of this file.
1 /*
2  * Licensed under the GNU General Public License version 2 with exceptions. See
3  * LICENSE file in the project root for full license information
4  */
5 
6 #include "oshw.h"
7 
15 {
16  uint16 network = htons (host);
17  return network;
18 }
19 
27 {
28  uint16 host = ntohs (network);
29  return host;
30 }
31 
32 /* Create list over available network adapters.
33  * @return First element in linked list of adapters
34  */
36 {
37  int i = 0;
38  int ret = 0;
39  pcap_if_t *alldevs;
40  pcap_if_t *d;
41  ec_adaptert * adapter;
42  ec_adaptert * prev_adapter;
43  ec_adaptert * ret_adapter = NULL;
45  size_t string_len;
46 
47  /* find all devices */
48  if (pcap_findalldevs(&alldevs, errbuf) == -1)
49  {
50  fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
51  return (NULL);
52  }
53  /* Iterate all devices and create a local copy holding the name and
54  * description.
55  */
56  for(d= alldevs; d != NULL; d= d->next)
57  {
58  adapter = (ec_adaptert *)malloc(sizeof(ec_adaptert));
59  /* If we got more than one adapter save link list pointer to previous
60  * adapter.
61  * Else save as pointer to return.
62  */
63  if (i)
64  {
65  prev_adapter->next = adapter;
66  }
67  else
68  {
69  ret_adapter = adapter;
70  }
71 
72  /* fetch description and name of the device from libpcap */
73  adapter->next = NULL;
74  if (d->name)
75  {
76  string_len = strlen(d->name);
77  if (string_len > (EC_MAXLEN_ADAPTERNAME - 1))
78  {
79  string_len = EC_MAXLEN_ADAPTERNAME - 1;
80  }
81  strncpy(adapter->name, d->name,string_len);
82  adapter->name[string_len] = '\0';
83  }
84  else
85  {
86  adapter->name[0] = '\0';
87  }
88  if (d->description)
89  {
90  string_len = strlen(d->description);
91  if (string_len > (EC_MAXLEN_ADAPTERNAME - 1))
92  {
93  string_len = EC_MAXLEN_ADAPTERNAME - 1;
94  }
95  strncpy(adapter->desc, d->description,string_len);
96  adapter->desc[string_len] = '\0';
97  }
98  else
99  {
100  adapter->desc[0] = '\0';
101  }
102  prev_adapter = adapter;
103  i++;
104  }
105  /* free all devices allocated */
106  pcap_freealldevs(alldevs);
107 
108  return ret_adapter;
109 }
110 
116 {
117  ec_adaptert * next_adapter;
118  /* Iterate the linked list and free all elements holding
119  * adapter information
120  */
121  if(adapter)
122  {
123  next_adapter = adapter->next;
124  free (adapter);
125  while (next_adapter)
126  {
127  adapter = next_adapter;
128  next_adapter = adapter->next;
129  free (adapter);
130  }
131  }
132 }
int pcap_findalldevs(pcap_if_t **, char *)
#define EC_MAXLEN_ADAPTERNAME
Definition: ethercatmain.h:39
char name[EC_MAXLEN_ADAPTERNAME]
Definition: ethercatmain.h:46
static char errbuf[PCAP_ERRBUF_SIZE]
Definition: macosx/nicdrv.c:68
ec_adaptert * next
Definition: ethercatmain.h:48
char * name
Definition: pcap/pcap.h:225
uint16_t uint16
Definition: osal.h:29
char * description
Definition: pcap/pcap.h:226
Headerfile for ethercatbase.c.
char desc[EC_MAXLEN_ADAPTERNAME]
Definition: ethercatmain.h:47
uint16 oshw_htons(uint16 host)
Definition: win32/oshw.c:14
void oshw_free_adapters(ec_adaptert *adapter)
Definition: win32/oshw.c:115
struct pcap_if * next
Definition: pcap/pcap.h:224
void pcap_freealldevs(pcap_if_t *)
uint16 oshw_ntohs(uint16 network)
Definition: win32/oshw.c:26
ec_adaptert * oshw_find_adapters(void)
Definition: win32/oshw.c:35
#define PCAP_ERRBUF_SIZE
Definition: pcap/pcap.h:76


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Mon Feb 28 2022 23:46:57