-static inline int stun_msg2type(int class, int method)
-{
- return ((class & 1) << 4) | ((class & 2) << 7) |
- (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2);
-}
-
-/* helper function to print message names */
-static const char *stun_msg2str(int msg)
-{
- static const struct stun_strings classes[] = {
- { STUN_REQUEST, "Request" },
- { STUN_INDICATION, "Indication" },
- { STUN_RESPONSE, "Response" },
- { STUN_ERROR_RESPONSE, "Error Response" },
- { 0, NULL }
-};
- static const struct stun_strings methods[] = {
- { STUN_BINDING, "Binding" },
- { 0, NULL }
-};
- static char result[32];
- const char *class = NULL, *method = NULL;
- int i, value;
-
- value = stun_msg2class(msg);
- for (i = 0; classes[i].name; i++) {
- class = classes[i].name;
- if (classes[i].value == value)
- break;
- }
- value = stun_msg2method(msg);
- for (i = 0; methods[i].name; i++) {
- method = methods[i].name;
- if (methods[i].value == value)
- break;
- }
- snprintf(result, sizeof(result), "%s %s",
- method ? : "Unknown Method",
- class ? : "Unknown Class Message");
- return result;
-}
-
-/* helper function to print attribute names */
-static const char *stun_attr2str(int msg)
-{
- static const struct stun_strings attrs[] = {
- { STUN_MAPPED_ADDRESS, "Mapped Address" },
- { STUN_RESPONSE_ADDRESS, "Response Address" },
- { STUN_CHANGE_ADDRESS, "Change Address" },
- { STUN_SOURCE_ADDRESS, "Source Address" },
- { STUN_CHANGED_ADDRESS, "Changed Address" },
- { STUN_USERNAME, "Username" },
- { STUN_PASSWORD, "Password" },
- { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
- { STUN_ERROR_CODE, "Error Code" },
- { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
- { STUN_REFLECTED_FROM, "Reflected From" },
- { STUN_REALM, "Realm" },
- { STUN_NONCE, "Nonce" },
- { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
- { STUN_MS_VERSION, "MS Version" },
- { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
- { STUN_SOFTWARE, "Software" },
- { STUN_ALTERNATE_SERVER, "Alternate Server" },
- { STUN_FINGERPRINT, "Fingerprint" },
- { 0, NULL }
-};
- int i;
-
- for (i = 0; attrs[i].name; i++) {
- if (attrs[i].value == msg)
- return attrs[i].name;
- }
- return "Unknown Attribute";
-}
-
-/* here we store credentials extracted from a message */
-struct stun_state {
- unsigned short attr;
-};
-
-static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
-{
- if (stun_debug)
- fprintf(stderr, "Found STUN Attribute %s (%04x), length %d\n",
- stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
- switch (ntohs(attr->attr)) {
- case STUN_MAPPED_ADDRESS:
- case STUN_XOR_MAPPED_ADDRESS:
- case STUN_MS_XOR_MAPPED_ADDRESS:
- break;
- default:
- if (stun_debug)
- fprintf(stderr, "Ignoring STUN Attribute %s (%04x), length %d\n",
- stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
- }
- return 0;
-}
-
-/* append a string to an STUN message */
-static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
-{
- int str_length = strlen(s);
- int attr_length = str_length + ((~(str_length - 1)) & 0x3);
- int size = sizeof(**attr) + attr_length;
- if (*left > size) {
- (*attr)->attr = htons(attrval);
- (*attr)->len = htons(attr_length);
- memcpy((*attr)->value, s, str_length);
- memset((*attr)->value + str_length, 0, attr_length - str_length);
- (*attr) = (struct stun_attr *)((*attr)->value + attr_length);
- *len += size;
- *left -= size;
- }
-}
-
-
-/* helper function to generate a random request id */
-static void stun_req_id(struct stun_header *req)
-{
- int x;
- srand(time(0));
- req->magic = htonl(STUN_MAGIC_COOKIE);
- for (x = 0; x < 3; x++)
- req->id.id[x] = rand();
-}