text2qos.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* text2qos.c - Converts textual representation of QOS parameters to binary
  2. encoding */
  3. /* Written 1996-2000 by Werner Almesberger, EPFL-LRC/ICA */
  4. #if HAVE_CONFIG_H
  5. #include <config.h>
  6. #endif
  7. #include <stdlib.h>
  8. #include <stdarg.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <limits.h>
  12. #include "atm.h"
  13. #define fetch __atmlib_fetch
  14. #define RATE_ERROR -2
  15. int __t2q_get_rate(const char **text,int up)
  16. {
  17. const char mult[] = "kKmMgGg";
  18. const char *multiplier;
  19. char *end;
  20. unsigned int rate,fract;
  21. int power;
  22. if (!strncmp(*text,"max",3)) {
  23. *text += 3;
  24. return ATM_MAX_PCR;
  25. }
  26. rate = strtoul(*text,&end,10);
  27. power = fract = 0;
  28. if (*end == '.')
  29. for (end++; *end && isdigit(*end); end++) {
  30. fract = fract*10+*end-48;
  31. if (--power == -9) break;
  32. }
  33. multiplier = NULL;
  34. if (*end && (multiplier = strchr(mult,*end))) {
  35. while (multiplier >= mult) {
  36. if (rate > UINT_MAX/1000) return RATE_ERROR;
  37. rate *= 1000;
  38. power += 3;
  39. multiplier -= 2;
  40. }
  41. end++;
  42. }
  43. while (power && fract)
  44. if (power < 0) {
  45. fract /= 10;
  46. power++;
  47. }
  48. else {
  49. fract *= 10;
  50. power--;
  51. }
  52. rate += fract;
  53. if (strlen(end) < 3) {
  54. if (multiplier) return RATE_ERROR;
  55. }
  56. else if (!strncmp(end,"cps",3)) end += 3;
  57. else if (!strncmp(end,"bps",3)) {
  58. rate = (rate+(up ? 8*ATM_CELL_PAYLOAD-1 : 0))/8/
  59. ATM_CELL_PAYLOAD;
  60. end += 3;
  61. }
  62. else if (multiplier) return RATE_ERROR;
  63. if (rate > INT_MAX) return RATE_ERROR;
  64. *text = end;
  65. return rate;
  66. }
  67. static int params(const char **text,struct atm_trafprm *a,
  68. struct atm_trafprm *b)
  69. {
  70. int value;
  71. char *end;
  72. if (*(*text)++ != ':') return -1;
  73. while (1) {
  74. if (!**text) return -1;
  75. switch (fetch(text,"max_pcr=","pcr=","min_pcr=","max_sdu=","sdu=",
  76. NULL)) {
  77. case 0:
  78. if ((value = __t2q_get_rate(text,0)) == RATE_ERROR) return -1;
  79. if (a) a->max_pcr = value;
  80. if (b) b->max_pcr = value;
  81. break;
  82. case 1:
  83. if ((value = __t2q_get_rate(text,0)) == RATE_ERROR) return -1;
  84. if (a) a->pcr = value;
  85. if (b) b->pcr = value;
  86. break;
  87. case 2:
  88. if ((value = __t2q_get_rate(text,1)) == RATE_ERROR) return -1;
  89. if (value == ATM_MAX_PCR) return -1;
  90. if (a) a->min_pcr = value;
  91. if (b) b->min_pcr = value;
  92. break;
  93. case 3:
  94. case 4:
  95. value = strtol(*text,&end,10);
  96. if (value < 0) return -1;
  97. *text = end;
  98. if (a) a->max_sdu = value;
  99. if (b) b->max_sdu = value;
  100. break;
  101. default:
  102. return 0;
  103. }
  104. if (!**text) break;
  105. if (*(*text)++ != ',') return -1;
  106. }
  107. return 0;
  108. }
  109. int text2qos(const char *text,struct atm_qos *qos,int flags)
  110. {
  111. int traffic_class,aal;
  112. traffic_class = ATM_NONE;
  113. aal = ATM_NO_AAL;
  114. do {
  115. static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
  116. int item;
  117. item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
  118. switch (item) {
  119. case 1:
  120. case 2:
  121. /* we don't support VBR yet */
  122. case 4:
  123. traffic_class = item;
  124. break;
  125. case 5:
  126. case 6:
  127. aal = aal_number[item-5];
  128. break;
  129. default:
  130. return -1;
  131. }
  132. }
  133. while (*text == ',' ? text++ : 0);
  134. if (!traffic_class) return -1;
  135. if (qos && !(flags & T2Q_DEFAULTS)) memset(qos,0,sizeof(*qos));
  136. if (qos) qos->txtp.traffic_class = qos->rxtp.traffic_class = traffic_class;
  137. if (qos && aal) qos->aal = aal;
  138. if (!*text) return 0;
  139. if (params(&text,qos ? &qos->txtp : NULL,qos ? &qos->rxtp : NULL))
  140. return -1;
  141. if (!*text) return 0;
  142. switch (fetch(&text,"tx","rx",NULL)) {
  143. case 0:
  144. if (!fetch(&text,":none",NULL)) {
  145. if (qos) qos->txtp.traffic_class = ATM_NONE;
  146. if (*text == ',') text++;
  147. break;
  148. }
  149. if (params(&text,qos ? &qos->txtp : NULL,NULL)) return -1;
  150. break;
  151. case 1:
  152. text -= 2;
  153. break;
  154. default:
  155. return -1;
  156. }
  157. if (!*text) return 0;
  158. if (fetch(&text,"rx",NULL)) return -1;
  159. if (!fetch(&text,":none",NULL) && qos) qos->rxtp.traffic_class = ATM_NONE;
  160. else if (params(&text,qos ? &qos->rxtp : NULL,NULL)) return -1;
  161. return *text ? -1 : 0;
  162. }