]> The Tcpdump Group git mirrors - libpcap/blob - pcap-options.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / pcap-options.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "pcap-int.h"
31
32 /*
33 * Private data for storing sets of options.
34 * This is to be used by functions which seem to grow additional options
35 * This is avoid ABI explosion of do_thing, and do_thing_with_opt_bar(),
36 * and then do_thing_with_opt_bar_baz(...). Instead a "pcap_option" should be
37 * created to which get/set shall be done.
38 *
39 * each option shall have an element in enum pcap_option_name {}.
40 */
41 struct pcap_options {
42 int tstamp_precision;
43 const char *io_read_plugin;
44 const char *io_write_plugin;
45 };
46
47 pcap_options *pcap_alloc_option(void)
48 {
49 pcap_options *po = malloc(sizeof(struct pcap_options));
50 memset(po, 0, sizeof(struct pcap_options));
51 return po; // caller has to check for NULL anyway.
52 }
53
54 void pcap_free_option(pcap_options *po)
55 {
56 if(po != NULL) {
57 if(po->io_read_plugin) free((void *)po->io_read_plugin);
58 if(po->io_write_plugin) free((void *)po->io_write_plugin);
59 free((void *)po);
60 }
61 }
62
63 /* Return 0 on success, -1 on failure invalid option, -2 on type mismatch */
64 int pcap_set_option_string(pcap_options *po,
65 enum pcap_option_name pon,
66 const char *value)
67 {
68 const char *saved = strdup(value);
69 switch(pon) {
70 case PON_TSTAMP_PRECISION:
71 free((void *)saved);
72 return -2;
73
74 case PON_IO_READ_PLUGIN:
75 po->io_read_plugin = saved;
76 break;
77 case PON_IO_WRITE_PLUGIN:
78 po->io_write_plugin= saved;
79 break;
80 default:
81 free((void *)saved);
82 return -1;
83 }
84 return 0;
85 }
86
87 /* Return 0 on success, -1 on failure invalid option, -2 on type mismatch */
88 int pcap_set_option_int(pcap_options *po,
89 enum pcap_option_name pon,
90 const int value)
91 {
92 switch(pon) {
93 case PON_TSTAMP_PRECISION:
94 po->tstamp_precision = value;
95 break;
96
97 case PON_IO_READ_PLUGIN:
98 case PON_IO_WRITE_PLUGIN:
99 return -2;
100 default:
101 return -1;
102 }
103 return 0;
104 }
105
106 const char *pcap_get_option_string(pcap_options *po,
107 enum pcap_option_name pon)
108 {
109 switch(pon) {
110 case PON_TSTAMP_PRECISION:
111 return NULL;
112
113 case PON_IO_READ_PLUGIN:
114 return po->io_read_plugin;
115 case PON_IO_WRITE_PLUGIN:
116 return po->io_write_plugin;
117 }
118 return NULL;
119 }
120
121
122 /* return int value, or zero for not-found, mis-type */
123 int pcap_get_option_int(pcap_options *po,
124 enum pcap_option_name pon)
125 {
126 switch(pon) {
127 case PON_TSTAMP_PRECISION:
128 return po->tstamp_precision;
129 break;
130
131 case PON_IO_READ_PLUGIN:
132 case PON_IO_WRITE_PLUGIN:
133 return 0;
134 }
135 return 0;
136 }
137