]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jstun-src-0.7.1/de/javawi/jstun/attribute/MessageAttribute.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jstun-src-0.7.1 / de / javawi / jstun / attribute / MessageAttribute.java
1 /*
2  * This file is part of JSTUN. 
3  * 
4  * Copyright (c) 2005 Thomas King <king@t-king.de> - All rights
5  * reserved.
6  * 
7  * This software is licensed under either the GNU Public License (GPL),
8  * or the Apache 2.0 license. Copies of both license agreements are
9  * included in this distribution.
10  */
11
12 package de.javawi.jstun.attribute;
13
14 import java.util.logging.*;
15
16 import de.javawi.jstun.util.*;
17
18
19 public abstract class MessageAttribute implements MessageAttributeInterface {
20         private static Logger logger = Logger.getLogger("de.javawi.stun.util.MessageAttribute");
21         MessageAttributeType type;
22         
23         public MessageAttribute() {
24         }
25         
26         public MessageAttribute(MessageAttributeType type) {
27                 setType(type);
28         }
29         
30         public void setType(MessageAttributeType type) {
31                 this.type = type;
32         }
33         
34         public MessageAttribute.MessageAttributeType getType() {
35                 return type;
36         }
37         
38         public static int typeToInteger(MessageAttributeType type) {
39                 if (type == MessageAttributeType.MappedAddress) return MAPPEDADDRESS;
40                 if (type == MessageAttributeType.ResponseAddress) return RESPONSEADDRESS;
41                 if (type == MessageAttributeType.ChangeRequest) return CHANGEREQUEST;
42                 if (type == MessageAttributeType.SourceAddress) return SOURCEADDRESS;
43                 if (type == MessageAttributeType.ChangedAddress) return CHANGEDADDRESS;
44                 if (type == MessageAttributeType.Username) return USERNAME;
45                 if (type == MessageAttributeType.Password) return PASSWORD;
46                 if (type == MessageAttributeType.MessageIntegrity) return MESSAGEINTEGRITY;
47                 if (type == MessageAttributeType.ErrorCode) return ERRORCODE;
48                 if (type == MessageAttributeType.UnknownAttribute) return UNKNOWNATTRIBUTE;
49                 if (type == MessageAttributeType.ReflectedFrom) return REFLECTEDFROM;
50                 if (type == MessageAttributeType.Dummy) return DUMMY;
51                 //turn
52                 if (type == MessageAttributeType.ChannelNumber) return CHANNELNUMBER;
53                 if (type == MessageAttributeType.LifeTime) return LIFETIME;
54                 if (type == MessageAttributeType.Bandwidth ) return BANDWIDTH;
55                 if (type == MessageAttributeType.PeerAddress ) return PEERADDRESS;
56                 if (type == MessageAttributeType.Data ) return DATA;
57                 if (type == MessageAttributeType.RelayAddress ) return RELAYADDRESS;
58                 if (type == MessageAttributeType.RequestedProps ) return REQUESTEDPROPS;
59                 if (type == MessageAttributeType.RequestedTransport ) return REQUESTEDTRANSPORT;
60                 if (type == MessageAttributeType.ReservationToken ) return RESERVATIONTOKEN;
61                 return -1;
62         }
63         
64         public static MessageAttributeType intToType(long type) {
65                 if (type == MAPPEDADDRESS) return MessageAttributeType.MappedAddress;
66                 if (type == RESPONSEADDRESS) return MessageAttributeType.ResponseAddress;
67                 if (type == CHANGEREQUEST) return MessageAttributeType.ChangeRequest;
68                 if (type == SOURCEADDRESS) return MessageAttributeType.SourceAddress;
69                 if (type == CHANGEDADDRESS) return MessageAttributeType.ChangedAddress;
70                 if (type == USERNAME) return MessageAttributeType.Username;
71                 if (type == PASSWORD) return MessageAttributeType.Password;
72                 if (type == MESSAGEINTEGRITY) return MessageAttributeType.MessageIntegrity;
73                 if (type == ERRORCODE) return MessageAttributeType.ErrorCode;
74                 if (type == UNKNOWNATTRIBUTE) return MessageAttributeType.UnknownAttribute;
75                 if (type == REFLECTEDFROM) return MessageAttributeType.ReflectedFrom;
76                 if (type == DUMMY) return MessageAttributeType.Dummy;
77                 //turn
78                 if (type == CHANNELNUMBER) return MessageAttributeType.ChannelNumber;
79                 if (type == LIFETIME) return MessageAttributeType.LifeTime;
80                 if (type == BANDWIDTH) return MessageAttributeType.Bandwidth;
81                 if (type == PEERADDRESS) return MessageAttributeType.PeerAddress;
82                 if (type == DATA) return MessageAttributeType.Data;
83                 if (type == RELAYADDRESS) return MessageAttributeType.RelayAddress;
84                 if (type == REQUESTEDPROPS) return MessageAttributeType.RequestedProps;
85                 if (type == REQUESTEDTRANSPORT) return MessageAttributeType.RequestedTransport;
86                 if (type == RESERVATIONTOKEN) return MessageAttributeType.ReservationToken;
87                 return null;
88         }
89         
90         abstract public byte[] getBytes() throws UtilityException;
91         //abstract public MessageAttribute parse(byte[] data) throws MessageAttributeParsingException;
92         
93         public int getLength() throws UtilityException {
94                 int length = getBytes().length;
95                 return length;
96         }
97         
98         public static MessageAttribute parseCommonHeader(byte[] data) throws MessageAttributeParsingException {
99                 try {                   
100                         byte[] typeArray = new byte[2];
101                         System.arraycopy(data, 0, typeArray, 0, 2);
102                         int type = Utility.twoBytesToInteger(typeArray);
103                         byte[] lengthArray = new byte[2];
104                         System.arraycopy(data, 2, lengthArray, 0, 2);
105                         int lengthValue = Utility.twoBytesToInteger(lengthArray);
106                         byte[] valueArray = new byte[lengthValue];
107                         System.arraycopy(data, 4, valueArray, 0, lengthValue);
108                         MessageAttribute ma;
109                         switch (type) {
110                         case MAPPEDADDRESS: ma = MappedAddress.parse(valueArray); break;
111                         case RESPONSEADDRESS: ma = ResponseAddress.parse(valueArray); break;
112                         case CHANGEREQUEST: ma = ChangeRequest.parse(valueArray); break;
113                         case SOURCEADDRESS: ma = SourceAddress.parse(valueArray); break;
114                         case CHANGEDADDRESS: ma = ChangedAddress.parse(valueArray); break;
115                         case USERNAME: ma = Username.parse(valueArray); break;
116                         case PASSWORD: ma = Password.parse(valueArray); break;
117                         case MESSAGEINTEGRITY: ma = MessageIntegrity.parse(valueArray); break;
118                         case ERRORCODE: ma = ErrorCode.parse(valueArray); break;
119                         case UNKNOWNATTRIBUTE: ma = UnknownAttribute.parse(valueArray); break;
120                         case REFLECTEDFROM: ma = ReflectedFrom.parse(valueArray); break;
121                         default:
122                                 if (type <= 0x7fff) {
123                                         throw new UnknownMessageAttributeException("Unkown mandatory message attribute", intToType(type));
124                                 } else {
125                                         logger.finer("MessageAttribute with type " + type + " unkown.");
126                                         ma = Dummy.parse(valueArray);
127                                         break;
128                                 }
129                         }
130                         return ma;
131                 } catch (UtilityException ue) {
132                         throw new MessageAttributeParsingException("Parsing error");
133                 }
134         }
135 }