Changeset 64ea525 in mainline for uspace/lib/pcap/src/pcap_dumper.c


Ignore:
Timestamp:
2024-12-13T08:44:05Z (15 months ago)
Author:
Nataliia Korop <n.corop08@…>
Children:
f161ce1
Parents:
1d14090
git-author:
Nataliia Korop <n.corop08@…> (2024-11-10 09:31:01)
git-committer:
Nataliia Korop <n.corop08@…> (2024-12-13 08:44:05)
Message:

init dumping destionation move to ops

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/pcap/src/pcap_dumper.c

    r1d14090 r64ea525  
    3838#include "pcap_dumper.h"
    3939
     40/** Initialize writing to .pcap file.
     41 *
     42 * @param writer    Interface for working with .pcap file
     43 * @param filename  Name of the file for dumping packets
     44 * @return          EOK on success or an error code
     45 *
     46 */
     47static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
     48{
     49        errno_t rc;
     50        writer->data = fopen(filename, "a");
     51        if (writer->data == NULL) {
     52                rc = EINVAL;
     53                return rc;
     54        }
     55        pcap_writer_add_header(writer);
     56
     57        rc = EOK;
     58        return rc;
     59}
     60
    4061static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
    4162{
     
    6182
    6283static pcap_writer_ops_t file_ops = {
    63 
     84        .open = &pcap_writer_to_file_init,
    6485        .write_u32 = &pcap_file_w32,
    6586        .write_u16 = &pcap_file_w16,
     
    80101static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
    81102{
    82         return fwrite(data, 1, size<60?size:60, (FILE *)writer->data);
     103        return fwrite(data, 1, size < 60 ? size : 60, (FILE *)writer->data);
    83104}
    84105
     
    89110
    90111static pcap_writer_ops_t short_file_ops = {
     112        .open = &pcap_writer_to_file_init,
    91113        .write_u32 = &pcap_short_file_w32,
    92114        .write_u16 = &pcap_short_file_w16,
     
    104126                pcap_dumper_stop(dumper);
    105127        }
    106         errno_t rc = pcap_writer_to_file_init(&dumper->writer, name);
     128        errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
    107129        if (rc == EOK) {
    108130                dumper->to_dump = true;
     
    118140        fibril_mutex_lock(&dumper->mutex);
    119141        errno_t rc = EOK;
    120         if (!str_cmp(name, "short_file"))
    121         {
     142        if (!str_cmp(name, "short_file")) {
    122143                dumper->writer.ops = &short_file_ops;
    123         }
    124         else if (!str_cmp(name, "full_file"))
    125         {
     144        } else if (!str_cmp(name, "full_file")) {
    126145                dumper->writer.ops = &file_ops;
    127         }
    128         else
    129         {
     146        } else {
    130147                rc = EINVAL;
    131148        }
     
    133150        return rc;
    134151}
    135 
    136152
    137153void pcap_dumper_add_packet(struct pcap_dumper *dumper, const void *data, size_t size)
Note: See TracChangeset for help on using the changeset viewer.