2 * MAC address manupulation utility
4 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
18 #define MAC_ADDRESS_LEN 6
23 static void usage(void);
27 static int parse_mac(const char *mac_str
, unsigned char *buf
)
31 t
= sscanf(mac_str
, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
32 &buf
[0], &buf
[1], &buf
[2], &buf
[3], &buf
[4], &buf
[5]);
34 if (t
!= MAC_ADDRESS_LEN
)
40 static void print_mac(unsigned char *buf
)
42 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
43 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5]);
46 static int maccalc_do_add(int argc
, const char *argv
[])
48 unsigned char mac
[MAC_ADDRESS_LEN
];
58 err
= parse_mac(argv
[0], mac
);
64 t
= (mac
[3] << 16) | (mac
[4] << 8) | mac
[5];
66 mac
[3] = (t
>> 16) & 0xff;
67 mac
[4] = (t
>> 8) & 0xff;
74 static int maccalc_do_logical(int argc
, const char *argv
[],
75 unsigned char (*op
)(unsigned char n1
,
78 unsigned char mac1
[MAC_ADDRESS_LEN
];
79 unsigned char mac2
[MAC_ADDRESS_LEN
];
88 err
= parse_mac(argv
[0], mac1
);
92 err
= parse_mac(argv
[1], mac2
);
96 for (i
= 0; i
< MAC_ADDRESS_LEN
; i
++)
97 mac1
[i
] = op(mac1
[i
],mac2
[i
]);
103 static int maccalc_do_mac2bin(int argc
, const char *argv
[])
105 unsigned char mac
[MAC_ADDRESS_LEN
];
114 err
= parse_mac(argv
[0], mac
);
118 c
= write(STDOUT_FILENO
, mac
, sizeof(mac
));
119 if (c
!= sizeof(mac
)) {
120 fprintf(stderr
, "failed to write to stdout\n");
127 static int maccalc_do_bin2mac(int argc
, const char *argv
[])
129 unsigned char mac
[MAC_ADDRESS_LEN
];
137 c
= read(STDIN_FILENO
, mac
, sizeof(mac
));
138 if (c
!= sizeof(mac
)) {
139 fprintf(stderr
, "failed to read from stdin\n");
147 static unsigned char op_or(unsigned char n1
, unsigned char n2
)
152 static int maccalc_do_or(int argc
, const char *argv
[])
154 return maccalc_do_logical(argc
, argv
, op_or
);
157 static unsigned char op_and(unsigned char n1
, unsigned char n2
)
162 static int maccalc_do_and(int argc
, const char *argv
[])
164 return maccalc_do_logical(argc
, argv
, op_and
);
167 static unsigned char op_xor(unsigned char n1
, unsigned char n2
)
172 static int maccalc_do_xor(int argc
, const char *argv
[])
174 return maccalc_do_logical(argc
, argv
, op_xor
);
177 static void usage(void)
180 "Usage: %s <command>\n"
182 " add <mac> <number>\n"
183 " and|or|xor <mac1> <mac2>\n"
189 int main(int argc
, const char *argv
[])
191 int (*op
)(int argc
, const char *argv
[]);
194 maccalc_name
= (char *) argv
[0];
201 if (strcmp(argv
[1], "add") == 0) {
203 } else if (strcmp(argv
[1], "and") == 0) {
205 } else if (strcmp(argv
[1], "or") == 0) {
207 } else if (strcmp(argv
[1], "xor") == 0) {
209 } else if (strcmp(argv
[1], "mac2bin") == 0) {
210 op
= maccalc_do_mac2bin
;
211 } else if (strcmp(argv
[1], "bin2mac") == 0) {
212 op
= maccalc_do_bin2mac
;
214 fprintf(stderr
, "unknown command '%s'\n", argv
[1]);
222 ret
= op(argc
, argv
);
This page took 0.061224 seconds and 5 git commands to generate.