source: mainline/uspace/lib/pcap/src/pcap_iface.c@ 03cd7a9e

Last change on this file since 03cd7a9e was 03cd7a9e, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

refactoring after 23.10

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2023 Nataliia Korop
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libpcap
30 * @{
31 */
32/** @file
33 * @brief pcap inteface: Dumping interface for the device which packets we want to dump
34 */
35
36#include <errno.h>
37#include "pcap_iface.h"
38
39static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
40{
41 return fwrite(&data, 1, 4, (FILE *)writer->data);
42}
43
44static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
45{
46 return fwrite(&data, 1, 2, (FILE *)writer->data);
47}
48
49static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
50{
51 return fwrite(data, 1, size, (FILE *)writer->data);
52}
53
54static void pcap_file_close(pcap_writer_t *writer)
55{
56 fclose((FILE *)writer->data);
57}
58
59static pcap_writer_ops_t file_ops = {
60
61 .write_u32 = &pcap_file_w32,
62 .write_u16 = &pcap_file_w16,
63 .write_buffer = &pcap_file_wbuffer,
64 .close = &pcap_file_close
65};
66
67// static size_t pcap_short_file_w32(pcap_writer_t *writer, uint32_t data)
68// {
69// return fwrite(&data, 1, 4, (FILE *)writer->data);
70// }
71
72// static size_t pcap_short_file_w16(pcap_writer_t *writer, uint16_t data)
73// {
74// return fwrite(&data, 1, 2, (FILE *)writer->data);
75// }
76
77// static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
78// {
79// return fwrite(data, 1, size<60?size:60, (FILE *)writer->data);
80// }
81
82// static void pcap_short_file_close(pcap_writer_t *writer)
83// {
84// fclose((FILE *)writer->data);
85// }
86
87
88// static pcap_writer_ops_t short_file_ops = {
89// .write_u32 = &pcap_short_file_w32,
90// .write_u16 = &pcap_short_file_w16,
91// .write_buffer = &pcap_short_file_wbuffer,
92// .close = &pcap_short_file_close
93
94// };
95
96errno_t pcap_dumper_start(struct pcap_dumper *dumper, const char *name)
97{
98 fibril_mutex_lock(&dumper->mutex);
99
100 /** When try to start when already started, close current and starts new */
101 if (dumper->to_dump == true)
102 {
103 pcap_dumper_stop(dumper);
104 }
105 errno_t rc = pcap_writer_to_file_init(&dumper->writer, name);
106 if (rc == EOK)
107 {
108 dumper->to_dump = true;
109 }
110 else
111 {
112 printf("Failed creating pcap dumper: %s", str_error(rc));
113 }
114 fibril_mutex_unlock(&dumper->mutex);
115 return rc;
116}
117
118//udelat globalni
119void pcap_dumper_add_packet(struct pcap_dumper *dumper, const void *data, size_t size)
120{
121 fibril_mutex_lock(&dumper->mutex);
122
123 if (dumper->writer.data == NULL || !dumper->to_dump)
124 {
125 fibril_mutex_unlock(&dumper->mutex);
126 return;
127 }
128 pcap_writer_add_packet(&dumper->writer, data, size);
129 fibril_mutex_unlock(&dumper->mutex);
130}
131
132//udelt globalni
133void pcap_dumper_stop(struct pcap_dumper *dumper)
134{
135 fibril_mutex_lock(&dumper->mutex);
136
137 /** If want to stop, when already stopped, do nothing */
138 if (dumper->to_dump == false) {
139 fibril_mutex_unlock(&dumper->mutex);
140 return;
141 }
142 dumper->to_dump = false;
143 dumper->writer.ops->close(&dumper->writer);
144 dumper->writer.data = NULL;
145 fibril_mutex_unlock(&dumper->mutex);
146}
147
148/** Initialize interface for dumping packets
149 *
150 * @param dumper Device dumping interface
151 *
152 */
153errno_t pcap_dumper_init(pcap_dumper_t *dumper)
154{
155 fibril_mutex_initialize(&dumper->mutex);
156 dumper->to_dump = false;
157 dumper->writer.ops = &file_ops;
158 return EOK;
159}
160
161/** @}
162 */
Note: See TracBrowser for help on using the repository browser.