github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/tcp/socket-internal.go (about)

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