## iof-bird-daemon / nest / a-set.c @ fbde6c39

History | View | Annotate | Download (1.77 KB)

1 | c0668f36 | Martin Mares | ```
/*
``` |
---|---|---|---|

2 | ```
* BIRD -- Set/Community-list Operations
``` |
||

3 | ```
*
``` |
||

4 | ```
* (c) 2000 Martin Mares <mj@ucw.cz>
``` |
||

5 | ```
* (c) 2000 Pavel Machek <pavel@ucw.cz>
``` |
||

6 | ```
*
``` |
||

7 | ```
* Can be freely distributed and used under the terms of the GNU GPL.
``` |
||

8 | ```
*/
``` |
||

9 | |||

10 | #include "nest/bird.h" |
||

11 | #include "nest/route.h" |
||

12 | c6add07f | Martin Mares | #include "nest/attrs.h" |

13 | c0668f36 | Martin Mares | #include "lib/resource.h" |

14 | c6add07f | Martin Mares | #include "lib/string.h" |

15 | |||

16 | ```
void
``` |
||

17 | aebe06b4 | Ondrej Zajicek | int_set_format(struct adata *set, int way, byte *buf, unsigned int size) |

18 | c6add07f | Martin Mares | { |

19 | u32 *z = (u32 *) set->data; |
||

20 | int l = set->length / 4; |
||

21 | int sp = 1; |
||

22 | ```
byte *end = buf + size - 16;
``` |
||

23 | |||

24 | ```
while (l--)
``` |
||

25 | { |
||

26 | 700bbe60 | Martin Mares | ```
if (!sp)
``` |

27 | ```
*buf++ = ' ';
``` |
||

28 | c6add07f | Martin Mares | ```
if (buf > end)
``` |

29 | { |
||

30 | ```
strcpy(buf, "...");
``` |
||

31 | ```
return;
``` |
||

32 | } |
||

33 | aebe06b4 | Ondrej Zajicek | |

34 | ```
if (way)
``` |
||

35 | buf += bsprintf(buf, "(%d,%d)", *z >> 16, *z & 0xffff); |
||

36 | ```
else
``` |
||

37 | ```
buf += bsprintf(buf, "%d.%d.%d.%d",
``` |
||

38 | (*z >> 24) & 0xff, (*z >> 16) & 0xff, |
||

39 | (*z >> 8) & 0xff, *z & 0xff); |
||

40 | |||

41 | c6add07f | Martin Mares | z++; |

42 | 700bbe60 | Martin Mares | ```
sp = 0;
``` |

43 | c6add07f | Martin Mares | } |

44 | ```
*buf = 0;
``` |
||

45 | } |
||

46 | 9c400ec9 | Pavel Machek | |

47 | ```
struct adata *
``` |
||

48 | int_set_add(struct linpool *pool, struct adata *list, u32 val) |
||

49 | { |
||

50 | 4847a894 | Ondrej Zajicek | int len = list ? list->length : 0; |

51 | struct adata *res = lp_alloc(pool, len + sizeof(struct adata) + 4); |
||

52 | ```
res->length = len + 4;
``` |
||

53 | 9c400ec9 | Pavel Machek | * (u32 *) res->data = val; |

54 | 4847a894 | Ondrej Zajicek | ```
if (list)
``` |

55 | memcpy((char *) res->data + 4, list->data, list->length); |
||

56 | 9c400ec9 | Pavel Machek | ```
return res;
``` |

57 | } |
||

58 | |||

59 | ```
int
``` |
||

60 | ```
int_set_contains(struct adata *list, u32 val)
``` |
||

61 | { |
||

62 | 700bbe60 | Martin Mares | u32 *l = (u32 *) list->data; |

63 | unsigned int i; |
||

64 | 9c400ec9 | Pavel Machek | for (i=0; i<list->length/4; i++) |

65 | ```
if (*l++ == val)
``` |
||

66 | return 1; |
||

67 | return 0; |
||

68 | } |
||

69 | |||

70 | ```
struct adata *
``` |
||

71 | int_set_del(struct linpool *pool, struct adata *list, u32 val) |
||

72 | { |
||

73 | ```
struct adata *res;
``` |
||

74 | u32 *l, *k; |
||

75 | 700bbe60 | Martin Mares | unsigned int i; |

76 | 9c400ec9 | Pavel Machek | |

77 | ```
if (!int_set_contains(list, val))
``` |
||

78 | ```
return list;
``` |
||

79 | |||

80 | res = lp_alloc(pool, list->length + sizeof(struct adata) - 4); |
||

81 | ```
res->length = list->length-4;
``` |
||

82 | |||

83 | 700bbe60 | Martin Mares | l = (u32 *) list->data; |

84 | k = (u32 *) res->data; |
||

85 | 9c400ec9 | Pavel Machek | for (i=0; i<list->length/4; i++) |

86 | ```
if (l[i] != val)
``` |
||

87 | *k++ = l[i]; |
||

88 | |||

89 | ```
return res;
``` |
||

90 | } |