]> The Tcpdump Group git mirrors - libpcap/blob - pcap-options.c
Fix spelling
[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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "pcap-int.h"
33
34 /*
35 * Private data for storing sets of options.
36 * This is to be used by functions which seem to grow additional options
37 * This is avoid ABI explosion of do_thing, and do_thing_with_opt_bar(),
38 * and then do_thing_with_opt_bar_baz(...). Instead a "pcap_option" should be
39 * created to which get/set shall be done.
40 *
41 * each option shall have an element in enum pcap_option_name {}.
42 */
43 struct pcap_options {
44 int tstamp_precision;
45 const char *io_read_plugin;
46 const char *io_write_plugin;
47 };
48
49 pcap_options *pcap_alloc_option(void)
50 {
51 pcap_options *po = malloc(sizeof(struct pcap_options));
52 memset(po, 0, sizeof(struct pcap_options));
53 return po; // caller has to check for NULL anyway.
54 }
55
56 void pcap_free_option(pcap_options *po)
57 {
58 if(po != NULL) {
59 if(po->io_read_plugin) free((void *)po->io_read_plugin);
60 if(po->io_write_plugin) free((void *)po->io_write_plugin);
61 free((void *)po);
62 }
63 }
64
65 /* Return 0 on success, -1 on failure invalid option, -2 on type mismatch */
66 int pcap_set_option_string(pcap_options *po,
67 enum pcap_option_name pon,
68 const char *value)
69 {
70 const char *saved = strdup(value);
71 switch(pon) {
72 case PON_TSTAMP_PRECISION:
73 free((void *)saved);
74 return -2;
75
76 case PON_IO_READ_PLUGIN:
77 po->io_read_plugin = saved;
78 break;
79 case PON_IO_WRITE_PLUGIN:
80 po->io_write_plugin= saved;
81 break;
82 default:
83 free((void *)saved);
84 return -1;
85 }
86 return 0;
87 }
88
89 /* Return 0 on success, -1 on failure invalid option, -2 on type mismatch */
90 int pcap_set_option_int(pcap_options *po,
91 enum pcap_option_name pon,
92 const int value)
93 {
94 switch(pon) {
95 case PON_TSTAMP_PRECISION:
96 po->tstamp_precision = value;
97 break;
98
99 case PON_IO_READ_PLUGIN:
100 case PON_IO_WRITE_PLUGIN:
101 return -2;
102 default:
103 return -1;
104 }
105 return 0;
106 }
107
108 const char *pcap_get_option_string(pcap_options *po,
109 enum pcap_option_name pon)
110 {
111 switch(pon) {
112 case PON_TSTAMP_PRECISION:
113 return NULL;
114
115 case PON_IO_READ_PLUGIN:
116 return po->io_read_plugin;
117 case PON_IO_WRITE_PLUGIN:
118 return po->io_write_plugin;
119 }
120 return NULL;
121 }
122
123
124 /* return int value, or zero for not-found, mis-type */
125 int pcap_get_option_int(pcap_options *po,
126 enum pcap_option_name pon)
127 {
128 switch(pon) {
129 case PON_TSTAMP_PRECISION:
130 return po->tstamp_precision;
131 break;
132
133 case PON_IO_READ_PLUGIN:
134 case PON_IO_WRITE_PLUGIN:
135 return 0;
136 }
137 return 0;
138 }
139