Code covered by the BSD License  

Highlights from
Ethernet Header Extraction Tool

image thumbnail
from Ethernet Header Extraction Tool by Mike Donovan
This tool reads in a TCP/UDP file captured by Ethereal and extracts selected header fields.

etherealParser2(varargin)
function varargout = etherealParser2(varargin)
% ETHEREALPARSER2 M-file for etherealParser2.fig
%      ETHEREALPARSER2, by itself, creates a new ETHEREALPARSER2 or raises the existing
%      singleton*.
%
%      H = ETHEREALPARSER2 returns the handle to a new ETHEREALPARSER2 or the handle to
%      the existing singleton*.
%
%      ETHEREALPARSER2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ETHEREALPARSER2.M with the given input arguments.
%
%      ETHEREALPARSER2('Property','Value',...) creates a new ETHEREALPARSER2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before etherealParser2_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to etherealParser2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help etherealParser2

% Last Modified by GUIDE v2.5 26-Jan-2006 21:45:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @etherealParser2_OpeningFcn, ...
                   'gui_OutputFcn',  @etherealParser2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before etherealParser2 is made visible.
function etherealParser2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to etherealParser2 (see VARARGIN)

% Choose default command line output for etherealParser2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes etherealParser2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = etherealParser2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in MACType.
function MACType_Callback(hObject, eventdata, handles)
% hObject    handle to MACType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of MACType


% --- Executes on button press in MACSourceAddress.
function MACSourceAddress_Callback(hObject, eventdata, handles)
% hObject    handle to MACSourceAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of MACSourceAddress


% --- Executes on button press in IPTotalLength.
function IPTotalLength_Callback(hObject, eventdata, handles)
% hObject    handle to IPTotalLength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPTotalLength


% --- Executes on button press in IPVersion.
function IPVersion_Callback(hObject, eventdata, handles)
% hObject    handle to IPVersion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPVersion


% --- Executes on button press in IPTimeToLive.
function IPTimeToLive_Callback(hObject, eventdata, handles)
% hObject    handle to IPTimeToLive (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPTimeToLive


% --- Executes on button press in IPProtocol.
function IPProtocol_Callback(hObject, eventdata, handles)
% hObject    handle to IPProtocol (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPProtocol


% --- Executes on button press in IPHdrChecksum.
function IPHdrChecksum_Callback(hObject, eventdata, handles)
% hObject    handle to IPHdrChecksum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPHdrChecksum


% --- Executes on button press in IPSourceAddress.
function IPSourceAddress_Callback(hObject, eventdata, handles)
% hObject    handle to IPSourceAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPSourceAddress


% --- Executes on button press in IPDestAddress.
function IPDestAddress_Callback(hObject, eventdata, handles)
% hObject    handle to IPDestAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPDestAddress


% --- Executes on button press in IPFragOffset.
function IPFragOffset_Callback(hObject, eventdata, handles)
% hObject    handle to IPFragOffset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPFragOffset


% --- Executes on button press in IPDiffServeField.
function IPDiffServeField_Callback(hObject, eventdata, handles)
% hObject    handle to IPDiffServeField (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPDiffServeField


% --- Executes on button press in IPHdrLength.
function IPHdrLength_Callback(hObject, eventdata, handles)
% hObject    handle to IPHdrLength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPHdrLength


% --- Executes on button press in IPFlags.
function IPFlags_Callback(hObject, eventdata, handles)
% hObject    handle to IPFlags (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPFlags


% --- Executes on button press in IPID.
function IPID_Callback(hObject, eventdata, handles)
% hObject    handle to IPID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of IPID


% --- Executes on button press in UDPSourcePort.
function UDPSourcePort_Callback(hObject, eventdata, handles)
% hObject    handle to UDPSourcePort (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of UDPSourcePort


% --- Executes on button press in UDPDestPort.
function UDPDestPort_Callback(hObject, eventdata, handles)
% hObject    handle to UDPDestPort (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of UDPDestPort


% --- Executes on button press in UDPLength.
function UDPLength_Callback(hObject, eventdata, handles)
% hObject    handle to UDPLength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of UDPLength


% --- Executes on button press in UDPChecksum.
function UDPChecksum_Callback(hObject, eventdata, handles)
% hObject    handle to UDPChecksum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of UDPChecksum


% --- Executes on button press in pbBrowse.
function pbBrowse_Callback(hObject, eventdata, handles)
% hObject    handle to pbBrowse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[fileName,pathName]=uigetfile('FilterSpec');
fullName=[pathName fileName];
set(handles.filename,'String',fullName);

function filename_Callback(hObject, eventdata, handles)
% hObject    handle to filename (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of filename as text
%        str2double(get(hObject,'String')) returns contents of filename as a double


% --- Executes during object creation, after setting all properties.
function filename_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filename (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function IP1_Callback(hObject, eventdata, handles)
% hObject    handle to IP1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of IP1 as text
%        str2double(get(hObject,'String')) returns contents of IP1 as a double


% --- Executes during object creation, after setting all properties.
function IP1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IP1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function IP2_Callback(hObject, eventdata, handles)
% hObject    handle to IP2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of IP2 as text
%        str2double(get(hObject,'String')) returns contents of IP2 as a double


% --- Executes during object creation, after setting all properties.
function IP2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IP2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function IP3_Callback(hObject, eventdata, handles)
% hObject    handle to IP3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of IP3 as text
%        str2double(get(hObject,'String')) returns contents of IP3 as a double


% --- Executes during object creation, after setting all properties.
function IP3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IP3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function IP4_Callback(hObject, eventdata, handles)
% hObject    handle to IP4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of IP4 as text
%        str2double(get(hObject,'String')) returns contents of IP4 as a double


% --- Executes during object creation, after setting all properties.
function IP4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IP4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nPkts_Callback(hObject, eventdata, handles)
% hObject    handle to nPkts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nPkts as text
%        str2double(get(hObject,'String')) returns contents of nPkts as a double


% --- Executes during object creation, after setting all properties.
function nPkts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nPkts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in MACDestAddress.
function MACDestAddress_Callback(hObject, eventdata, handles)
% hObject    handle to MACDestAddress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of MACDestAddress


% --- Executes on button press in TCPSourcePort.
function TCPSourcePort_Callback(hObject, eventdata, handles)
% hObject    handle to TCPSourcePort (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPSourcePort


% --- Executes on button press in TCPSeqNumber.
function TCPSeqNumber_Callback(hObject, eventdata, handles)
% hObject    handle to TCPSeqNumber (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPSeqNumber


% --- Executes on button press in TCPAckNumber.
function TCPAckNumber_Callback(hObject, eventdata, handles)
% hObject    handle to TCPAckNumber (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPAckNumber


% --- Executes on button press in TCPHdrLength.
function TCPHdrLength_Callback(hObject, eventdata, handles)
% hObject    handle to TCPHdrLength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPHdrLength


% --- Executes on button press in TCPChecksum.
function TCPChecksum_Callback(hObject, eventdata, handles)
% hObject    handle to TCPChecksum (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPChecksum


% --- Executes on button press in TCPSeqAck.
function TCPSeqAck_Callback(hObject, eventdata, handles)
% hObject    handle to TCPSeqAck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPSeqAck


% --- Executes on button press in TCPDestPort.
function TCPDestPort_Callback(hObject, eventdata, handles)
% hObject    handle to TCPDestPort (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPDestPort


% --- Executes on button press in TCPFlags.
function TCPFlags_Callback(hObject, eventdata, handles)
% hObject    handle to TCPFlags (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPFlags


% --- Executes on button press in TCPWindowSize.
function TCPWindowSize_Callback(hObject, eventdata, handles)
% hObject    handle to TCPWindowSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of TCPWindowSize


% --- Executes on button press in pbProcessFile.
function pbProcessFile_Callback(hObject, eventdata, handles)
% hObject    handle to pbProcessFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%% Open Ethereal Capture File and skip Capture ID Bytes
fullName=get(handles.filename,'String');
fid=fopen(fullName);

s=fseek(fid,24,'cof');

%% Initialize data variables
nPkts=str2num(get(handles.nPkts,'String'));
ts=zeros(nPkts,7);
len=zeros(nPkts,1);
IP(1)=str2num(get(handles.IP1,'String'));
IP(2)=str2num(get(handles.IP2,'String'));
IP(3)=str2num(get(handles.IP3,'String'));
IP(4)=str2num(get(handles.IP4,'String'));

% Set up empty arrays for selected Ethernet fields
bitfield=zeros(28,1);
nFields=0;
fieldNames=[];
% MAC fields
tv=get(handles.MACDestAddress,'Value');
if (tv==1)
    MACDestAddress=zeros(nPkts,6);
    bitfield(1)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'MACDestAddress   '];
end
tv=get(handles.MACSourceAddress,'Value');
if (tv==1)
    MACSourceAddress=zeros(nPkts,6);
    bitfield(2)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'MACSourceAddress   '];
end
tv=get(handles.MACType,'Value');
if (tv==1)
    MACType=zeros(nPkts,2);
    bitfield(3)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'MACType   '];
end

% IP Fields
tv=get(handles.IPVersion,'Value');
if (tv==1)
    IPVersion=zeros(nPkts,1);
    bitfield(4)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPVersion   '];
end
tv=get(handles.IPHdrLength,'Value');
if (tv==1)
    IPHdrLength=zeros(nPkts,1);
    bitfield(5)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPHdrLength   '];
end
tv=get(handles.IPDiffServeField,'Value');
if (tv==1)
    IPDiffServeField=zeros(nPkts,1);
    bitfield(6)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPDiffServeField   '];
end
tv=get(handles.IPTotalLength,'Value');
if (tv==1)
    IPTotalLength=zeros(nPkts,2);
    bitfield(7)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPTotalLength   '];
end
tv=get(handles.IPID,'Value');
if (tv==1)
    IPID=zeros(nPkts,2);
    bitfield(8)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPID   '];
end
tv=get(handles.IPFlags,'Value');
if (tv==1)
    IPFlags=zeros(nPkts,1);
    bitfield(9)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPFlags   '];
end
tv=get(handles.IPFragOffset,'Value');
if (tv==1)
    IPFragOffset=zeros(nPkts,1);
    bitfield(10)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPFragOffset   '];
end
tv=get(handles.IPTimeToLive,'Value');
if (tv==1)
    IPTimeToLive=zeros(nPkts,1);
    bitfield(11)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPTimeToLive   '];
end
tv=get(handles.IPProtocol,'Value');
if (tv==1)
    IPProtocol=zeros(nPkts,1);
    bitfield(12)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPProtocol   '];
end
tv=get(handles.IPHdrChecksum,'Value');
if (tv==1)
    IPHdrChecksum=zeros(nPkts,2);
    bitfield(13)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPHdrChecksum   '];
end
tv=get(handles.IPSourceAddress,'Value');
if (tv==1)
    IPSourceAddress=zeros(nPkts,4);
    bitfield(14)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPSourceAddress   '];
end
tv=get(handles.IPDestAddress,'Value');
if (tv==1)
    IPDestAddress=zeros(nPkts,4);
    bitfield(15)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'IPDestAddress   '];
end

% TCP Fields
tv=get(handles.TCPSourcePort,'Value');
if (tv==1)
    TCPSourcePort=zeros(nPkts,2);
    bitfield(16)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPSourcePort   '];
end
tv=get(handles.TCPDestPort,'Value');
if (tv==1)
    TCPDestPort=zeros(nPkts,2);
    bitfield(17)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPDestPort   '];
end
tv=get(handles.TCPSeqNumber,'Value');
if (tv==1)
    TCPSeqNumber=zeros(nPkts,4);
    bitfield(18)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPSeqNumber   '];
end
tv=get(handles.TCPAckNumber,'Value');
if (tv==1)
    TCPAckNumber=zeros(nPkts,4);
    bitfield(19)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPAckNumber   '];
end
tv=get(handles.TCPHdrLength,'Value');
if (tv==1)
    TCPHdrLength=zeros(nPkts,1);
    bitfield(20)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPHdrLength   '];
end
tv=get(handles.TCPFlags,'Value');
if (tv==1)
    TCPFlags=zeros(nPkts,1);
    bitfield(21)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPFlags   '];
end
tv=get(handles.TCPWindowSize,'Value');
if (tv==1)
    TCPWindowSize=zeros(nPkts,2);
    bitfield(22)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPWindowSize   '];
end
tv=get(handles.TCPChecksum,'Value');
if (tv==1)
    TCPChecksum=zeros(nPkts,2);
    bitfield(23)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPChecksum   '];
end
tv=get(handles.TCPSeqAck,'Value');
if (tv==1)
    TCPSeqAck=zeros(nPkts,2);
    bitfield(24)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'TCPSeqAck   '];
end

% UDP Fields
tv=get(handles.UDPSourcePort,'Value');
if (tv==1)
    UDPSourcePort=zeros(nPkts,2);
    bitfield(25)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'UDPSourcePort    '];
end
tv=get(handles.UDPDestPort,'Value');
if (tv==1)
    UDPDestPort=zeros(nPkts,2);
    bitfield(26)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'UDPDestPort   '];
end
tv=get(handles.UDPLength,'Value');
if (tv==1)
    UDPLength=zeros(nPkts,2);
    bitfield(27)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'UDPLength   '];
end
tv=get(handles.UDPChecksum,'Value');
if (tv==1)
    UDPChecksum=zeros(nPkts,2);
    bitfield(28)=1;
    nFields=nFields+1;
    fieldNames=[fieldNames 'UDPChecksum   '];
end

fieldArray = zeros(nPkts,nFields);

%% Read data packets
% Ethereal Header with timestamp is first 16 bytes.  Timestamp is in first
% 7 bytes.
% MAC Header is 14 bytes
% IP Header has IP+Data length field in bytes 3&4
for i=1:nPkts
    lostSync=0;
    ethHdr=fread(fid,16,'uchar');    % Read Ethereal Header
    ts(i,:) = ethHdr(1:7)';  % Save timestamp bytes
    macHdr = fread(fid,14,'uchar'); % Read MAC Header
%    macType = macHdr(13)*256 + macHdr(14);
    ipHdr = fread(fid,20,'uchar');
    len(i) = ipHdr(3)*256+ipHdr(4);   % Calculate packet length
    protocol(i) = ipHdr(10);    % Check if protocol is TCP or UDP
    if (protocol(i)==6)
        tcpHdr = fread(fid,20,'uchar');
        fseek(fid,-20,'cof');
    elseif (protocol(i)==17)
        udpHdr = fread(fid,8,'uchar');
        fseek(fid,-8,'cof');
    end
        
    
    %   Make sure IP address is correct, otherwise sync has been lost
    for (j=1:4)
        if (ipHdr(j+12)~=IP(j))
            if (ipHdr(j+16)~=IP(j))
                lostSync=1;
                disp('Damaged File - Attempting To Repair Packet ');
                disp(i);
            end
        end
    end
    
    % Search for the IP address in the file 
    while (lostSync==1)
        testIP = fread(fid,4,'uchar');
        foundIP=0;
        for (j=1:4)     % test all 4 bytes and make sure they match the IP address
            if testIP(j)==IP(j)
                foundIP=foundIP+1;
            end
        end
        if (foundIP==4) % if the address is found, rewind to first IP byte, which should be 0x45
            lostSync=0;
            fseek(fid,-16,'cof'); % Don't know if you're rewinding from source or dest IP address
            if (fread(fid,1,'uchar')~=69)  % so test both cases
                fseek(fid,-5,'cof');
                if (fread(fid,1,'uchar')~=69)
                    lostSync=1; % if you don't find 0x45 your sync is still lost
                    fseek(fid,20,'cof') % advance to front and keep searching
                end
            end
            if (lostSync==0) % if sync is found, recalc length and advance to end of IP packet
                lenBytes=fread(fid,3,'uchar');
                len(i)=256*lenBytes(2)+lenBytes(3);
                fseek(fid,16,'cof');
                %% Also go back and recalc time stamp
                fseek(fid,-50,'cof');
                ts(i,:)=fread(fid,7,'uchar');
                fseek(fid,43,'cof');
            end
        else
            fseek(fid,-3,'cof'); % if you didn't find IP, rewind 3 bytes and go to top of while loop
        end
    end

    % Store the selected attributes in their arrays
    if (protocol(i)==6)
        tempArray = storeFields(bitfield,i,macHdr,ipHdr,tcpHdr);
    elseif (protocol==17)
        tempArray = storeFields(bitfield,i,macHdr,ipHdr,udpHdr);
    end
    fieldArray(i,:) = tempArray;

    fseek(fid,-20,'cof');    % Rewind to start of IP packet
    fseek(fid,len(i),'cof');    % Forward to start of next Ethereal packet
end

%% Calculate timestamps
tPkt=zeros(nPkts,1);
for (i=1:nPkts)
    tPkt(i)=(ts(i,5) + ts(i,6)*256 + ts(i,7)*256*256)/1000000 + ts(i,1) + ts(i,2)*256 + ts(i,3)*256*256 + ts(i,4)*256*256*256;
end
tPkt=tPkt-tPkt(1);

%% Make final data source
source = [tPkt len];
z=diff(source(:,1));
source2=[source(1:end-1,1) z];
len2=[source(1:end-1,1) len(1:end-1)];

assignin('base','lenArray',len2);
assignin('base','sourceArray',source2);
assignin('base','fieldArray',fieldArray);

fieldNames
fclose(fid);


% --- Executes on button press in pbExit.
function pbExit_Callback(hObject, eventdata, handles)
% hObject    handle to pbExit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

delete(handles.figure1);


%% Write selected fields into arrays
function array=storeFields(bf,i,macHdr,ipHdr,layer4Hdr)

columnLocation=1;

%% Test for MAC Fields
if bf(1)==1
    array(columnLocation) = macHdr(1)*256^5 + macHdr(2)*256^4 + macHdr(3)*256^3 + macHdr(4)*256^2 + macHdr(5)*256 + macHdr(6);
    columnLocation=columnLocation+1;
end
if bf(2)==1
    array(columnLocation) = macHdr(7)*256^5 + macHdr(8)*256^4 + macHdr(9)*256^3 + macHdr(10)*256^2 + macHdr(11)*256 + macHdr(12);
    columnLocation=columnLocation+1;
end
if bf(3)==1
    array(columnLocation) = macHdr(13)*256 + macHdr(14);
    columnLocation=columnLocation+1;
end

%% Test for IP Fields
if bf(4)==1
    array(columnLocation) = ipHdr(1);
    columnLocation=columnLocation+1;
end
if bf(5)==1
    array(columnLocation) = ipHdr(1);
    columnLocation=columnLocation+1;
end
if bf(6)==1
    array(columnLocation) = ipHdr(2);
    columnLocation=columnLocation+1;
end
if bf(7)==1
    array(columnLocation) = ipHdr(3)*256+ipHdr(4);
    columnLocation=columnLocation+1;
end
if bf(8)==1
    array(columnLocation) = ipHdr(5)*256+ipHdr(6);
    columnLocation=columnLocation+1;
end
if bf(9)==1
    array(columnLocation) = ipHdr(7);
    columnLocation=columnLocation+1;
end
if bf(10)==1
    array(columnLocation) = ipHdr(8);
    columnLocation=columnLocation+1;
end
if bf(11)==1
    array(columnLocation) = ipHdr(9);
    columnLocation=columnLocation+1;
end
if bf(12)==1
    array(columnLocation) = ipHdr(10);
    columnLocation=columnLocation+1;
end
if bf(13)==1
    array(columnLocation) = ipHdr(11)*256+ipHdr(12);
    columnLocation=columnLocation+1;
end
if bf(14)==1
    array(columnLocation) = ipHdr(13)*256^3+ipHdr(14)*256^2+ipHdr(15)*256+ipHdr(16);
    columnLocation=columnLocation+1;
end
if bf(15)==1
    array(columnLocation) = ipHdr(17)*256^3+ipHdr(18)*256^2+ipHdr(19)*256+ipHdr(20);
    columnLocation=columnLocation+1;
end

%% Test for TCP Fields
if bf(16)==1
    array(columnLocation) = layer4Hdr(1)*256+layer4Hdr(2);
    columnLocation=columnLocation+1;
end
if bf(17)==1
    array(columnLocation) = layer4Hdr(3)*256+layer4Hdr(4);
    columnLocation=columnLocation+1;
end
if bf(18)==1
    array(columnLocation) = layer4Hdr(5)*256^3+layer4Hdr(6)*256^2+layer4Hdr(7)*256+layer4Hdr(8);
    columnLocation=columnLocation+1;
end
if bf(19)==1
    array(columnLocation) = layer4Hdr(9)*256^3+layer4Hdr(10)*256^2+layer4Hdr(11)*256+layer4Hdr(12);
    columnLocation=columnLocation+1;
end
if bf(20)==1
    array(columnLocation) = layer4Hdr(13);
    columnLocation=columnLocation+1;
end
if bf(21)==1
    array(columnLocation) = layer4Hdr(14);
    columnLocation=columnLocation+1;
end
if bf(22)==1
    array(columnLocation) = layer4Hdr(15)*256+layer4Hdr(16);
    columnLocation=columnLocation+1;
end
if bf(23)==1
    array(columnLocation) = layer4Hdr(17)*256+layer4Hdr(18);
    columnLocation=columnLocation+1;
end
if bf(24)==1
    array(columnLocation) = layer4Hdr(19)*256+layer4Hdr(20);
    columnLocation=columnLocation+1;
end

%% Test for UDP Fields
if bf(25)==1
    array(columnLocation) = layer4Hdr(1)*256+layer4Hdr(2);
    columnLocation=columnLocation+1;
end
if bf(26)==1
    array(columnLocation) = layer4Hdr(3)*256+layer4Hdr(4);
    columnLocation=columnLocation+1;
end
if bf(27)==1
    array(columnLocation) = layer4Hdr(5)*256+layer4Hdr(6);
    columnLocation=columnLocation+1;
end
if bf(28)==1
    array(columnLocation) = layer4Hdr(7)*256+layer4Hdr(8);
    columnLocation=columnLocation+1;
end


Contact us at files@mathworks.com