2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
10 #include <arpa/inet.h>
16 size_t strlcpy(char *dest
, const char *src
, size_t size
)
18 size_t ret
= strlen(src
);
21 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
23 memcpy(dest
, src
, len
);
30 static inline int vslprintf(char *dst
, size_t size
, const char *fmt
, va_list ap
)
34 ret
= vsnprintf(dst
, size
, fmt
, ap
);
40 int slprintf(char *dst
, size_t size
, const char *fmt
, ...)
46 ret
= vslprintf(dst
, size
, fmt
, ap
);
52 int slprintf_nocheck(char *dst
, size_t size
, const char *fmt
, ...)
58 ret
= vslprintf(dst
, size
, fmt
, ap
);
64 noinline
void *xmemset(void *s
, int c
, size_t n
)
69 for (i
= 0; i
< n
; ++i
)
75 char *strtrim_right(char *p
, char c
)
93 char *argv2str(int startind
, int argc
, char **argv
)
99 for (i
= startind
; i
< argc
; ++i
) {
100 size_t alen
= strlen(argv
[i
]) + 2;
101 size_t slen
= str
? strlen(str
) : 0;
103 str
= xrealloc(str
, slen
+ alen
);
104 ret
= slprintf(str
+ offset
, strlen(argv
[i
]) + 2, "%s ", argv
[i
]);
106 panic("Cannot concatenate string!\n");
114 char **argv_insert(char **argv
, size_t *count
, const char *str
)
116 argv
= xrealloc(argv
, (*count
+ 2) * sizeof(char *));
117 argv
[*count
] = str
? xstrdup(str
) : xstrdup("");
118 argv
[*count
+ 1] = NULL
;
124 void argv_free(char **argv
)
128 for (; argv
&& *argv
; argv
++)
134 int str2mac(const char *str
, uint8_t *mac
, size_t len
)
144 count
= sscanf(str
, "%02X:%02X:%02X:%02X:%02X:%02X",
145 &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3], &tmp
[4], &tmp
[5]);
147 if (errno
|| count
!= 6)
148 count
= sscanf(str
, "%02x:%02x:%02x:%02x:%02x:%02x",
149 &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3], &tmp
[4], &tmp
[5]);
156 for (i
= 0; i
< 6; i
++)
157 mac
[i
] = (uint8_t)tmp
[i
];