spdylay_session.c 77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Spdylay - SPDY Library
 *
 * Copyright (c) 2012 Tatsuhiro Tsujikawa
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "spdylay_session.h"

#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <arpa/inet.h>

#include "spdylay_helper.h"

35 36 37 38 39 40 41
/*
 * Returns non-zero if the number of opened streams is larger than or
 * equal to SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS value.
 */
static int spdylay_session_get_max_concurrent_streams_reached
(spdylay_session *session)
{
42 43 44 45 46
  uint32_t local_max, remote_max;
  local_max = session->local_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS];
  remote_max =
    session->remote_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS];
  return spdylay_min(local_max, remote_max)
47 48 49
    <= spdylay_map_size(&session->streams);
}

50 51 52 53 54 55 56 57
/*
 * Returns non-zero if |error| is non-fatal error.
 */
static int spdylay_is_non_fatal(int error)
{
  return error < 0 && error > SPDYLAY_ERR_FATAL;
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
58 59 60 61 62 63 64 65
/*
 * Returns non-zero if |error| is fatal error.
 */
static int spdylay_is_fatal(int error)
{
  return error < SPDYLAY_ERR_FATAL;
}

66 67
int spdylay_session_is_my_stream_id(spdylay_session *session,
                                    int32_t stream_id)
68 69 70 71 72 73
{
  int r;
  if(stream_id == 0) {
    return 0;
  }
  r = stream_id % 2;
74
  return (session->server && r == 0) || (!session->server && r == 1);
75 76
}

77 78
spdylay_stream* spdylay_session_get_stream(spdylay_session *session,
                                           int32_t stream_id)
79 80 81 82
{
  return (spdylay_stream*)spdylay_map_find(&session->streams, stream_id);
}

83
static int spdylay_outbound_item_compar(const void *lhsx, const void *rhsx)
84 85 86 87
{
  const spdylay_outbound_item *lhs, *rhs;
  lhs = (const spdylay_outbound_item*)lhsx;
  rhs = (const spdylay_outbound_item*)rhsx;
88 89 90 91 92
  if(lhs->pri == rhs->pri) {
    return (lhs->seq < rhs->seq) ? -1 : ((lhs->seq > rhs->seq) ? 1 : 0);
  } else {
    return lhs->pri-rhs->pri;
  }
93 94
}

95
static int spdylay_session_new(spdylay_session **session_ptr,
96
                               uint16_t version,
97 98
                               const spdylay_session_callbacks *callbacks,
                               void *user_data)
99 100
{
  int r;
101 102 103
  if(version != SPDYLAY_PROTO_SPDY2 && version != SPDYLAY_PROTO_SPDY3) {
    return SPDYLAY_ERR_UNSUPPORTED_VERSION;
  }
104 105
  *session_ptr = malloc(sizeof(spdylay_session));
  if(*session_ptr == NULL) {
106
    r = SPDYLAY_ERR_NOMEM;
107
    goto fail_session;
108 109
  }
  memset(*session_ptr, 0, sizeof(spdylay_session));
110

111 112
  (*session_ptr)->version = version;

113 114 115
  /* next_stream_id, last_recv_stream_id and next_unique_id are
     initialized in either spdylay_session_client_new or
     spdylay_session_server_new */
116

117 118 119
  (*session_ptr)->flow_control =
    (*session_ptr)->version == SPDYLAY_PROTO_SPDY3;

120
  (*session_ptr)->last_ping_unique_id = 0;
121

122 123
  (*session_ptr)->next_seq = 0;

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
124 125 126
  (*session_ptr)->goaway_flags = SPDYLAY_GOAWAY_NONE;
  (*session_ptr)->last_good_stream_id = 0;

127 128
  r = spdylay_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater,
                                   (*session_ptr)->version);
129
  if(r != 0) {
130
    goto fail_hd_deflater;
131
  }
132 133
  r = spdylay_zlib_inflate_hd_init(&(*session_ptr)->hd_inflater,
                                   (*session_ptr)->version);
134
  if(r != 0) {
135
    goto fail_hd_inflater;
136
  }
137
  spdylay_map_init(&(*session_ptr)->streams);
138 139
  r = spdylay_pq_init(&(*session_ptr)->ob_pq, spdylay_outbound_item_compar);
  if(r != 0) {
140
    goto fail_ob_pq;
141
  }
142 143
  r = spdylay_pq_init(&(*session_ptr)->ob_ss_pq, spdylay_outbound_item_compar);
  if(r != 0) {
144
    goto fail_ob_ss_pq;
145 146
  }

147 148
  (*session_ptr)->aob.framebuf = malloc
    (SPDYLAY_INITIAL_OUTBOUND_FRAMEBUF_LENGTH);
149
  if((*session_ptr)->aob.framebuf == NULL) {
150
    r = SPDYLAY_ERR_NOMEM;
151
    goto fail_aob_framebuf;
152
  }
153
  (*session_ptr)->aob.framebufmax = SPDYLAY_INITIAL_OUTBOUND_FRAMEBUF_LENGTH;
154 155 156

  (*session_ptr)->nvbuf = malloc(SPDYLAY_INITIAL_NV_BUFFER_LENGTH);
  if((*session_ptr)->nvbuf == NULL) {
157
    r = SPDYLAY_ERR_NOMEM;
158
    goto fail_nvbuf;
159 160 161
  }
  (*session_ptr)->nvbuflen = SPDYLAY_INITIAL_NV_BUFFER_LENGTH;

162 163
  spdylay_buffer_init(&(*session_ptr)->inflatebuf, 4096);

164 165 166
  memset((*session_ptr)->remote_settings, 0,
         sizeof((*session_ptr)->remote_settings));
  (*session_ptr)->remote_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS] =
167
    SPDYLAY_CONCURRENT_STREAMS_MAX;
168 169
  (*session_ptr)->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE] =
    SPDYLAY_INITIAL_WINDOW_SIZE;
170

171 172 173 174 175
  memset((*session_ptr)->local_settings, 0,
         sizeof((*session_ptr)->local_settings));
  (*session_ptr)->local_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS] =
    SPDYLAY_CONCURRENT_STREAMS_MAX;
  (*session_ptr)->local_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE] =
176 177
    SPDYLAY_INITIAL_WINDOW_SIZE;

178 179 180 181 182 183 184
  (*session_ptr)->callbacks = *callbacks;
  (*session_ptr)->user_data = user_data;

  (*session_ptr)->ibuf.mark = (*session_ptr)->ibuf.buf;
  (*session_ptr)->ibuf.limit = (*session_ptr)->ibuf.buf;
  
  (*session_ptr)->iframe.state = SPDYLAY_RECV_HEAD;
185 186
  (*session_ptr)->iframe.buf = malloc(SPDYLAY_INITIAL_INBOUND_FRAMEBUF_LENGTH);
  if((*session_ptr)->iframe.buf == NULL) {
187
    r = SPDYLAY_ERR_NOMEM;
188 189 190 191
    goto fail_iframe_buf;
  }
  (*session_ptr)->iframe.bufmax = SPDYLAY_INITIAL_INBOUND_FRAMEBUF_LENGTH;

192
  return 0;
193

194 195
 fail_iframe_buf:
  free((*session_ptr)->nvbuf);
196 197 198 199 200 201 202 203 204 205 206 207 208 209
 fail_nvbuf:
  free((*session_ptr)->aob.framebuf);
 fail_aob_framebuf:
  spdylay_pq_free(&(*session_ptr)->ob_ss_pq);
 fail_ob_ss_pq:
  spdylay_pq_free(&(*session_ptr)->ob_pq);
 fail_ob_pq:
  spdylay_map_free(&(*session_ptr)->streams);
  spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
 fail_hd_inflater:
  spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
 fail_hd_deflater:
  free(*session_ptr);
 fail_session:
210
  return r;
211 212
}

213
int spdylay_session_client_new(spdylay_session **session_ptr,
214
                               uint16_t version,
215 216 217 218
                               const spdylay_session_callbacks *callbacks,
                               void *user_data)
{
  int r;
219
  r = spdylay_session_new(session_ptr, version, callbacks, user_data);
220 221 222 223 224 225 226 227 228 229
  if(r == 0) {
    /* IDs for use in client */
    (*session_ptr)->next_stream_id = 1;
    (*session_ptr)->last_recv_stream_id = 0;
    (*session_ptr)->next_unique_id = 1;
  }
  return r;
}

int spdylay_session_server_new(spdylay_session **session_ptr,
230
                               uint16_t version,
231 232 233 234
                               const spdylay_session_callbacks *callbacks,
                               void *user_data)
{
  int r;
235
  r = spdylay_session_new(session_ptr, version, callbacks, user_data);
236 237 238 239 240 241 242 243 244 245
  if(r == 0) {
    (*session_ptr)->server = 1;
    /* IDs for use in client */
    (*session_ptr)->next_stream_id = 2;
    (*session_ptr)->last_recv_stream_id = 0;
    (*session_ptr)->next_unique_id = 2;
  }
  return r;
}

246
static void spdylay_free_streams(key_type key, void *val, void *ptr)
247 248 249 250 251
{
  spdylay_stream_free((spdylay_stream*)val);
  free(val);
}

252
static void spdylay_session_ob_pq_free(spdylay_pq *pq)
253
{
254 255
  while(!spdylay_pq_empty(pq)) {
    spdylay_outbound_item *item = (spdylay_outbound_item*)spdylay_pq_top(pq);
256 257
    spdylay_outbound_item_free(item);
    free(item);
258
    spdylay_pq_pop(pq);
259
  }
260 261 262
  spdylay_pq_free(pq);
}

263 264 265 266 267 268 269 270 271
static void spdylay_active_outbound_item_reset
(spdylay_active_outbound_item *aob)
{
  spdylay_outbound_item_free(aob->item);
  free(aob->item);
  aob->item = NULL;
  aob->framebuflen = aob->framebufoff = 0;
}

272 273 274 275 276
void spdylay_session_del(spdylay_session *session)
{
  if(session == NULL) {
    return;
  }
277
  spdylay_map_each(&session->streams, spdylay_free_streams, NULL);
278 279 280
  spdylay_map_free(&session->streams);
  spdylay_session_ob_pq_free(&session->ob_pq);
  spdylay_session_ob_pq_free(&session->ob_ss_pq);
281 282
  spdylay_zlib_deflate_free(&session->hd_deflater);
  spdylay_zlib_inflate_free(&session->hd_inflater);
283
  spdylay_active_outbound_item_reset(&session->aob);
284 285
  free(session->aob.framebuf);
  free(session->nvbuf);
286
  spdylay_buffer_free(&session->inflatebuf);
287
  free(session->iframe.buf);
288 289 290 291 292
  free(session);
}

int spdylay_session_add_frame(spdylay_session *session,
                              spdylay_frame_type frame_type,
293 294
                              spdylay_frame *frame,
                              void *aux_data)
295 296 297 298 299 300 301 302 303
{
  int r;
  spdylay_outbound_item *item;
  item = malloc(sizeof(spdylay_outbound_item));
  if(item == NULL) {
    return SPDYLAY_ERR_NOMEM;
  }
  item->frame_type = frame_type;
  item->frame = frame;
304
  item->aux_data = aux_data;
305
  item->seq = session->next_seq++;
306
  /* Set priority lowest at the moment. */
307
  item->pri = spdylay_session_get_pri_lowest(session);
308 309
  switch(frame_type) {
  case SPDYLAY_SYN_STREAM:
310
    item->pri = frame->syn_stream.pri;
311
    break;
312 313 314 315 316 317 318 319
  case SPDYLAY_SYN_REPLY: {
    spdylay_stream *stream = spdylay_session_get_stream
      (session, frame->syn_reply.stream_id);
    if(stream) {
      item->pri = stream->pri;
    }
    break;
  }
320 321 322 323 324 325 326 327 328
  case SPDYLAY_RST_STREAM: {
    spdylay_stream *stream = spdylay_session_get_stream
      (session, frame->rst_stream.stream_id);
    if(stream) {
      stream->state = SPDYLAY_STREAM_CLOSING;
      item->pri = stream->pri;
    }
    break;
  }
329 330
  case SPDYLAY_SETTINGS:
    /* Should SPDYLAY_SETTINGS have higher priority? */
331
    item->pri = -1;
332
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
333 334 335 336
  case SPDYLAY_NOOP:
    /* We don't have any public API to add NOOP, so here is
       unreachable. */
    abort();
337 338 339 340
  case SPDYLAY_PING:
    /* Ping has "height" priority. Give it -1. */
    item->pri = -1;
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
341 342 343
  case SPDYLAY_GOAWAY:
    /* Should GOAWAY have higher priority? */
    break;
344 345 346 347 348 349 350 351
  case SPDYLAY_HEADERS: {
    spdylay_stream *stream = spdylay_session_get_stream
      (session, frame->headers.stream_id);
    if(stream) {
      item->pri = stream->pri;
    }
    break;
  }
352 353 354 355 356 357 358 359
  case SPDYLAY_WINDOW_UPDATE: {
    spdylay_stream *stream = spdylay_session_get_stream
      (session, frame->window_update.stream_id);
    if(stream) {
      item->pri = stream->pri;
    }
    break;
  }
360 361 362 363 364 365 366 367
  case SPDYLAY_DATA: {
    spdylay_stream *stream = spdylay_session_get_stream
      (session, frame->data.stream_id);
    if(stream) {
      item->pri = stream->pri;
    }
    break;
  }
368
  };
369 370 371 372 373
  if(frame_type == SPDYLAY_SYN_STREAM) {
    r = spdylay_pq_push(&session->ob_ss_pq, item);
  } else {
    r = spdylay_pq_push(&session->ob_pq, item);
  }
374 375 376 377 378 379 380
  if(r != 0) {
    free(item);
    return r;
  }
  return 0;
}

381 382 383 384 385 386 387 388 389
int spdylay_session_add_rst_stream(spdylay_session *session,
                                   int32_t stream_id, uint32_t status_code)
{
  int r;
  spdylay_frame *frame;
  frame = malloc(sizeof(spdylay_frame));
  if(frame == NULL) {
    return SPDYLAY_ERR_NOMEM;
  }
390 391
  spdylay_frame_rst_stream_init(&frame->rst_stream, session->version,
                                stream_id, status_code);
392
  r = spdylay_session_add_frame(session, SPDYLAY_RST_STREAM, frame, NULL);
393 394 395 396 397 398 399 400
  if(r != 0) {
    spdylay_frame_rst_stream_free(&frame->rst_stream);
    free(frame);
    return r;
  }
  return 0;
}

401 402 403
spdylay_stream* spdylay_session_open_stream(spdylay_session *session,
                                            int32_t stream_id,
                                            uint8_t flags, uint8_t pri,
404 405
                                            spdylay_stream_state initial_state,
                                            void *stream_user_data)
406 407 408 409
{
  int r;
  spdylay_stream *stream = malloc(sizeof(spdylay_stream));
  if(stream == NULL) {
410
    return NULL;
411
  }
412
  spdylay_stream_init(stream, stream_id, flags, pri, initial_state,
413 414
                      session->remote_settings
                      [SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE],
415
                      stream_user_data);
416 417 418
  r = spdylay_map_insert(&session->streams, stream_id, stream);
  if(r != 0) {
    free(stream);
419
    stream = NULL;
420
  }
421
  return stream;
422 423
}

424 425
int spdylay_session_close_stream(spdylay_session *session, int32_t stream_id,
                                 spdylay_status_code status_code)
426 427 428
{
  spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
  if(stream) {
429 430 431 432 433 434
    if(stream->state != SPDYLAY_STREAM_INITIAL &&
       session->callbacks.on_stream_close_callback) {
      session->callbacks.on_stream_close_callback(session, stream_id,
                                                  status_code,
                                                  session->user_data);
    }
435
    spdylay_map_erase(&session->streams, stream_id);
436 437 438 439 440 441 442 443
    spdylay_stream_free(stream);
    free(stream);
    return 0;
  } else {
    return SPDYLAY_ERR_INVALID_ARGUMENT;
  }
}

444 445 446 447
int spdylay_session_close_stream_if_shut_rdwr(spdylay_session *session,
                                              spdylay_stream *stream)
{
  if((stream->shut_flags & SPDYLAY_SHUT_RDWR) == SPDYLAY_SHUT_RDWR) {
448 449
    return spdylay_session_close_stream(session, stream->stream_id,
                                        SPDYLAY_OK);
450 451 452 453 454
  } else {
    return 0;
  }
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
void spdylay_session_close_pushed_streams(spdylay_session *session,
                                          int32_t stream_id,
                                          spdylay_status_code status_code)
{
  spdylay_stream *stream;
  stream = spdylay_session_get_stream(session, stream_id);
  if(stream) {
    int i;
    for(i = 0; i < stream->pushed_streams_length; ++i) {
      spdylay_session_close_stream(session, stream->pushed_streams[i],
                                   status_code);
    }
  }
}

470 471 472
static int spdylay_predicate_stream_for_send(spdylay_stream *stream)
{
  if(stream == NULL) {
473
    return SPDYLAY_ERR_STREAM_CLOSED;
474 475 476 477 478 479 480
  } else if(stream->shut_flags & SPDYLAY_SHUT_WR) {
    return SPDYLAY_ERR_STREAM_SHUT_WR;
  } else {
    return 0;
  }
}

481
/*
482 483 484 485 486 487
 * This function checks SYN_REPLY with the stream ID |stream_id| can
 * be sent at this time.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
488
 * SPDYLAY_ERR_STREAM_CLOSED
489 490 491 492 493 494 495 496 497 498 499
 *     The stream is already closed or does not exist.
 * SPDYLAY_ERR_STREAM_SHUT_WR
 *     The transmission is not allowed for this stream (e.g., a frame
 *     with FIN flag set has already sent)
 * SPDYLAY_ERR_INVALID_STREAM_ID
 *     The stream ID is invalid.
 * SPDYLAY_ERR_STREAM_CLOSING
 *     RST_STREAM was queued for this stream.
 * SPDYLAY_ERR_INVALID_STREAM_STATE
 *     The state of the stream is not valid (e.g., SYN_REPLY has
 *     already sent).
500
 */
501 502
static int spdylay_session_predicate_syn_reply_send(spdylay_session *session,
                                                    int32_t stream_id)
503 504
{
  spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
505 506 507 508
  int r;
  r = spdylay_predicate_stream_for_send(stream);
  if(r != 0) {
    return r;
509 510
  }
  if(spdylay_session_is_my_stream_id(session, stream_id)) {
511
    return SPDYLAY_ERR_INVALID_STREAM_ID;
512
  } else {
513 514 515 516 517 518 519
    if(stream->state == SPDYLAY_STREAM_OPENING) {
      return 0;
    } else if(stream->state == SPDYLAY_STREAM_CLOSING) {
      return SPDYLAY_ERR_STREAM_CLOSING;
    } else {
      return SPDYLAY_ERR_INVALID_STREAM_STATE;
    }
520 521 522
  }
}

523
/*
524 525 526 527 528 529
 * This function checks HEADERS with the stream ID |stream_id| can
 * be sent at this time.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
530
 * SPDYLAY_ERR_STREAM_CLOSED
531 532 533 534 535 536 537 538 539
 *     The stream is already closed or does not exist.
 * SPDYLAY_ERR_STREAM_SHUT_WR
 *     The transmission is not allowed for this stream (e.g., a frame
 *     with FIN flag set has already sent)
 * SPDYLAY_ERR_STREAM_CLOSING
 *     RST_STREAM was queued for this stream.
 * SPDYLAY_ERR_INVALID_STREAM_STATE
 *     The state of the stream is not valid (e.g., if the local peer
 *     is receiving side and SYN_REPLY has not been sent).
540
 */
541 542
static int spdylay_session_predicate_headers_send(spdylay_session *session,
                                                  int32_t stream_id)
543 544
{
  spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
545 546 547 548
  int r;
  r = spdylay_predicate_stream_for_send(stream);
  if(r != 0) {
    return r;
549 550
  }
  if(spdylay_session_is_my_stream_id(session, stream_id)) {
551 552 553 554 555
    if(stream->state != SPDYLAY_STREAM_CLOSING) {
      return 0;
    } else {
      return SPDYLAY_ERR_STREAM_CLOSING;
    }
556
  } else {
557 558 559 560 561 562 563
    if(stream->state == SPDYLAY_STREAM_OPENED) {
      return 0;
    } else if(stream->state == SPDYLAY_STREAM_CLOSING) {
      return SPDYLAY_ERR_STREAM_CLOSING;
    } else {
      return SPDYLAY_ERR_INVALID_STREAM_STATE;
    }
564 565 566
  }
}

567
/*
568 569 570 571 572 573 574
 * This function checks WINDOW_UPDATE with the stream ID |stream_id|
 * can be sent at this time. Note that FIN flag of the previous frame
 * does not affect the transmission of the WINDOW_UPDATE frame.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
575
 * SPDYLAY_ERR_STREAM_CLOSED
576 577 578
 *     The stream is already closed or does not exist.
 * SPDYLAY_ERR_STREAM_CLOSING
 *     RST_STREAM was queued for this stream.
579
 */
580 581 582
static int spdylay_session_predicate_window_update_send
(spdylay_session *session,
 int32_t stream_id)
583 584 585
{
  spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
  if(stream == NULL) {
586
    return SPDYLAY_ERR_STREAM_CLOSED;
587 588
  }
  if(stream->state != SPDYLAY_STREAM_CLOSING) {
589
    return 0;
590 591
  } else {
    return SPDYLAY_ERR_STREAM_CLOSING;
592 593 594 595
  }
}

/*
596 597 598
 * Returns the maximum length of next data read. If the flow control
 * is enabled, the return value takes into account the current window
 * size.
599
 */
600 601
static size_t spdylay_session_next_data_read(spdylay_session *session,
                                             spdylay_stream *stream)
602 603 604
{
  if(session->flow_control == 0) {
    return SPDYLAY_DATA_PAYLOAD_LENGTH;
605 606 607
  } else if(stream->window_size > 0) {
    return stream->window_size < SPDYLAY_DATA_PAYLOAD_LENGTH ?
      stream->window_size : SPDYLAY_DATA_PAYLOAD_LENGTH;
608
  } else {
609
    return 0;
610 611 612
  }
}

613 614 615 616 617 618 619
/*
 * This function checks DATA with the stream ID |stream_id| can be
 * sent at this time.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
620
 * SPDYLAY_ERR_STREAM_CLOSED
621 622 623 624 625 626 627 628 629 630 631 632 633 634
 *     The stream is already closed or does not exist.
 * SPDYLAY_ERR_STREAM_SHUT_WR
 *     The transmission is not allowed for this stream (e.g., a frame
 *     with FIN flag set has already sent)
 * SPDYLAY_ERR_DEFERRED_DATA_EXIST
 *     Another DATA frame has already been deferred.
 * SPDYLAY_ERR_STREAM_CLOSING
 *     RST_STREAM was queued for this stream.
 * SPDYLAY_ERR_INVALID_STREAM_STATE
 *     The state of the stream is not valid (e.g., if the local peer
 *     is receiving side and SYN_REPLY has not been sent).
 */
static int spdylay_session_predicate_data_send(spdylay_session *session,
                                               int32_t stream_id)
635 636
{
  spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
637 638 639 640
  int r;
  r = spdylay_predicate_stream_for_send(stream);
  if(r != 0) {
    return r;
641
  }
642 643 644 645
  if(stream->deferred_data != NULL) {
    /* stream->deferred_data != NULL means previously queued DATA
       frame has not been sent. We don't allow new DATA frame is sent
       in this case. */
646
    return SPDYLAY_ERR_DEFERRED_DATA_EXIST;
647
  }
648
  if(spdylay_session_is_my_stream_id(session, stream_id)) {
649 650 651 652 653 654 655 656
    /* If stream->state is SPDYLAY_STREAM_CLOSING, RST_STREAM was
       queued but not yet sent. In this case, we won't send DATA
       frames. This is because in the current architecture, DATA and
       RST_STREAM in the same stream have same priority and DATA is
       small seq number. So RST_STREAM will not be sent until all DATA
       frames are sent. This is not desirable situation; we want to
       close stream as soon as possible. To achieve this, we remove
       DATA frame before RST_STREAM. */
657 658 659 660 661
    if(stream->state != SPDYLAY_STREAM_CLOSING) {
      return 0;
    } else {
      return SPDYLAY_ERR_STREAM_CLOSING;
    }
662
  } else {
663 664 665 666 667 668 669
    if(stream->state == SPDYLAY_STREAM_OPENED) {
      return 0;
    } else if(stream->state == SPDYLAY_STREAM_CLOSING) {
      return SPDYLAY_ERR_STREAM_CLOSING;
    } else {
      return SPDYLAY_ERR_INVALID_STREAM_STATE;
    }
670 671 672
  }
}

673 674
static ssize_t spdylay_session_prep_frame(spdylay_session *session,
                                          spdylay_outbound_item *item)
675
{
676
  /* TODO Get or validate stream ID here */
677
  /* TODO Validate assoc_stream_id here */
678 679 680
  ssize_t framebuflen;
  switch(item->frame_type) {
  case SPDYLAY_SYN_STREAM: {
681
    int32_t stream_id;
682
    spdylay_syn_stream_aux_data *aux_data;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
683 684 685
    if(session->goaway_flags) {
      /* When GOAWAY is sent or received, peer must not send new
         SYN_STREAM. */
686 687 688 689 690
      return SPDYLAY_ERR_SYN_STREAM_NOT_ALLOWED;
    }
    /* All 32bit signed stream IDs are spent. */
    if(session->next_stream_id > INT32_MAX) {
      return SPDYLAY_ERR_STREAM_ID_NOT_AVAILABLE;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
691
    }
692
    stream_id = session->next_stream_id;
693

694
    item->frame->syn_stream.stream_id = stream_id;
695
    session->next_stream_id += 2;
696 697 698
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_3to2(item->frame->syn_stream.nv);
    }
699 700 701 702
    framebuflen = spdylay_frame_pack_syn_stream(&session->aob.framebuf,
                                                &session->aob.framebufmax,
                                                &session->nvbuf,
                                                &session->nvbuflen,
703 704
                                                &item->frame->syn_stream,
                                                &session->hd_deflater);
705 706 707
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_2to3(item->frame->syn_stream.nv);
    }
708 709 710
    if(framebuflen < 0) {
      return framebuflen;
    }
711
    aux_data = (spdylay_syn_stream_aux_data*)item->aux_data;
712
    if(spdylay_session_open_stream(session, stream_id,
713 714
                                   item->frame->syn_stream.hd.flags,
                                   item->frame->syn_stream.pri,
715 716
                                   SPDYLAY_STREAM_INITIAL,
                                   aux_data->stream_user_data) == NULL) {
717
      return SPDYLAY_ERR_NOMEM;
718
    }
719 720 721
    break;
  }
  case SPDYLAY_SYN_REPLY: {
722 723 724 725 726
    int r;
    r = spdylay_session_predicate_syn_reply_send
      (session, item->frame->syn_reply.stream_id);
    if(r != 0) {
      return r;
727
    }
728 729 730
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_3to2(item->frame->syn_reply.nv);
    }
731 732 733 734
    framebuflen = spdylay_frame_pack_syn_reply(&session->aob.framebuf,
                                               &session->aob.framebufmax,
                                               &session->nvbuf,
                                               &session->nvbuflen,
735 736
                                               &item->frame->syn_reply,
                                               &session->hd_deflater);
737 738 739
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_2to3(item->frame->syn_reply.nv);
    }
740 741 742
    if(framebuflen < 0) {
      return framebuflen;
    }
743 744
    break;
  }
745
  case SPDYLAY_RST_STREAM:
746 747
    framebuflen = spdylay_frame_pack_rst_stream(&session->aob.framebuf,
                                                &session->aob.framebufmax,
748 749 750 751 752 753
                                                &item->frame->rst_stream);
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
  case SPDYLAY_SETTINGS:
754 755
    framebuflen = spdylay_frame_pack_settings(&session->aob.framebuf,
                                              &session->aob.framebufmax,
756 757 758 759 760
                                              &item->frame->settings);
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
761 762 763 764
  case SPDYLAY_NOOP:
    /* We don't have any public API to add NOOP, so here is
       unreachable. */
    abort();
765
  case SPDYLAY_PING:
766 767 768
    framebuflen = spdylay_frame_pack_ping(&session->aob.framebuf,
                                          &session->aob.framebufmax,
                                          &item->frame->ping);
769 770 771 772
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
773
  case SPDYLAY_HEADERS: {
774 775 776 777 778
    int r;
    r = spdylay_session_predicate_headers_send(session,
                                               item->frame->headers.stream_id);
    if(r != 0) {
      return r;
779
    }
780 781 782
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_3to2(item->frame->headers.nv);
    }
783 784 785 786 787 788
    framebuflen = spdylay_frame_pack_headers(&session->aob.framebuf,
                                             &session->aob.framebufmax,
                                             &session->nvbuf,
                                             &session->nvbuflen,
                                             &item->frame->headers,
                                             &session->hd_deflater);
789 790 791
    if(session->version == SPDYLAY_PROTO_SPDY2) {
      spdylay_frame_nv_2to3(item->frame->headers.nv);
    }
792 793 794 795 796
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
  }
797
  case SPDYLAY_WINDOW_UPDATE: {
798 799 800 801 802
    int r;
    r = spdylay_session_predicate_window_update_send
      (session, item->frame->window_update.stream_id);
    if(r != 0) {
      return r;
803 804 805 806 807 808 809 810 811
    }
    framebuflen = spdylay_frame_pack_window_update(&session->aob.framebuf,
                                                   &session->aob.framebufmax,
                                                   &item->frame->window_update);
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
  }
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
812 813 814 815 816 817
  case SPDYLAY_GOAWAY:
    if(session->goaway_flags & SPDYLAY_GOAWAY_SEND) {
      /* TODO The spec does not mandate that both endpoints have to
         exchange GOAWAY. This implementation allows receiver of first
         GOAWAY can sent its own GOAWAY to tell the remote peer that
         last-good-stream-id. */
818
      return SPDYLAY_ERR_GOAWAY_ALREADY_SENT;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
819
    }
820 821 822
    framebuflen = spdylay_frame_pack_goaway(&session->aob.framebuf,
                                            &session->aob.framebufmax,
                                            &item->frame->goaway);
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
823 824 825 826
    if(framebuflen < 0) {
      return framebuflen;
    }
    break;
827
  case SPDYLAY_DATA: {
828
    size_t next_readmax;
829
    spdylay_stream *stream;
830 831 832 833 834
    int r;
    r = spdylay_session_predicate_data_send(session,
                                            item->frame->data.stream_id);
    if(r != 0) {
      return r;
835
    }
836 837 838
    stream = spdylay_session_get_stream(session, item->frame->data.stream_id);
    /* Assuming stream is not NULL */
    assert(stream);
839 840
    next_readmax = spdylay_session_next_data_read(session, stream);
    if(next_readmax == 0) {
841 842 843
      spdylay_stream_defer_data(stream, item, SPDYLAY_DEFERRED_FLOW_CONTROL);
      return SPDYLAY_ERR_DEFERRED;
    }
844 845 846 847 848
    framebuflen = spdylay_session_pack_data(session,
                                            &session->aob.framebuf,
                                            &session->aob.framebufmax,
                                            next_readmax,
                                            &item->frame->data);
849
    if(framebuflen == SPDYLAY_ERR_DEFERRED) {
850
      spdylay_stream_defer_data(stream, item, SPDYLAY_DEFERRED_NONE);
851 852
      return SPDYLAY_ERR_DEFERRED;
    } else if(framebuflen < 0) {
853 854 855 856
      return framebuflen;
    }
    break;
  }
857 858 859 860 861 862
  default:
    framebuflen = SPDYLAY_ERR_INVALID_ARGUMENT;
  }
  return framebuflen;
}

863
spdylay_outbound_item* spdylay_session_get_ob_pq_top
864 865 866 867 868
(spdylay_session *session)
{
  return (spdylay_outbound_item*)spdylay_pq_top(&session->ob_pq);
}

869 870 871 872 873 874 875 876 877
spdylay_outbound_item* spdylay_session_get_next_ob_item
(spdylay_session *session)
{
  if(spdylay_pq_empty(&session->ob_pq)) {
    if(spdylay_pq_empty(&session->ob_ss_pq)) {
      return NULL;
    } else {
      /* Return item only when concurrent connection limit is not
         reached */
878
      if(spdylay_session_get_max_concurrent_streams_reached(session)) {
879
        return NULL;
880 881
      } else {
        return spdylay_pq_top(&session->ob_ss_pq);
882 883 884 885 886 887 888 889 890
      }
    }
  } else {
    if(spdylay_pq_empty(&session->ob_ss_pq)) {
      return spdylay_pq_top(&session->ob_pq);
    } else {
      spdylay_outbound_item *item, *syn_stream_item;
      item = spdylay_pq_top(&session->ob_pq);
      syn_stream_item = spdylay_pq_top(&session->ob_ss_pq);
891
      if(spdylay_session_get_max_concurrent_streams_reached(session) ||
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
         item->pri < syn_stream_item->pri ||
         (item->pri == syn_stream_item->pri &&
          item->seq < syn_stream_item->seq)) {
        return item;
      } else {
        return syn_stream_item;
      }
    }
  }
}

spdylay_outbound_item* spdylay_session_pop_next_ob_item
(spdylay_session *session)
{
  if(spdylay_pq_empty(&session->ob_pq)) {
    if(spdylay_pq_empty(&session->ob_ss_pq)) {
      return NULL;
    } else {
      /* Pop item only when concurrent connection limit is not
         reached */
912 913 914
      if(spdylay_session_get_max_concurrent_streams_reached(session)) {
        return NULL;
      } else {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
        spdylay_outbound_item *item;
        item = spdylay_pq_top(&session->ob_ss_pq);
        spdylay_pq_pop(&session->ob_ss_pq);
        return item;
      }
    }
  } else {
    if(spdylay_pq_empty(&session->ob_ss_pq)) {
      spdylay_outbound_item *item;
      item = spdylay_pq_top(&session->ob_pq);
      spdylay_pq_pop(&session->ob_pq);
      return item;
    } else {
      spdylay_outbound_item *item, *syn_stream_item;
      item = spdylay_pq_top(&session->ob_pq);
      syn_stream_item = spdylay_pq_top(&session->ob_ss_pq);
931
      if(spdylay_session_get_max_concurrent_streams_reached(session) ||
932 933 934 935 936 937 938 939 940 941 942 943 944
         item->pri < syn_stream_item->pri ||
         (item->pri == syn_stream_item->pri &&
          item->seq < syn_stream_item->seq)) {
        spdylay_pq_pop(&session->ob_pq);
        return item;
      } else {
        spdylay_pq_pop(&session->ob_ss_pq);
        return syn_stream_item;
      }
    }
  }
}

945 946 947 948 949 950 951 952 953 954 955
/*
 * Called after a frame is sent.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
 * SPDYLAY_ERR_NOMEM
 *     Out of memory.
 * SPDYLAY_ERR_CALLBACK_FAILURE
 *     The callback function failed.
 */
956 957
static int spdylay_session_after_frame_sent(spdylay_session *session)
{
958
  /* TODO handle FIN flag. */
959
  spdylay_outbound_item *item = session->aob.item;
960
  spdylay_frame *frame = session->aob.item->frame;
961
  spdylay_frame_type type = session->aob.item->frame_type;
962 963 964
  if(type == SPDYLAY_DATA) {
    if(session->callbacks.on_data_send_callback) {
      session->callbacks.on_data_send_callback
965 966
        (session,
         frame->data.eof ? frame->data.flags :
967
         (frame->data.flags & (~SPDYLAY_DATA_FLAG_FIN)),
968
         frame->data.stream_id,
969
         session->aob.framebuflen-SPDYLAY_HEAD_LEN, session->user_data);
970 971 972 973 974 975 976
    }
  } else {
    if(session->callbacks.on_ctrl_send_callback) {
      session->callbacks.on_ctrl_send_callback
        (session, type, frame, session->user_data);
    }
  }
977
  switch(type) {
978 979 980 981
  case SPDYLAY_SYN_STREAM: {
    spdylay_stream *stream =
      spdylay_session_get_stream(session, frame->syn_stream.stream_id);
    if(stream) {
982
      spdylay_syn_stream_aux_data *aux_data;
983
      stream->state = SPDYLAY_STREAM_OPENING;
984
      if(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
985 986
        spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
      }
987
      if(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
988 989 990
        spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
      }
      spdylay_session_close_stream_if_shut_rdwr(session, stream);
991 992 993
      /* We assume aux_data is a pointer to spdylay_syn_stream_aux_data */
      aux_data = (spdylay_syn_stream_aux_data*)item->aux_data;
      if(aux_data->data_prd) {
994
        int r;
995 996
        /* spdylay_submit_data() makes a copy of aux_data->data_prd */
        r = spdylay_submit_data(session, frame->syn_stream.stream_id,
997
                                SPDYLAY_DATA_FLAG_FIN, aux_data->data_prd);
998
        if(r != 0) {
999 1000
          /* FATAL error */
          assert(r < SPDYLAY_ERR_FATAL);
1001 1002 1003 1004
          /* TODO If r is not FATAL, we should send RST_STREAM. */
          return r;
        }
      }
1005 1006 1007 1008 1009 1010 1011 1012
    }
    break;
  }
  case SPDYLAY_SYN_REPLY: {
    spdylay_stream *stream =
      spdylay_session_get_stream(session, frame->syn_reply.stream_id);
    if(stream) {
      stream->state = SPDYLAY_STREAM_OPENED;
1013
      if(frame->syn_reply.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
1014 1015 1016
        spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
      }
      spdylay_session_close_stream_if_shut_rdwr(session, stream);
1017 1018 1019 1020 1021
      if(item->aux_data) {
        /* We assume aux_data is a pointer to spdylay_data_provider */
        spdylay_data_provider *data_prd =
          (spdylay_data_provider*)item->aux_data;
        int r;
1022
        r = spdylay_submit_data(session, frame->syn_reply.stream_id,
1023
                                SPDYLAY_DATA_FLAG_FIN, data_prd);
1024
        if(r != 0) {
1025 1026
          /* FATAL error */
          assert(r < SPDYLAY_ERR_FATAL);
1027 1028 1029 1030
          /* TODO If r is not FATAL, we should send RST_STREAM. */
          return r;
        }
      }
1031 1032 1033 1034
    }
    break;
  }
  case SPDYLAY_RST_STREAM:
1035 1036 1037 1038 1039 1040
    if(!session->server &&
       spdylay_session_is_my_stream_id(session, frame->rst_stream.stream_id) &&
       frame->rst_stream.status_code == SPDYLAY_CANCEL) {
      spdylay_session_close_pushed_streams(session, frame->rst_stream.stream_id,
                                           frame->rst_stream.status_code);
    }
1041 1042
    spdylay_session_close_stream(session, frame->rst_stream.stream_id,
                                 frame->rst_stream.status_code);
1043
    break;
1044 1045 1046
  case SPDYLAY_SETTINGS:
    /* nothing to do */
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1047 1048 1049 1050
  case SPDYLAY_NOOP:
    /* We don't have any public API to add NOOP, so here is
       unreachable. */
    abort();
1051 1052 1053 1054 1055
  case SPDYLAY_PING:
    /* We record the time now and show application code RTT when
       reply PING is received. */
    session->last_ping_unique_id = frame->ping.unique_id;
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1056 1057 1058
  case SPDYLAY_GOAWAY:
    session->goaway_flags |= SPDYLAY_GOAWAY_SEND;
    break;
1059 1060 1061 1062
  case SPDYLAY_HEADERS: {
    spdylay_stream *stream =
      spdylay_session_get_stream(session, frame->headers.stream_id);
    if(stream) {
1063
      if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
1064 1065 1066 1067 1068 1069
        spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
      }
      spdylay_session_close_stream_if_shut_rdwr(session, stream);
    }
    break;
  }
1070 1071
  case SPDYLAY_WINDOW_UPDATE:
    break;
1072
  case SPDYLAY_DATA:
1073
    if(frame->data.eof && (frame->data.flags & SPDYLAY_DATA_FLAG_FIN)) {
1074 1075 1076 1077 1078
      spdylay_stream *stream =
        spdylay_session_get_stream(session, frame->data.stream_id);
      if(stream) {
        spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
        spdylay_session_close_stream_if_shut_rdwr(session, stream);
1079
      }
1080 1081
    }
    break;
1082
  };
1083 1084
  if(type == SPDYLAY_DATA) {
    int r;
1085 1086
    /* If session is closed or RST_STREAM was queued, we won't send
       further data. */
1087
    if(frame->data.eof ||
1088 1089
       spdylay_session_predicate_data_send(session,
                                           frame->data.stream_id) != 0) {
1090 1091
      spdylay_active_outbound_item_reset(&session->aob);
    } else {
1092
      spdylay_outbound_item* item = spdylay_session_get_next_ob_item(session);
1093 1094 1095
      /* If priority of this stream is higher or equal to other stream
         waiting at the top of the queue, we continue to send this
         data. */
1096
      if(item == NULL || session->aob.item->pri <= item->pri) {
1097
        size_t next_readmax;
1098 1099 1100 1101
        spdylay_stream *stream;
        stream = spdylay_session_get_stream(session, frame->data.stream_id);
        /* Assuming stream is not NULL */
        assert(stream);
1102 1103
        next_readmax = spdylay_session_next_data_read(session, stream);
        if(next_readmax == 0) {
1104 1105 1106 1107 1108 1109
          spdylay_stream_defer_data(stream, session->aob.item,
                                    SPDYLAY_DEFERRED_FLOW_CONTROL);
          session->aob.item = NULL;
          spdylay_active_outbound_item_reset(&session->aob);
          return 0;
        }
1110 1111 1112 1113 1114
        r = spdylay_session_pack_data(session,
                                      &session->aob.framebuf,
                                      &session->aob.framebufmax,
                                      next_readmax,
                                      &frame->data);
1115
        if(r == SPDYLAY_ERR_DEFERRED) {
1116 1117
          spdylay_stream_defer_data(stream, session->aob.item,
                                    SPDYLAY_DEFERRED_NONE);
1118 1119 1120
          session->aob.item = NULL;
          spdylay_active_outbound_item_reset(&session->aob);
        } else if(r < 0) {
1121 1122
          /* We don't return other error code other than
             SPDYLAY_ERR_CALLBACK_FAILURE here. */
1123
          spdylay_active_outbound_item_reset(&session->aob);
1124
          return SPDYLAY_ERR_CALLBACK_FAILURE;
1125 1126 1127
        } else {
          session->aob.framebuflen = r;
          session->aob.framebufoff = 0;
1128
        }
1129
      } else {
1130 1131 1132 1133 1134
        r = spdylay_pq_push(&session->ob_pq, session->aob.item);
        if(r == 0) {
          session->aob.item = NULL;
          spdylay_active_outbound_item_reset(&session->aob);
        } else {
1135 1136
          /* FATAL error */
          assert(r < SPDYLAY_ERR_FATAL);
1137 1138 1139
          spdylay_active_outbound_item_reset(&session->aob);
          return r;
        }
1140 1141 1142 1143 1144 1145
      }
    }
  } else {
    spdylay_active_outbound_item_reset(&session->aob);
  }
  return 0;
1146 1147
}

1148 1149
int spdylay_session_send(spdylay_session *session)
{
1150
  int r;
1151
  while(1) {
1152 1153 1154 1155
    const uint8_t *data;
    size_t datalen;
    ssize_t sentlen;
    if(session->aob.item == NULL) {
1156
      spdylay_outbound_item *item;
1157
      ssize_t framebuflen;
1158 1159 1160 1161
      item = spdylay_session_pop_next_ob_item(session);
      if(item == NULL) {
        break;
      }
1162
      framebuflen = spdylay_session_prep_frame(session, item);
1163 1164 1165
      if(framebuflen == SPDYLAY_ERR_DEFERRED) {
        continue;
      } else if(framebuflen < 0) {
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        /* The library is responsible for the transmission of
           WINDOW_UPDATE frame, so we don't call error callback for
           it. */
        if(session->callbacks.on_ctrl_not_send_callback &&
           spdylay_is_non_fatal(framebuflen) &&
           item->frame_type != SPDYLAY_WINDOW_UPDATE) {
          session->callbacks.on_ctrl_not_send_callback(session,
                                                       item->frame_type,
                                                       item->frame,
                                                       framebuflen,
                                                       session->user_data);
        }
1178 1179
        spdylay_outbound_item_free(item);
        free(item);
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1180
        if(spdylay_is_fatal(framebuflen)) {
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1181 1182
          return framebuflen;
        } else {
1183
          continue;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1184
        }
1185 1186 1187
      }
      session->aob.item = item;
      session->aob.framebuflen = framebuflen;
1188 1189 1190 1191 1192 1193
      /* Call before_send callback */
      if(item->frame_type != SPDYLAY_DATA &&
         session->callbacks.before_ctrl_send_callback) {
        session->callbacks.before_ctrl_send_callback
          (session, item->frame_type, item->frame, session->user_data);
      }
1194 1195 1196
    }
    data = session->aob.framebuf + session->aob.framebufoff;
    datalen = session->aob.framebuflen - session->aob.framebufoff;
1197
    sentlen = session->callbacks.send_callback(session, data, datalen, 0,
1198 1199 1200 1201 1202
                                               session->user_data);
    if(sentlen < 0) {
      if(sentlen == SPDYLAY_ERR_WOULDBLOCK) {
        return 0;
      } else {
1203
        return SPDYLAY_ERR_CALLBACK_FAILURE;
1204 1205 1206
      }
    } else {
      session->aob.framebufoff += sentlen;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
      if(session->flow_control &&
         session->aob.item->frame_type == SPDYLAY_DATA) {
        spdylay_frame *frame;
        spdylay_stream *stream;
        frame = session->aob.item->frame;
        stream = spdylay_session_get_stream(session, frame->data.stream_id);
        if(stream) {
          stream->window_size -= spdylay_get_uint32(&session->aob.framebuf[4]);
        }
      }
1217 1218
      if(session->aob.framebufoff == session->aob.framebuflen) {
        /* Frame has completely sent */
1219 1220
        r = spdylay_session_after_frame_sent(session);
        if(r < 0) {
1221 1222
          /* FATAL */
          assert(r < SPDYLAY_ERR_FATAL);
1223 1224
          return r;
        }
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
      }
    }
  }
  return 0;
}

static void spdylay_inbound_buffer_shift(spdylay_inbound_buffer *ibuf)
{
  ptrdiff_t len = ibuf->limit-ibuf->mark;
  memmove(ibuf->buf, ibuf->mark, len);
  ibuf->limit = ibuf->buf+len;
  ibuf->mark = ibuf->buf;
}

static ssize_t spdylay_recv(spdylay_session *session)
{
  ssize_t r;
  size_t recv_max;
  if(session->ibuf.mark != session->ibuf.buf) {
    spdylay_inbound_buffer_shift(&session->ibuf);
  }
  recv_max = session->ibuf.buf+sizeof(session->ibuf.buf)-session->ibuf.limit;
  r = session->callbacks.recv_callback
1248
    (session, session->ibuf.limit, recv_max, 0, session->user_data);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  if(r > 0) {
    if(r > recv_max) {
      return SPDYLAY_ERR_CALLBACK_FAILURE;
    } else {
      session->ibuf.limit += r;
    }
  } else if(r < 0) {
    if(r != SPDYLAY_ERR_WOULDBLOCK) {
      r = SPDYLAY_ERR_CALLBACK_FAILURE;
    }
  }
  return r;
}

static size_t spdylay_inbound_buffer_avail(spdylay_inbound_buffer *ibuf)
{
  return ibuf->limit-ibuf->mark;
}

static void spdylay_inbound_frame_reset(spdylay_inbound_frame *iframe)
{
  iframe->state = SPDYLAY_RECV_HEAD;
  iframe->len = iframe->off = 0;
  iframe->ign = 0;
}

1275 1276 1277 1278 1279 1280 1281 1282 1283
static void spdylay_session_call_on_request_recv
(spdylay_session *session, int32_t stream_id)
{
  if(session->callbacks.on_request_recv_callback) {
    session->callbacks.on_request_recv_callback(session, stream_id,
                                                session->user_data);
  }
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
static void spdylay_session_call_on_ctrl_frame_received
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame)
{
  if(session->callbacks.on_ctrl_recv_callback) {
    session->callbacks.on_ctrl_recv_callback
      (session, type, frame, session->user_data);
  }
}

/*
 * Checks whether received stream_id is valid.
 * This function returns 1 if it succeeds, or 0.
 */
static int spdylay_session_is_new_peer_stream_id(spdylay_session *session,
                                                 int32_t stream_id)
{
  if(stream_id == 0) {
    return 0;
  }
  if(session->server) {
    return stream_id % 2 == 1 && session->last_recv_stream_id < stream_id;
  } else {
    return stream_id % 2 == 0 && session->last_recv_stream_id < stream_id;
  }
}

1310
/*
1311
 * Returns non-zero iff version == session->version
1312
 */
1313 1314
static int spdylay_session_check_version(spdylay_session *session,
                                         uint16_t version)
1315
{
1316
  return session->version == version;
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
}

/*
 * Returns non-zero iff name/value pairs |nv| are good shape.
 * Currently, we only checks whether names are lower cased. The spdy/2
 * spec requires that names must be lower cased.
 */
static int spdylay_session_check_nv(char **nv)
{
  int i;
  for(i = 0; nv[i]; i += 2) {
    int j;
    for(j = 0; nv[i][j] != '\0'; ++j) {
      if('A' <= nv[i][j] && nv[i][j] <= 'Z') {
        return 0;
      }
    }
  }
  return 1;
}

1338
/*
1339
 * Validates SYN_STREAM frame |frame|.  This function returns 0 if it
1340
 * succeeds, or non-zero spdylay_status_code.
1341
 */
1342 1343
static int spdylay_session_validate_syn_stream(spdylay_session *session,
                                               spdylay_syn_stream *frame)
1344
{
1345 1346
  if(!spdylay_session_is_new_peer_stream_id(session, frame->stream_id)) {
    return SPDYLAY_PROTOCOL_ERROR;
1347
  }
1348
  if(!spdylay_session_check_version(session, frame->hd.version)) {
1349 1350
    return SPDYLAY_UNSUPPORTED_VERSION;
  }
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  if(session->server) {
    if(frame->assoc_stream_id != 0) {
      return SPDYLAY_PROTOCOL_ERROR;
    }
  } else {
    if(frame->assoc_stream_id == 0) {
      /* spdy/2 spec: When a client receives a SYN_STREAM from the
         server with an Associated-To-Stream-ID of 0, it must reply with
         a RST_STREAM with error code INVALID_STREAM. */
      return SPDYLAY_INVALID_STREAM;
    }
1362
    if((frame->hd.flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) == 0 ||
1363 1364 1365 1366 1367 1368 1369
       frame->assoc_stream_id % 2 == 0 ||
       spdylay_session_get_stream(session, frame->assoc_stream_id) == NULL) {
      /* It seems spdy/2 spec does not say which status code should be
         returned in these cases. */
      return SPDYLAY_PROTOCOL_ERROR;
    }
  }
1370 1371 1372 1373 1374 1375 1376
  if(spdylay_session_get_max_concurrent_streams_reached(session)) {
    /* spdy/2 spec does not clearly say what to do when max concurrent
       streams number is reached. The mod_spdy sends
       SPDYLAY_REFUSED_STREAM and we think it is reasonable. So we
       follow it. */
    return SPDYLAY_REFUSED_STREAM;
  }
1377 1378 1379
  if(!spdylay_session_check_nv(frame->nv)) {
    return SPDYLAY_PROTOCOL_ERROR;
  }
1380
  return 0;
1381 1382
}

1383

1384 1385 1386 1387 1388 1389
static int spdylay_session_handle_invalid_stream
(spdylay_session *session,
 int32_t stream_id,
 spdylay_frame_type type,
 spdylay_frame *frame,
 spdylay_status_code status_code)
1390 1391
{
  int r;
1392
  r = spdylay_session_add_rst_stream(session, stream_id, status_code);
1393 1394 1395 1396 1397 1398 1399
  if(r != 0) {
    return r;
  }
  if(session->callbacks.on_invalid_ctrl_recv_callback) {
    session->callbacks.on_invalid_ctrl_recv_callback
      (session, type, frame, session->user_data);
  }
1400 1401 1402 1403 1404 1405
  return 0;
}

int spdylay_session_on_syn_stream_received(spdylay_session *session,
                                           spdylay_frame *frame)
{
1406 1407
  int r = 0;
  int status_code;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1408 1409 1410 1411
  if(session->goaway_flags) {
    /* We don't accept SYN_STREAM after GOAWAY is sent or received. */
    return 0;
  }
1412 1413 1414
  status_code = spdylay_session_validate_syn_stream(session,
                                                    &frame->syn_stream);
  if(status_code == 0) {
1415
    uint8_t flags = frame->syn_stream.hd.flags;
1416 1417
    if((flags & SPDYLAY_CTRL_FLAG_FIN) &&
       (flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL)) {
1418 1419 1420 1421
      /* If the stream is UNIDIRECTIONAL and FIN bit set, we can close
         stream upon receiving SYN_STREAM. So, the stream needs not to
         be opened. */
    } else {
1422 1423 1424 1425
      spdylay_stream *stream;
      stream = spdylay_session_open_stream(session, frame->syn_stream.stream_id,
                                           frame->syn_stream.hd.flags,
                                           frame->syn_stream.pri,
1426 1427
                                           SPDYLAY_STREAM_OPENING,
                                           NULL);
1428
      if(stream) {
1429
        if(flags & SPDYLAY_CTRL_FLAG_FIN) {
1430 1431
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
        }
1432
        if(flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
1433 1434 1435
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
        }
        /* We don't call spdylay_session_close_stream_if_shut_rdwr()
1436 1437
           here because either SPDYLAY_CTRL_FLAG_FIN or
           SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL is not set here. */
1438
      }
1439
    }
1440 1441 1442
    session->last_recv_stream_id = frame->syn_stream.stream_id;
    spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SYN_STREAM,
                                                frame);
1443
    if(flags & SPDYLAY_CTRL_FLAG_FIN) {
1444 1445
      spdylay_session_call_on_request_recv(session,
                                           frame->syn_stream.stream_id);
1446
      if(flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
1447 1448 1449 1450 1451 1452 1453
        /* Note that we call on_stream_close_callback without opening
           stream. */
        if(session->callbacks.on_stream_close_callback) {
          session->callbacks.on_stream_close_callback
            (session, frame->syn_stream.stream_id, SPDYLAY_OK,
             session->user_data);
        }
1454
      }
1455 1456
    }
  } else {
1457
    r = spdylay_session_handle_invalid_stream
1458 1459
      (session, frame->syn_stream.stream_id, SPDYLAY_SYN_STREAM, frame,
       status_code);
1460 1461 1462 1463 1464 1465 1466 1467 1468
  }
  return r;
}

int spdylay_session_on_syn_reply_received(spdylay_session *session,
                                          spdylay_frame *frame)
{
  int r = 0;
  int valid = 0;
1469
  spdylay_stream *stream;
1470
  if(!spdylay_session_check_version(session, frame->syn_reply.hd.version)) {
1471 1472
    return 0;
  }
1473 1474 1475 1476
  if((stream = spdylay_session_get_stream(session,
                                          frame->syn_reply.stream_id)) &&
     (stream->shut_flags & SPDYLAY_SHUT_RD) == 0 &&
     spdylay_session_check_nv(frame->syn_reply.nv)) {
1477
    if(spdylay_session_is_my_stream_id(session, frame->syn_reply.stream_id)) {
1478 1479 1480 1481 1482
      if(stream->state == SPDYLAY_STREAM_OPENING) {
        valid = 1;
        stream->state = SPDYLAY_STREAM_OPENED;
        spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SYN_REPLY,
                                                    frame);
1483
        if(frame->syn_reply.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
1484 1485 1486 1487
          /* This is the last frame of this stream, so disallow
             further receptions. */
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
          spdylay_session_close_stream_if_shut_rdwr(session, stream);
1488
        }
1489 1490 1491 1492 1493 1494 1495 1496 1497
      } else if(stream->state == SPDYLAY_STREAM_CLOSING) {
        /* This is race condition. SPDYLAY_STREAM_CLOSING indicates
           that we queued RST_STREAM but it has not been sent. It will
           eventually sent, so we just ignore this frame. */
        valid = 1;
      }
    }
  }
  if(!valid) {
1498 1499 1500
    r = spdylay_session_handle_invalid_stream
      (session, frame->syn_reply.stream_id, SPDYLAY_SYN_REPLY, frame,
       SPDYLAY_PROTOCOL_ERROR);
1501 1502
  }
  return r;
1503 1504
}

1505 1506 1507
int spdylay_session_on_rst_stream_received(spdylay_session *session,
                                           spdylay_frame *frame)
{
1508
  if(!spdylay_session_check_version(session, frame->rst_stream.hd.version)) {
1509 1510
    return 0;
  }
1511 1512 1513 1514 1515 1516
  if(session->server &&
     !spdylay_session_is_my_stream_id(session, frame->rst_stream.stream_id) &&
     frame->rst_stream.status_code == SPDYLAY_CANCEL) {
    spdylay_session_close_pushed_streams(session, frame->rst_stream.stream_id,
                                         frame->rst_stream.status_code);
  }
1517 1518
  spdylay_session_close_stream(session, frame->rst_stream.stream_id,
                               frame->rst_stream.status_code);
1519
  return 0;
1520 1521
}

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
static void spdylay_update_initial_window_size_func(key_type key, void *value,
                                                    void *ptr)
{
  int32_t *vals;
  vals = (int32_t*)ptr;
  spdylay_stream_update_initial_window_size((spdylay_stream*)value,
                                            vals[0], vals[1]);
}

/*
 * Updates the initial window size of all active streams.
 */
static void spdylay_session_update_initial_window_size
(spdylay_session *session,
 int32_t new_initial_window_size)
{
  int32_t vals[2];
  vals[0] = new_initial_window_size;
  vals[1] = session->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE];
  spdylay_map_each(&session->streams,
                   spdylay_update_initial_window_size_func,
                   vals);
}

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
void spdylay_session_update_local_settings(spdylay_session *session,
                                           spdylay_settings_entry *iv,
                                           size_t niv)
{
  int i;
  for(i = 0; i < niv; ++i) {
    assert(iv[i].settings_id > 0 && iv[i].settings_id <= SPDYLAY_SETTINGS_MAX);
    session->local_settings[iv[i].settings_id] = iv[i].value;
  }
}

1557 1558 1559
int spdylay_session_on_settings_received(spdylay_session *session,
                                         spdylay_frame *frame)
{
1560
  int i, check[SPDYLAY_SETTINGS_MAX+1];
1561
  if(!spdylay_session_check_version(session, frame->settings.hd.version)) {
1562 1563
    return 0;
  }
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
  /* Check ID/value pairs and persist them if necessary. */
  memset(check, 0, sizeof(check));
  for(i = 0; i < frame->settings.niv; ++i) {
    const spdylay_settings_entry *entry = &frame->settings.iv[i];
    /* SPDY/3 spec says if the multiple values for the same ID were
       found, use the first one and ignore the rest. */
    if(entry->settings_id > SPDYLAY_SETTINGS_MAX || entry->settings_id == 0 ||
       check[entry->settings_id] == 1) {
      continue;
    }
    check[entry->settings_id] = 1;
    if(entry->settings_id == SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE &&
       session->flow_control) {
      /* Update the initial window size of the all active streams */
      /* Check that initial_window_size < (1u << 31) */
      if(entry->value < (1u << 31)) {
        spdylay_session_update_initial_window_size(session, entry->value);
      }
    }
    session->remote_settings[entry->settings_id] = entry->value;
  }
1585 1586 1587 1588
  spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SETTINGS, frame);
  return 0;
}

1589 1590 1591 1592
int spdylay_session_on_ping_received(spdylay_session *session,
                                     spdylay_frame *frame)
{
  int r = 0;
1593
  if(!spdylay_session_check_version(session, frame->ping.hd.version)) {
1594 1595
    return 0;
  }
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
  if(frame->ping.unique_id != 0) {
    if(session->last_ping_unique_id == frame->ping.unique_id) {
      /* This is ping reply from peer */
      /* Assign 0 to last_ping_unique_id so that we can ignore same
         ID. */
      session->last_ping_unique_id = 0;
      spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_PING, frame);
    } else if((session->server && frame->ping.unique_id % 2 == 1) ||
              (!session->server && frame->ping.unique_id % 2 == 0)) {
      /* Peer sent ping, so ping it back */
      r = spdylay_session_add_ping(session, frame->ping.unique_id);
      spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_PING, frame);
    }
  }
  return r;
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1613 1614 1615
int spdylay_session_on_goaway_received(spdylay_session *session,
                                       spdylay_frame *frame)
{
1616
  if(!spdylay_session_check_version(session, frame->goaway.hd.version)) {
1617 1618
    return 0;
  }
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1619 1620 1621 1622 1623 1624
  session->last_good_stream_id = frame->goaway.last_good_stream_id;
  session->goaway_flags |= SPDYLAY_GOAWAY_RECV;
  spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_GOAWAY, frame);
  return 0;
}

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
int spdylay_session_on_window_update_received(spdylay_session *session,
                                              spdylay_frame *frame)
{
  spdylay_stream *stream;
  if(!spdylay_session_check_version(session, frame->window_update.hd.version)) {
    return 0;
  }
  if(!session->flow_control) {
    return 0;
  }
  stream = spdylay_session_get_stream(session, frame->window_update.stream_id);
  if(stream) {
    if(INT32_MAX-frame->window_update.delta_window_size < stream->window_size) {
      int r;
      r = spdylay_session_handle_invalid_stream
        (session, frame->window_update.stream_id, SPDYLAY_WINDOW_UPDATE, frame,
         SPDYLAY_FLOW_CONTROL_ERROR);
      return r;
    } else {
      stream->window_size += frame->window_update.delta_window_size;
      if(stream->deferred_data != NULL &&
         (stream->deferred_flags & SPDYLAY_DEFERRED_FLOW_CONTROL)) {
        int r;
        r = spdylay_pq_push(&session->ob_pq, stream->deferred_data);
        if(r == 0) {
          spdylay_stream_detach_deferred_data(stream);
        } else if(r < 0) {
          /* FATAL */
          assert(r < SPDYLAY_ERR_FATAL);
          return r;
        }
      }
      spdylay_session_call_on_ctrl_frame_received(session,
                                                  SPDYLAY_WINDOW_UPDATE, frame);
    }
  }
  return 0;
}

1664 1665 1666 1667 1668
int spdylay_session_on_headers_received(spdylay_session *session,
                                        spdylay_frame *frame)
{
  int r = 0;
  int valid = 0;
1669
  spdylay_stream *stream;
1670
  if(!spdylay_session_check_version(session, frame->headers.hd.version)) {
1671 1672
    return 0;
  }
1673 1674 1675 1676
  if((stream = spdylay_session_get_stream(session,
                                          frame->headers.stream_id)) &&
     (stream->shut_flags & SPDYLAY_SHUT_RD) == 0 &&
     spdylay_session_check_nv(frame->headers.nv)) {
1677
    if(spdylay_session_is_my_stream_id(session, frame->headers.stream_id)) {
1678
      if(stream->state == SPDYLAY_STREAM_OPENED) {
1679 1680 1681
        valid = 1;
        spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_HEADERS,
                                                    frame);
1682
        if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
1683 1684
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
          spdylay_session_close_stream_if_shut_rdwr(session, stream);
1685 1686 1687 1688 1689 1690 1691
        }
      } else if(stream->state == SPDYLAY_STREAM_CLOSING) {
        /* This is race condition. SPDYLAY_STREAM_CLOSING indicates
           that we queued RST_STREAM but it has not been sent. It will
           eventually sent, so we just ignore this frame. */
        valid = 1;
      }
1692 1693
    } else {
      /* If this is remote peer initiated stream, it is OK unless it
1694 1695 1696
         have sent FIN frame already. But if stream is in
         SPDYLAY_STREAM_CLOSING, we discard the frame. This is a race
         condition. */
1697
      valid = 1;
1698 1699 1700
      if(stream->state != SPDYLAY_STREAM_CLOSING) {
        spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_HEADERS,
                                                    frame);
1701
        if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
1702 1703
          spdylay_session_call_on_request_recv(session,
                                               frame->headers.stream_id);
1704 1705 1706
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
          spdylay_session_close_stream_if_shut_rdwr(session, stream);
        }
1707 1708 1709 1710
      }
    }
  }
  if(!valid) {
1711 1712 1713
    r = spdylay_session_handle_invalid_stream
      (session, frame->headers.stream_id, SPDYLAY_HEADERS, frame,
       SPDYLAY_PROTOCOL_ERROR);
1714 1715 1716 1717
  }
  return r;
}

1718 1719
/*
 * This function should be called when the session wants to drop
1720 1721
 * connection after sending GOAWAY. These cases are called as the
 * session error.  For example, when it receives bad zlib data.
1722
 */
1723 1724
static int spdylay_session_fail_session(spdylay_session *session,
                                        uint32_t status_code)
1725 1726
{
  session->goaway_flags |= SPDYLAY_GOAWAY_FAIL_ON_SEND;
1727
  return spdylay_submit_goaway(session, status_code);
1728 1729
}

1730
/* For errors, this function only returns FATAL error. */
1731
static int spdylay_session_process_ctrl_frame(spdylay_session *session)
1732 1733
{
  int r = 0;
1734
  uint16_t type;
1735
  spdylay_frame frame;
1736 1737 1738
  memcpy(&type, &session->iframe.headbuf[2], sizeof(uint16_t));
  type = ntohs(type);
  switch(type) {
1739
  case SPDYLAY_SYN_STREAM:
1740
    spdylay_buffer_reset(&session->inflatebuf);
1741
    r = spdylay_frame_unpack_syn_stream(&frame.syn_stream,
1742 1743 1744
                                        &session->inflatebuf,
                                        &session->nvbuf,
                                        &session->nvbuflen,
1745
                                        session->iframe.headbuf,
1746 1747 1748 1749 1750
                                        sizeof(session->iframe.headbuf),
                                        session->iframe.buf,
                                        session->iframe.len,
                                        &session->hd_inflater);
    if(r == 0) {
1751 1752 1753
      if(session->version == SPDYLAY_PROTO_SPDY2) {
        spdylay_frame_nv_2to3(frame.syn_stream.nv);
      }
1754
      r = spdylay_session_on_syn_stream_received(session, &frame);
1755 1756 1757 1758
      spdylay_frame_syn_stream_free(&frame.syn_stream);
      /* TODO if r indicates mulformed NV pairs (multiple nulls) or
         invalid frame, send RST_STREAM with PROTOCOL_ERROR. Same for
         other control frames. */
1759
    } else if(spdylay_is_non_fatal(r)) {
1760
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1761 1762
    }
    break;
1763
  case SPDYLAY_SYN_REPLY:
1764
    spdylay_buffer_reset(&session->inflatebuf);
1765
    r = spdylay_frame_unpack_syn_reply(&frame.syn_reply,
1766 1767 1768
                                       &session->inflatebuf,
                                       &session->nvbuf,
                                       &session->nvbuflen,
1769
                                       session->iframe.headbuf,
1770 1771 1772 1773 1774
                                       sizeof(session->iframe.headbuf),
                                       session->iframe.buf,
                                       session->iframe.len,
                                       &session->hd_inflater);
    if(r == 0) {
1775 1776 1777
      if(session->version == SPDYLAY_PROTO_SPDY2) {
        spdylay_frame_nv_2to3(frame.syn_reply.nv);
      }
1778
      r = spdylay_session_on_syn_reply_received(session, &frame);
1779
      spdylay_frame_syn_reply_free(&frame.syn_reply);
1780
    } else if(spdylay_is_non_fatal(r)) {
1781
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1782 1783
    }
    break;
1784 1785 1786 1787 1788 1789 1790 1791 1792
  case SPDYLAY_RST_STREAM:
    r = spdylay_frame_unpack_rst_stream(&frame.rst_stream,
                                        session->iframe.headbuf,
                                        sizeof(session->iframe.headbuf),
                                        session->iframe.buf,
                                        session->iframe.len);
    if(r == 0) {
      r = spdylay_session_on_rst_stream_received(session, &frame);
      spdylay_frame_rst_stream_free(&frame.rst_stream);
1793
    } else if(spdylay_is_non_fatal(r)) {
1794
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1795 1796
    }
    break;
1797 1798 1799 1800 1801 1802 1803 1804 1805
  case SPDYLAY_SETTINGS:
    r = spdylay_frame_unpack_settings(&frame.settings,
                                      session->iframe.headbuf,
                                      sizeof(session->iframe.headbuf),
                                      session->iframe.buf,
                                      session->iframe.len);
    if(r == 0) {
      r = spdylay_session_on_settings_received(session, &frame);
      spdylay_frame_settings_free(&frame.settings);
1806
    } else if(spdylay_is_non_fatal(r)) {
1807
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1808 1809
    }
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1810 1811
  case SPDYLAY_NOOP:
    break;
1812 1813 1814 1815 1816 1817 1818 1819 1820
  case SPDYLAY_PING:
    r = spdylay_frame_unpack_ping(&frame.ping,
                                  session->iframe.headbuf,
                                  sizeof(session->iframe.headbuf),
                                  session->iframe.buf,
                                  session->iframe.len);
    if(r == 0) {
      r = spdylay_session_on_ping_received(session, &frame);
      spdylay_frame_ping_free(&frame.ping);
1821
    } else if(spdylay_is_non_fatal(r)) {
1822
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1823 1824
    }
    break;
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1825 1826 1827 1828 1829 1830 1831 1832 1833
  case SPDYLAY_GOAWAY:
    r = spdylay_frame_unpack_goaway(&frame.goaway,
                                    session->iframe.headbuf,
                                    sizeof(session->iframe.headbuf),
                                    session->iframe.buf,
                                    session->iframe.len);
    if(r == 0) {
      r = spdylay_session_on_goaway_received(session, &frame);
      spdylay_frame_goaway_free(&frame.goaway);
1834
    } else if(spdylay_is_non_fatal(r)) {
1835
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1836 1837
    }
    break;
1838
  case SPDYLAY_HEADERS:
1839
    spdylay_buffer_reset(&session->inflatebuf);
1840
    r = spdylay_frame_unpack_headers(&frame.headers,
1841 1842 1843
                                     &session->inflatebuf,
                                     &session->nvbuf,
                                     &session->nvbuflen,
1844 1845 1846 1847 1848 1849
                                     session->iframe.headbuf,
                                     sizeof(session->iframe.headbuf),
                                     session->iframe.buf,
                                     session->iframe.len,
                                     &session->hd_inflater);
    if(r == 0) {
1850 1851 1852
      if(session->version == SPDYLAY_PROTO_SPDY2) {
        spdylay_frame_nv_2to3(frame.headers.nv);
      }
1853 1854
      r = spdylay_session_on_headers_received(session, &frame);
      spdylay_frame_headers_free(&frame.headers);
1855
    } else if(spdylay_is_non_fatal(r)) {
1856
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1857 1858
    }
    break;
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
  case SPDYLAY_WINDOW_UPDATE:
    r = spdylay_frame_unpack_window_update(&frame.window_update,
                                           session->iframe.headbuf,
                                           sizeof(session->iframe.headbuf),
                                           session->iframe.buf,
                                           session->iframe.len);
    if(r == 0) {
      r = spdylay_session_on_window_update_received(session, &frame);
      spdylay_frame_window_update_free(&frame.window_update);
    } else if(spdylay_is_non_fatal(r)) {
1869
      r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
1870 1871
    }
    break;
1872
  }
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1873
  if(spdylay_is_fatal(r)) {
1874
    return r;
1875 1876
  } else {
    return 0;
1877
  }
1878 1879
}

1880 1881 1882
int spdylay_session_on_data_received(spdylay_session *session,
                                     uint8_t flags, int32_t length,
                                     int32_t stream_id)
1883
{
1884
  int r = 0;
1885 1886
  spdylay_status_code status_code = 0;
  spdylay_stream *stream;
1887 1888
  stream = spdylay_session_get_stream(session, stream_id);
  if(stream) {
1889
    if((stream->shut_flags & SPDYLAY_SHUT_RD) == 0) {
1890
      int valid = 0;
1891 1892 1893
      if(spdylay_session_is_my_stream_id(session, stream_id)) {
        if(stream->state == SPDYLAY_STREAM_OPENED) {
          valid = 1;
1894 1895 1896 1897
          if(session->callbacks.on_data_recv_callback) {
            session->callbacks.on_data_recv_callback
              (session, flags, stream_id, length, session->user_data);
          }
1898 1899 1900
        } else if(stream->state != SPDYLAY_STREAM_CLOSING) {
          status_code = SPDYLAY_PROTOCOL_ERROR;
        }
1901
      } else if(stream->state != SPDYLAY_STREAM_CLOSING) {
1902
        /* It is OK if this is remote peer initiated stream and we did
1903 1904
           not receive FIN unless stream is in SPDYLAY_STREAM_CLOSING
           state. This is a race condition. */
1905
        valid = 1;
1906 1907 1908 1909
        if(session->callbacks.on_data_recv_callback) {
          session->callbacks.on_data_recv_callback
            (session, flags, stream_id, length, session->user_data);
        }
1910
        if(flags & SPDYLAY_DATA_FLAG_FIN) {
1911 1912
          spdylay_session_call_on_request_recv(session, stream_id);
        }
1913
      }
1914
      if(valid) {
1915
        if(flags & SPDYLAY_DATA_FLAG_FIN) {
1916 1917 1918 1919
          spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
          spdylay_session_close_stream_if_shut_rdwr(session, stream);
        }
      }
1920
    } else {
1921
      status_code = SPDYLAY_PROTOCOL_ERROR;
1922 1923 1924 1925
    }
  } else {
    status_code = SPDYLAY_INVALID_STREAM;
  }
1926
  if(status_code != 0) {
1927 1928
    r = spdylay_session_add_rst_stream(session, stream_id, status_code);
  }
1929 1930 1931
  return r;
}

1932
/* For errors, this function only returns FATAL error. */
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
static int spdylay_session_process_data_frame(spdylay_session *session)
{
  uint8_t flags;
  int32_t length;
  int32_t stream_id;
  int r;
  stream_id = spdylay_get_uint32(session->iframe.headbuf) &
    SPDYLAY_STREAM_ID_MASK;
  flags = session->iframe.headbuf[4];
  length = spdylay_get_uint32(&session->iframe.headbuf[4]) &
    SPDYLAY_LENGTH_MASK;
  r = spdylay_session_on_data_received(session, flags, length, stream_id);
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
1945
  if(spdylay_is_fatal(r)) {
1946 1947 1948
    return r;
  } else {
    return 0;
1949
  }
1950 1951
}

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
/*
 * Accumulates received bytes |delta_size| and decides whether to send
 * WINDOW_UPDATE.
 *
 * This function returns 0 if it succeeds, or one of the following
 * negative error codes:
 *
 * SPDYLAY_ERR_NOMEM
 *     Out of memory.
 */
static int spdylay_session_update_recv_window_size(spdylay_session *session,
                                                   int32_t stream_id,
                                                   int32_t delta_size)
{
  spdylay_stream *stream;
  stream = spdylay_session_get_stream(session, stream_id);
  if(stream) {
    stream->recv_window_size += delta_size;
    /* This is just a heuristics. */
    if(stream->recv_window_size*2 >=
1972
       session->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE]) {
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
      int r;
      r = spdylay_session_add_window_update(session, stream_id,
                                            stream->recv_window_size);
      if(r == 0) {
        stream->recv_window_size = 0;
      } else {
        return r;
      }
    }
  }
  return 0;
}

1986 1987 1988 1989 1990 1991 1992 1993
int spdylay_session_recv(spdylay_session *session)
{
  while(1) {
    ssize_t r;
    if(session->iframe.state == SPDYLAY_RECV_HEAD) {
      uint32_t payloadlen;
      if(spdylay_inbound_buffer_avail(&session->ibuf) < SPDYLAY_HEAD_LEN) {
        r = spdylay_recv(session);
1994
        /* If EOF is reached, r == SPDYLAY_ERR_EOF */
1995 1996 1997
        if(r < 0) {
          if(r == SPDYLAY_ERR_WOULDBLOCK) {
            return 0;
1998
          } else if(r == SPDYLAY_ERR_EOF) {
1999
            return r;
2000 2001
          } else {
            return SPDYLAY_ERR_CALLBACK_FAILURE;
2002 2003 2004 2005 2006 2007 2008
          }
        }
        if(spdylay_inbound_buffer_avail(&session->ibuf) < SPDYLAY_HEAD_LEN) {
          return 0;
        }
      }
      session->iframe.state = SPDYLAY_RECV_PAYLOAD;
2009 2010
      payloadlen = spdylay_get_uint32(&session->ibuf.mark[4]) &
        SPDYLAY_LENGTH_MASK;
2011 2012 2013 2014 2015
      memcpy(session->iframe.headbuf, session->ibuf.mark, SPDYLAY_HEAD_LEN);
      session->ibuf.mark += SPDYLAY_HEAD_LEN;
      if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
        /* control frame */
        session->iframe.len = payloadlen;
2016 2017 2018 2019
        r = spdylay_reserve_buffer(&session->iframe.buf,
                                   &session->iframe.bufmax,
                                   session->iframe.len);
        if(r != 0) {
2020 2021
          /* FATAL */
          assert(r < SPDYLAY_ERR_FATAL);
2022
          return r;
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
        }
        session->iframe.off = 0;
      } else {
        /* TODO validate stream id here */
        session->iframe.len = payloadlen;
        session->iframe.off = 0;
      }
    }
    if(session->iframe.state == SPDYLAY_RECV_PAYLOAD) {
      size_t rempayloadlen = session->iframe.len - session->iframe.off;
      size_t bufavail, readlen;
2034 2035
      int32_t data_stream_id = 0;
      uint8_t data_flags = SPDYLAY_DATA_FLAG_NONE;
2036 2037 2038
      if(spdylay_inbound_buffer_avail(&session->ibuf) == 0 &&
         rempayloadlen > 0) {
        r = spdylay_recv(session);
2039 2040
        if(r == 0 || r == SPDYLAY_ERR_WOULDBLOCK) {
          return 0;
2041
        } else if(r == SPDYLAY_ERR_EOF) {
2042
          return r;
2043 2044
        } else if(r < 0) {
          return SPDYLAY_ERR_CALLBACK_FAILURE;
2045 2046 2047 2048
        }
      }
      bufavail = spdylay_inbound_buffer_avail(&session->ibuf);
      readlen =  bufavail < rempayloadlen ? bufavail : rempayloadlen;
2049
      if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
2050 2051
        memcpy(session->iframe.buf+session->iframe.off,
               session->ibuf.mark, readlen);
2052
      } else {
2053 2054
        /* For data frame, We don't buffer data. Instead, just pass
           received data to callback function. */
2055
        data_stream_id = spdylay_get_uint32(session->iframe.headbuf) &
2056
          SPDYLAY_STREAM_ID_MASK;
2057 2058 2059 2060 2061 2062 2063 2064 2065
        data_flags = session->iframe.headbuf[4];
        if(session->callbacks.on_data_chunk_recv_callback) {
          session->callbacks.on_data_chunk_recv_callback(session,
                                                         data_flags,
                                                         data_stream_id,
                                                         session->ibuf.mark,
                                                         readlen,
                                                         session->user_data);
        }
2066 2067 2068
      }
      session->iframe.off += readlen;
      session->ibuf.mark += readlen;
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084

      if(session->flow_control &&
         !spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
        if(readlen > 0 &&
           (session->iframe.len != session->iframe.off ||
            (data_flags & SPDYLAY_DATA_FLAG_FIN) == 0)) {
          r = spdylay_session_update_recv_window_size(session,
                                                      data_stream_id,
                                                      readlen);
          if(r < 0) {
            /* FATAL */
            assert(r < SPDYLAY_ERR_FATAL);
            return r;
          }
        }
      }
2085 2086
      if(session->iframe.len == session->iframe.off) {
        if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
2087
          r = spdylay_session_process_ctrl_frame(session);
2088
        } else {
2089 2090 2091
          r = spdylay_session_process_data_frame(session);
        }
        if(r < 0) {
2092 2093
          /* FATAL */
          assert(r < SPDYLAY_ERR_FATAL);
2094
          return r;
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
        }
        spdylay_inbound_frame_reset(&session->iframe);
      }
    }
  }
  return 0;
}

int spdylay_session_want_read(spdylay_session *session)
{
2105 2106 2107 2108 2109 2110
  /* If these flags are set, we don't want to read. The application
     should drop the connection. */
  if((session->goaway_flags & SPDYLAY_GOAWAY_FAIL_ON_SEND) &&
     (session->goaway_flags & SPDYLAY_GOAWAY_SEND)) {
    return 0;
  }
2111
  /* Unless GOAWAY is sent or received, we always want to read
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
2112
     incoming frames. After GOAWAY is sent or received, we are only
2113 2114
     interested in active streams. */
  return !session->goaway_flags || spdylay_map_size(&session->streams) > 0;
2115 2116 2117 2118
}

int spdylay_session_want_write(spdylay_session *session)
{
2119 2120 2121 2122 2123 2124
  /* If these flags are set, we don't want to write any data. The
     application should drop the connection. */
  if((session->goaway_flags & SPDYLAY_GOAWAY_FAIL_ON_SEND) &&
     (session->goaway_flags & SPDYLAY_GOAWAY_SEND)) {
    return 0;
  }
2125 2126
  /*
   * Unless GOAWAY is sent or received, we want to write frames if
2127 2128 2129 2130
   * there is pending ones. If pending frame is SYN_STREAM and
   * concurrent stream limit is reached, we don't want to write
   * SYN_STREAM.  After GOAWAY is sent or received, we want to write
   * frames if there is pending ones AND there are active frames.
2131
   */
2132 2133
  return (session->aob.item != NULL || !spdylay_pq_empty(&session->ob_pq) ||
          (!spdylay_pq_empty(&session->ob_ss_pq) &&
2134
           !spdylay_session_get_max_concurrent_streams_reached(session))) &&
2135
    (!session->goaway_flags || spdylay_map_size(&session->streams) > 0);
2136 2137
}

2138 2139 2140 2141 2142 2143 2144 2145
int spdylay_session_add_ping(spdylay_session *session, uint32_t unique_id)
{
  int r;
  spdylay_frame *frame;
  frame = malloc(sizeof(spdylay_frame));
  if(frame == NULL) {
    return SPDYLAY_ERR_NOMEM;
  }
2146
  spdylay_frame_ping_init(&frame->ping, session->version, unique_id);
2147
  r = spdylay_session_add_frame(session, SPDYLAY_PING, frame, NULL);
2148 2149 2150 2151 2152 2153 2154
  if(r != 0) {
    spdylay_frame_ping_free(&frame->ping);
    free(frame);
  }
  return r;
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
2155
int spdylay_session_add_goaway(spdylay_session *session,
2156 2157
                               int32_t last_good_stream_id,
                               uint32_t status_code)
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
2158 2159 2160 2161 2162 2163 2164
{
  int r;
  spdylay_frame *frame;
  frame = malloc(sizeof(spdylay_frame));
  if(frame == NULL) {
    return SPDYLAY_ERR_NOMEM;
  }
2165
  spdylay_frame_goaway_init(&frame->goaway, session->version,
2166
                            last_good_stream_id, status_code);
2167
  r = spdylay_session_add_frame(session, SPDYLAY_GOAWAY, frame, NULL);
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
2168 2169 2170 2171 2172 2173 2174
  if(r != 0) {
    spdylay_frame_goaway_free(&frame->goaway);
    free(frame);
  }
  return r;
}

2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
int spdylay_session_add_window_update(spdylay_session *session,
                                      int32_t stream_id,
                                      int32_t delta_window_size)
{
  int r;
  spdylay_frame *frame;
  frame = malloc(sizeof(spdylay_frame));
  if(frame == NULL) {
    return SPDYLAY_ERR_NOMEM;
  }
  spdylay_frame_window_update_init(&frame->window_update, session->version,
                                   stream_id, delta_window_size);
  r = spdylay_session_add_frame(session, SPDYLAY_WINDOW_UPDATE, frame, NULL);
  if(r != 0) {
    spdylay_frame_window_update_free(&frame->window_update);
    free(frame);
  }
  return r;
}

2195
ssize_t spdylay_session_pack_data(spdylay_session *session,
2196
                                  uint8_t **buf_ptr, size_t *buflen_ptr,
2197
                                  size_t datamax,
2198
                                  spdylay_data *frame)
2199
{
2200 2201 2202
  ssize_t framelen = datamax+8, r;
  int eof;
  uint8_t flags;
2203 2204 2205
  r = spdylay_reserve_buffer(buf_ptr, buflen_ptr, framelen);
  if(r != 0) {
    return r;
2206
  }
2207
  eof = 0;
2208
  r = frame->data_prd.read_callback
2209 2210
    (session, frame->stream_id, (*buf_ptr)+8, datamax,
     &eof, &frame->data_prd.source, session->user_data);
2211 2212
  if(r < 0) {
    return r;
2213
  } else if(datamax < r) {
2214 2215
    return SPDYLAY_ERR_CALLBACK_FAILURE;
  }
2216 2217 2218 2219
  memset(*buf_ptr, 0, SPDYLAY_HEAD_LEN);
  spdylay_put_uint32be(&(*buf_ptr)[0], frame->stream_id);
  spdylay_put_uint32be(&(*buf_ptr)[4], r);
  flags = 0;
2220
  if(eof) {
2221
    frame->eof = 1;
2222 2223
    if(frame->flags & SPDYLAY_DATA_FLAG_FIN) {
      flags |= SPDYLAY_DATA_FLAG_FIN;
2224
    }
2225
  }
2226
  (*buf_ptr)[4] = flags;
2227
  return r+8;
2228
}
2229 2230 2231

uint32_t spdylay_session_get_next_unique_id(spdylay_session *session)
{
2232
  uint32_t ret_id;
2233 2234 2235 2236 2237 2238 2239
  if(session->next_unique_id > SPDYLAY_MAX_UNIQUE_ID) {
    if(session->server) {
      session->next_unique_id = 2;
    } else {
      session->next_unique_id = 1;
    }
  }
2240 2241 2242
  ret_id = session->next_unique_id;
  session->next_unique_id += 2;
  return ret_id;
2243
}
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255

void* spdylay_session_get_stream_user_data(spdylay_session *session,
                                           int32_t stream_id)
{
  spdylay_stream *stream;
  stream = spdylay_session_get_stream(session, stream_id);
  if(stream) {
    return stream->stream_user_data;
  } else {
    return NULL;
  }
}
2256 2257 2258 2259 2260 2261

int spdylay_session_resume_data(spdylay_session *session, int32_t stream_id)
{
  int r;
  spdylay_stream *stream;
  stream = spdylay_session_get_stream(session, stream_id);
2262 2263
  if(stream == NULL || stream->deferred_data == NULL ||
     (stream->deferred_flags & SPDYLAY_DEFERRED_FLOW_CONTROL)) {
2264 2265 2266 2267 2268 2269 2270 2271
    return SPDYLAY_ERR_INVALID_ARGUMENT;
  }
  r = spdylay_pq_push(&session->ob_pq, stream->deferred_data);
  if(r == 0) {
    spdylay_stream_detach_deferred_data(stream);
  }
  return r;
}
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282

uint8_t spdylay_session_get_pri_lowest(spdylay_session *session)
{
  if(session->version == SPDYLAY_PROTO_SPDY2) {
    return SPDYLAY_SPDY2_PRI_LOWEST;
  } else if(session->version == SPDYLAY_PROTO_SPDY3) {
    return SPDYLAY_SPDY3_PRI_LOWEST;
  } else {
    return 0;
  }
}