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 tlen
= (i
< argc
- 1) ? 2 : 1;
101 size_t alen
= strlen(argv
[i
]) + tlen
;
102 size_t slen
= str
? strlen(str
) : 0;
104 str
= xrealloc(str
, slen
+ alen
);
105 ret
= slprintf(str
+ offset
, strlen(argv
[i
]) + tlen
, "%s%s",
106 argv
[i
], tlen
== 2 ? " " : "");
108 panic("Cannot concatenate string!\n");
116 char **argv_insert(char **argv
, size_t *count
, const char *str
)
118 argv
= xrealloc(argv
, (*count
+ 2) * sizeof(char *));
119 argv
[*count
] = str
? xstrdup(str
) : xstrdup("");
120 argv
[*count
+ 1] = NULL
;
126 void argv_free(char **argv
)
130 for (; argv
&& *argv
; argv
++)
136 int str2mac(const char *str
, uint8_t *mac
, size_t len
)
147 count
= sscanf(str
, "%02X:%02X:%02X:%02X:%02X:%02X",
148 &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3], &tmp
[4], &tmp
[5]);
150 if (errno
|| count
!= 6)
151 count
= sscanf(str
, "%02x:%02x:%02x:%02x:%02x:%02x",
152 &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3], &tmp
[4], &tmp
[5]);
159 for (i
= 0; i
< 6; i
++)
160 mac
[i
] = (uint8_t)tmp
[i
];
165 char *str2fqdn(const char *str
)
167 size_t slen
= strlen(str
);
178 fqdn
= xzmalloc(slen
+ 2);
180 while (tmp
<= dup
+ slen
&& c
++ <= slen
) {
181 if (tmp
[i
] == '.' || tmp
[i
] == '\0') {
187 memcpy(&fqdn
[flen
+ 1], tmp
, dlen
);