github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/tcp/stream-internal.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package tcp
     4  
     5  import (
     6  	"syscall"
     7  
     8  	"github.com/GeniusesGroup/libgo/protocol"
     9  )
    10  
    11  func (s *Socket) reinit() {
    12  	// TODO:::
    13  }
    14  
    15  func (t *Socket) deinit() {
    16  	// TODO:::
    17  	t.timing.deinit()
    18  }
    19  
    20  func (s *Socket) checkSocket() (err protocol.Error) {
    21  	if s != nil {
    22  		err = syscall.EINVAL
    23  	}
    24  	return
    25  }
    26  
    27  func (s *Socket) incomeSegmentOnListenState(segment Packet) (err protocol.Error) {
    28  	if segment.FlagSYN() {
    29  		// err = s.sendSYNandACK()
    30  		s.setState(SocketState_SYN_RECEIVED)
    31  
    32  		// TODO::: set ACK timeout timer
    33  
    34  		// TODO::: If we return without any error, caller send the socket to the listeners if any exist.
    35  		// Provide a mechanism to let the listener to decide to accept the socket or refuse it??
    36  
    37  		// TODO::: attack?? SYN floods, SYN with payload, ...
    38  		return
    39  	}
    40  	// TODO::: attack??
    41  	return
    42  }
    43  
    44  func (s *Socket) incomeSegmentOnSynSentState(segment Packet) (err protocol.Error) {
    45  	if segment.FlagSYN() && segment.FlagACK() {
    46  		s.setState(SocketState_ESTABLISHED)
    47  		// TODO::: anything else??
    48  		return
    49  	}
    50  	// TODO::: if we receive syn from sender? attack??
    51  	// TODO::: attack??
    52  	return
    53  }
    54  
    55  func (s *Socket) incomeSegmentOnSynReceivedState(segment Packet) (err protocol.Error) {
    56  
    57  	return
    58  }
    59  
    60  func (s *Socket) incomeSegmentOnEstablishedState(segment Packet) (err protocol.Error) {
    61  	var payload = segment.Payload()
    62  	var sn = segment.SequenceNumber()
    63  	var exceptedNext = s.recv.next
    64  	if sn == exceptedNext {
    65  		s.sendACK()
    66  
    67  		err = s.recv.buf.Write(payload)
    68  
    69  		// TODO::: Due to CongestionControlAlgorithm, if a segment with push flag not send again
    70  		if segment.FlagPSH() {
    71  			err = s.checkPushFlag()
    72  			if err != nil {
    73  				return
    74  			}
    75  
    76  			// TODO:::
    77  			s.recv.sendPushFlagSignal()
    78  			s.Stream.ScheduleProcessingStream()
    79  		}
    80  	} else {
    81  		err = s.validateSequence(segment)
    82  	}
    83  	return
    84  }
    85  
    86  func (s *Socket) incomeSegmentOnFinWait1State(segment Packet) (err protocol.Error) {
    87  
    88  	return
    89  }
    90  
    91  func (s *Socket) incomeSegmentOnFinWait2State(segment Packet) (err protocol.Error) {
    92  
    93  	return
    94  }
    95  
    96  func (s *Socket) incomeSegmentOnCloseState(segment Packet) (err protocol.Error) {
    97  
    98  	return
    99  }
   100  
   101  func (s *Socket) incomeSegmentOnCloseWaitState(segment Packet) (err protocol.Error) {
   102  
   103  	return
   104  }
   105  
   106  func (s *Socket) incomeSegmentOnClosingState(segment Packet) (err protocol.Error) {
   107  
   108  	return
   109  }
   110  
   111  func (s *Socket) incomeSegmentOnLastAckState(segment Packet) (err protocol.Error) {
   112  
   113  	return
   114  }
   115  
   116  func (s *Socket) incomeSegmentOnTimeWaitState(segment Packet) (err protocol.Error) {
   117  
   118  	return
   119  }
   120  
   121  func (s *Socket) handleOptions(opts []byte) (err protocol.Error) {
   122  	for len(opts) > 0 {
   123  		var options = Options(opts)
   124  		switch options.Kind() {
   125  		case OptionKind_EndList:
   126  			return
   127  		case OptionKind_Nop:
   128  			// var opt Option
   129  
   130  			// opts = options.NextOption()
   131  		case OptionKind_MSS:
   132  			var optionMSS = optionMSS(options.Payload())
   133  			err = optionMSS.Process(s)
   134  			if err != nil {
   135  				return
   136  			}
   137  			opts = optionMSS.NextOption()
   138  		default:
   139  			// TODO:::
   140  		}
   141  	}
   142  	return
   143  }
   144  
   145  // setState change state of socket and send notification on socket state Channel.
   146  func (s *Socket) setState(state SocketState) {
   147  	s.status = state
   148  	select {
   149  	case s.state <- state:
   150  		// state can be delivered by
   151  	default:
   152  		// nothing to do just drop state because channel is block from other
   153  	}
   154  
   155  }
   156  
   157  // reset the socket and tell peer about reset
   158  func (s *Socket) reset() {
   159  	// TODO:::
   160  }
   161  
   162  func (s *Socket) close() (err protocol.Error) {
   163  	err = s.sendFIN()
   164  	s.deinit()
   165  	return
   166  }
   167  
   168  // sendSYN sending a segment with SYN flag on
   169  func (s *Socket) sendSYN() (err protocol.Error) {
   170  	// TODO:::
   171  	return
   172  }
   173  
   174  // sendACK sending ACKs in SYN-RECV and TIME-WAIT states
   175  func (s *Socket) sendACK() (err protocol.Error) {
   176  	s.recv.next++
   177  	// TODO:::
   178  	if DelayedAcknowledgment && s.delayedACK {
   179  		// go to queue
   180  	} else {
   181  		// send segment
   182  	}
   183  	return
   184  }
   185  
   186  // sendRST sending RST flag on segment to other side of the socket
   187  func (s *Socket) sendRST() (err protocol.Error) {
   188  	// TODO:::
   189  	return
   190  }
   191  
   192  // sendFIN sending FIN flag on segment to other side of the socket
   193  func (s *Socket) sendFIN() (err protocol.Error) {
   194  	// TODO:::
   195  	return
   196  }
   197  
   198  // ValidateSequence: validates sequence number of the segment
   199  // Return: TRUE if acceptable, FALSE if not acceptable
   200  func (s *Socket) validateSequence(segment Packet) (err protocol.Error) {
   201  	// TODO::: Change func args if no more data need
   202  	var payload = segment.Payload()
   203  	var sn = segment.SequenceNumber()
   204  	var exceptedNext = s.recv.next
   205  	// TODO::: Due to CongestionControlAlgorithm make a decision to change next
   206  	err = s.recv.buf.WriteIn(payload, (sn - exceptedNext))
   207  	return
   208  }
   209  
   210  // ValidateSequence: validates sequence number of the segment
   211  // Return: TRUE if acceptable, FALSE if not acceptable
   212  func (s *Socket) validateSequenceTemp(cur_ts uint32, p Packet, seq uint32, ack_seq uint32, payloadlen int) bool {
   213  	// https://github.com/mtcp-stack/mtcp/blob/master/mtcp/src/tcp_in.c#L108
   214  
   215  	return true
   216  }
   217  
   218  // needReset check socket state that need RST on ABORT according to RFC793
   219  func (s *Socket) needReset() bool {
   220  	var ss = s.status
   221  	return ss == SocketState_ESTABLISHED || ss == SocketState_CLOSE_WAIT ||
   222  		ss == SocketState_FIN_WAIT_1 || ss == SocketState_FIN_WAIT_2 || ss == SocketState_SYN_RECEIVED
   223  }
   224  
   225  func (s *Socket) sendPayload(b []byte) (n int, err error) {
   226  
   227  	return
   228  }
   229  
   230  // BlockInSelect waits for something to happen, which is one of the following conditions in the function body.
   231  func (s *Socket) blockInSelect() (err protocol.Error) {
   232  	// TODO::: check auto scheduling or block??
   233  
   234  loop:
   235  	for {
   236  		select {
   237  		// TODO::: if buffer not full but before get push flag go to full state??
   238  		case <-s.readTimer.Signal():
   239  			// TODO:::
   240  			// break
   241  		case flag := <-s.recv.flag:
   242  			switch flag {
   243  			case Flag_FIN:
   244  				s.readTimer.Stop()
   245  				// err = TODO:::
   246  				break loop
   247  			case Flag_RST:
   248  				s.readTimer.Stop()
   249  				// err = TODO:::
   250  				break loop
   251  			case Flag_PSH, Flag_URG:
   252  				break loop
   253  			default:
   254  				// TODO::: attack??
   255  				goto loop
   256  			}
   257  		}
   258  	}
   259  	return
   260  }