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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
|
/*
* stream.c (based in webcam.c)
* Streaming using jpeg images over a multipart/x-mixed-replace stream
* Copyright (C) 2002 Jeroen Vreeken (pe1rxq@amsat.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "md5.h"
#include "picture.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <sys/fcntl.h>
#define STREAM_REALM "Motion Stream Security Access"
#define KEEP_ALIVE_TIMEOUT 100
typedef void* (*auth_handler)(void*);
struct auth_param {
struct context *cnt;
int sock;
int sock_flags;
int* thread_count;
struct config *conf;
};
pthread_mutex_t stream_auth_mutex;
/**
* set_sock_timeout
*
* Returns : 0 or 1 on timeout
*/
static int set_sock_timeout(int sock, int sec)
{
struct timeval tv;
tv.tv_sec = sec;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*) &tv, sizeof(tv))) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: set socket timeout failed");
return 1;
}
return 0;
}
/**
* read_http_request
*
*
* Returns : 1 on success or 0 if any error happens
*/
static int read_http_request(int sock, char* buffer, int buflen, char* uri, int uri_len)
{
int nread = 0;
int ret,readb = 1;
char method[10] = {'\0'};
char url[512] = {'\0'};
char protocol[10] = {'\0'};
static const char *bad_request_response_raw =
"HTTP/1.0 400 Bad Request\r\n"
"Content-type: text/plain\r\n\r\n"
"Bad Request\n";
static const char *bad_method_response_template_raw =
"HTTP/1.0 501 Method Not Implemented\r\n"
"Content-type: text/plain\r\n\r\n"
"Method Not Implemented\n";
static const char *timeout_response_template_raw =
"HTTP/1.0 408 Request Timeout\r\n"
"Content-type: text/plain\r\n\r\n"
"Request Timeout\n";
buffer[0] = '\0';
while ((strstr(buffer, "\r\n\r\n") == NULL) && (readb != 0) && (nread < buflen)) {
readb = read(sock, buffer+nread, buflen - nread);
if (readb == -1) {
nread = -1;
break;
}
nread += readb;
if (nread > buflen) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream End buffer reached"
" waiting for buffer ending");
break;
}
buffer[nread] = '\0';
}
/*
* Make sure the last read didn't fail. If it did, there's a
* problem with the connection, so give up.
*/
if (nread == -1) {
if(errno == EAGAIN) { // Timeout
ret = write(sock, timeout_response_template_raw, strlen(timeout_response_template_raw));
return 0;
}
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream READ give up!");
return 0;
}
ret = sscanf(buffer, "%9s %511s %9s", method, url, protocol);
if (ret != 3) {
ret = write(sock, bad_request_response_raw, sizeof(bad_request_response_raw));
return 0;
}
/* Check Protocol */
if (strcmp(protocol, "HTTP/1.0") && strcmp (protocol, "HTTP/1.1")) {
/* We don't understand this protocol. Report a bad response. */
ret = write(sock, bad_request_response_raw, sizeof(bad_request_response_raw));
return 0;
}
if (strcmp(method, "GET")) {
/*
* This server only implements the GET method. If client
* uses other method, report the failure.
*/
char response[1024];
snprintf(response, sizeof(response), bad_method_response_template_raw, method);
ret = write(sock, response, strlen (response));
return 0;
}
if(uri)
strncpy(uri, url, uri_len);
return 1;
}
static void stream_add_client(struct stream *list, int sc);
/**
* handle_basic_auth
*
*
*/
static void* handle_basic_auth(void* param)
{
struct auth_param *p = (struct auth_param*)param;
char buffer[1024] = {'\0'};
ssize_t length = 1023;
char *auth, *h, *authentication;
static const char *request_auth_response_template=
"HTTP/1.0 401 Authorization Required\r\n"
"Server: Motion/"VERSION"\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache, private\r\n"
"Pragma: no-cache\r\n"
"WWW-Authenticate: Basic realm=\""STREAM_REALM"\"\r\n\r\n";
pthread_mutex_lock(&stream_auth_mutex);
p->thread_count++;
pthread_mutex_unlock(&stream_auth_mutex);
if (!read_http_request(p->sock,buffer, length, NULL, 0))
goto Invalid_Request;
auth = strstr(buffer, "Authorization: Basic");
if (!auth)
goto Error;
auth += sizeof("Authorization: Basic");
h = strstr(auth, "\r\n");
if(!h)
goto Error;
*h='\0';
if (p->conf->stream_authentication != NULL) {
char *userpass = NULL;
size_t auth_size = strlen(p->conf->stream_authentication);
authentication = (char *) mymalloc(BASE64_LENGTH(auth_size) + 1);
userpass = mymalloc(auth_size + 4);
/* base64_encode can read 3 bytes after the end of the string, initialize it. */
memset(userpass, 0, auth_size + 4);
strcpy(userpass, p->conf->stream_authentication);
base64_encode(userpass, authentication, auth_size);
free(userpass);
if (strcmp(auth, authentication)) {
free(authentication);
goto Error;
}
free(authentication);
}
// OK - Access
/* Set socket to non blocking */
if (fcntl(p->sock, F_SETFL, p->sock_flags) < 0) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: fcntl");
goto Error;
}
/* Lock the mutex */
pthread_mutex_lock(&stream_auth_mutex);
stream_add_client(&p->cnt->stream, p->sock);
p->cnt->stream_count++;
p->thread_count--;
/* Unlock the mutex */
pthread_mutex_unlock(&stream_auth_mutex);
free(p);
pthread_exit(NULL);
Error:
write(p->sock, request_auth_response_template, strlen (request_auth_response_template));
Invalid_Request:
close(p->sock);
pthread_mutex_lock(&stream_auth_mutex);
p->thread_count--;
pthread_mutex_unlock(&stream_auth_mutex);
free(p);
pthread_exit(NULL);
}
#define HASHLEN 16
typedef char HASH[HASHLEN];
#define HASHHEXLEN 32
typedef char HASHHEX[HASHHEXLEN+1];
#define IN
#define OUT
/**
* CvtHex
* Calculates H(A1) as per HTTP Digest spec -- taken from RFC 2617.
*/
static void CvtHex(IN HASH Bin, OUT HASHHEX Hex)
{
unsigned short i;
unsigned char j;
for (i = 0; i < HASHLEN; i++) {
j = (Bin[i] >> 4) & 0xf;
if (j <= 9)
Hex[i*2] = (j + '0');
else
Hex[i*2] = (j + 'a' - 10);
j = Bin[i] & 0xf;
if (j <= 9)
Hex[i*2+1] = (j + '0');
else
Hex[i*2+1] = (j + 'a' - 10);
};
Hex[HASHHEXLEN] = '\0';
};
/**
* DigestCalcHA1
* Calculates H(A1) as per spec.
*/
static void DigestCalcHA1(
IN char * pszAlg,
IN char * pszUserName,
IN char * pszRealm,
IN char * pszPassword,
IN char * pszNonce,
IN char * pszCNonce,
OUT HASHHEX SessionKey
)
{
MD5_CTX Md5Ctx;
HASH HA1;
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (unsigned char *)pszUserName, strlen(pszUserName));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszRealm, strlen(pszRealm));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszPassword, strlen(pszPassword));
MD5Final((unsigned char *)HA1, &Md5Ctx);
if (strcmp(pszAlg, "md5-sess") == 0) {
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (unsigned char *)HA1, HASHLEN);
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszNonce, strlen(pszNonce));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszCNonce, strlen(pszCNonce));
MD5Final((unsigned char *)HA1, &Md5Ctx);
};
CvtHex(HA1, SessionKey);
};
/**
* DigestCalcResponse
* Calculates request-digest/response-digest as per HTTP Digest spec.
*/
static void DigestCalcResponse(
IN HASHHEX HA1, /* H(A1) */
IN char * pszNonce, /* nonce from server */
IN char * pszNonceCount, /* 8 hex digits */
IN char * pszCNonce, /* client nonce */
IN char * pszQop, /* qop-value: "", "auth", "auth-int" */
IN char * pszMethod, /* method from the request */
IN char * pszDigestUri, /* requested URL */
IN HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
OUT HASHHEX Response /* request-digest or response-digest */
)
{
MD5_CTX Md5Ctx;
HASH HA2;
HASH RespHash;
HASHHEX HA2Hex;
// Calculate H(A2)
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (unsigned char *)pszMethod, strlen(pszMethod));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszDigestUri, strlen(pszDigestUri));
if (strcmp(pszQop, "auth-int") == 0) {
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)HEntity, HASHHEXLEN);
}
MD5Final((unsigned char *)HA2, &Md5Ctx);
CvtHex(HA2, HA2Hex);
// Calculate response
MD5Init(&Md5Ctx);
MD5Update(&Md5Ctx, (unsigned char *)HA1, HASHHEXLEN);
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszNonce, strlen(pszNonce));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
if (*pszQop) {
MD5Update(&Md5Ctx, (unsigned char *)pszNonceCount, strlen(pszNonceCount));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszCNonce, strlen(pszCNonce));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
MD5Update(&Md5Ctx, (unsigned char *)pszQop, strlen(pszQop));
MD5Update(&Md5Ctx, (unsigned char *)":", 1);
}
MD5Update(&Md5Ctx, (unsigned char *)HA2Hex, HASHHEXLEN);
MD5Final((unsigned char *)RespHash, &Md5Ctx);
CvtHex(RespHash, Response);
};
/**
* handle_md5_digest
*
*
*/
static void* handle_md5_digest(void* param)
{
struct auth_param *p = (struct auth_param*)param;
char buffer[1024] = {'\0'};
ssize_t length = 1023;
char *auth, *h, *username, *realm, *uri, *nonce, *response;
int username_len, realm_len, uri_len, nonce_len, response_len;
#define SERVER_NONCE_LEN 17
char server_nonce[SERVER_NONCE_LEN];
#define SERVER_URI_LEN 512
char server_uri[SERVER_URI_LEN];
char* server_user = NULL, *server_pass = NULL;
unsigned int rand1,rand2;
HASHHEX HA1;
HASHHEX HA2 = "";
HASHHEX server_response;
static const char *request_auth_response_template=
"HTTP/1.0 401 Authorization Required\r\n"
"Server: Motion/"VERSION"\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache, private\r\n"
"Pragma: no-cache\r\n"
"WWW-Authenticate: Digest";
static const char *auth_failed_html_template=
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<HTML><HEAD>\r\n"
"<TITLE>401 Authorization Required</TITLE>\r\n"
"</HEAD><BODY>\r\n"
"<H1>Authorization Required</H1>\r\n"
"This server could not verify that you are authorized to access the document "
"requested. Either you supplied the wrong credentials (e.g., bad password), "
"or your browser doesn't understand how to supply the credentials required.\r\n"
"</BODY></HTML>\r\n";
static const char *internal_error_template=
"HTTP/1.0 500 Internal Server Error\r\n"
"Server: Motion/"VERSION"\r\n"
"Content-Type: text/html\r\n"
"Connection: Close\r\n\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<HTML><HEAD>\r\n"
"<TITLE>500 Internal Server Error</TITLE>\r\n"
"</HEAD><BODY>\r\n"
"<H1>500 Internal Server Error</H1>\r\n"
"</BODY></HTML>\r\n";
pthread_mutex_lock(&stream_auth_mutex);
p->thread_count++;
pthread_mutex_unlock(&stream_auth_mutex);
set_sock_timeout(p->sock, KEEP_ALIVE_TIMEOUT);
srand(time(NULL));
rand1 = (unsigned int)(42000000.0 * rand() / (RAND_MAX + 1.0));
rand2 = (unsigned int)(42000000.0 * rand() / (RAND_MAX + 1.0));
snprintf(server_nonce, SERVER_NONCE_LEN, "%08x%08x", rand1, rand2);
if (!p->conf->stream_authentication) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error no authentication data");
goto InternalError;
}
h = strstr(p->conf->stream_authentication, ":");
if (!h) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error no authentication data (no ':' found)");
goto InternalError;
}
server_user = (char*)malloc((h - p->conf->stream_authentication) + 1);
server_pass = (char*)malloc(strlen(h) + 1);
if (!server_user || !server_pass) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error malloc failed");
goto InternalError;
}
strncpy(server_user, p->conf->stream_authentication, h-p->conf->stream_authentication);
server_user[h - p->conf->stream_authentication] = '\0';
strncpy(server_pass, h + 1, strlen(h + 1));
server_pass[strlen(h + 1)] = '\0';
while(1) {
if(!read_http_request(p->sock, buffer, length, server_uri, SERVER_URI_LEN - 1))
goto Invalid_Request;
auth = strstr(buffer, "Authorization: Digest");
if(!auth)
goto Error;
auth += sizeof("Authorization: Digest");
h = strstr(auth, "\r\n");
if (!h)
goto Error;
*h = '\0';
// Username
h=strstr(auth, "username=\"");
if (!h)
goto Error;
username = h + 10;
h = strstr(username + 1, "\"");
if (!h)
goto Error;
username_len = h - username;
// Realm
h = strstr(auth, "realm=\"");
if (!h)
goto Error;
realm = h + 7;
h = strstr(realm + 1, "\"");
if (!h)
goto Error;
realm_len = h - realm;
// URI
h = strstr(auth, "uri=\"");
if (!h)
goto Error;
uri = h + 5;
h = strstr(uri + 1, "\"");
if (!h)
goto Error;
uri_len = h - uri;
// Nonce
h = strstr(auth, "nonce=\"");
if (!h)
goto Error;
nonce = h + 7;
h = strstr(nonce + 1, "\"");
if (!h)
goto Error;
nonce_len = h - nonce;
// Response
h = strstr(auth, "response=\"");
if (!h)
goto Error;
response = h + 10;
h = strstr(response + 1, "\"");
if (!h)
goto Error;
response_len = h - response;
username[username_len] = '\0';
realm[realm_len] = '\0';
uri[uri_len] = '\0';
nonce[nonce_len] = '\0';
response[response_len] = '\0';
DigestCalcHA1((char*)"md5", server_user, (char*)STREAM_REALM, server_pass, (char*)server_nonce, (char*)NULL, HA1);
DigestCalcResponse(HA1, server_nonce, NULL, NULL, (char*)"", (char*)"GET", server_uri, HA2, server_response);
if (strcmp(server_response, response) == 0)
break;
Error:
rand1 = (unsigned int)(42000000.0 * rand() / (RAND_MAX + 1.0));
rand2 = (unsigned int)(42000000.0 * rand() / (RAND_MAX + 1.0));
snprintf(server_nonce, SERVER_NONCE_LEN, "%08x%08x", rand1, rand2);
snprintf(buffer, length, "%s realm=\""STREAM_REALM"\", nonce=\"%s\"\r\n"
"Content-Type: text/html\r\n"
"Keep-Alive: timeout=%i\r\n"
"Connection: keep-alive\r\n"
"Content-Length: %Zu\r\n\r\n",
request_auth_response_template, server_nonce,
KEEP_ALIVE_TIMEOUT, strlen(auth_failed_html_template));
write(p->sock, buffer, strlen(buffer));
write(p->sock, auth_failed_html_template, strlen(auth_failed_html_template));
}
// OK - Access
/* Set socket to non blocking */
if (fcntl(p->sock, F_SETFL, p->sock_flags) < 0) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: fcntl");
goto Error;
}
if(server_user)
free(server_user);
if(server_pass)
free(server_pass);
/* Lock the mutex */
pthread_mutex_lock(&stream_auth_mutex);
stream_add_client(&p->cnt->stream, p->sock);
p->cnt->stream_count++;
p->thread_count--;
/* Unlock the mutex */
pthread_mutex_unlock(&stream_auth_mutex);
free(p);
pthread_exit(NULL);
InternalError:
if(server_user)
free(server_user);
if(server_pass)
free(server_pass);
write(p->sock, internal_error_template, strlen(internal_error_template));
Invalid_Request:
close(p->sock);
pthread_mutex_lock(&stream_auth_mutex);
p->thread_count--;
pthread_mutex_unlock(&stream_auth_mutex);
free(p);
pthread_exit(NULL);
}
/**
* do_client_auth
*
*
*/
static void do_client_auth(struct context *cnt, int sc)
{
pthread_t thread_id;
pthread_attr_t attr;
auth_handler handle_func;
struct auth_param* handle_param = NULL;
int flags;
static int first_call = 0;
static int thread_count = 0;
if(first_call == 0) {
first_call = 1;
/* Initialize the mutex */
pthread_mutex_init(&stream_auth_mutex, NULL);
}
switch(cnt->conf.stream_auth_method)
{
case 1: // Basic
handle_func = handle_basic_auth;
break;
case 2: // MD5 Digest
handle_func = handle_md5_digest;
break;
default:
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error unknown stream authentication method");
goto Error;
break;
}
handle_param = mymalloc(sizeof(struct auth_param));
handle_param->cnt = cnt;
handle_param->sock = sc;
handle_param->conf = &cnt->conf;
handle_param->thread_count = &thread_count;
/* Set socket to blocking */
if ((flags = fcntl(sc, F_GETFL, 0)) < 0) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: fcntl");
goto Error;
}
handle_param->sock_flags = flags;
if (fcntl(sc, F_SETFL, flags & (~O_NONBLOCK)) < 0) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: fcntl");
goto Error;
}
if (thread_count >= DEF_MAXSTREAMS)
goto Error;
if (pthread_attr_init(&attr)) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error pthread_attr_init");
goto Error;
}
if (pthread_create(&thread_id, &attr, handle_func, handle_param)) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error pthread_create");
goto Error;
}
pthread_detach(thread_id);
if (pthread_attr_destroy(&attr))
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error pthread_attr_destroy");
return;
Error:
close(sc);
if(handle_param)
free(handle_param);
}
/**
* http_bindsock
* Sets up a TCP/IP socket for incoming requests. It is called only during
* initialisation of Motion from the function stream_init
* The function sets up a a socket on the port number given by _port_.
* If the parameter _local_ is not zero the socket is setup to only accept connects from localhost.
* Otherwise any client IP address is accepted. The function returns an integer representing the socket.
*
* Returns: socket descriptor or -1 if any error happens
*/
int http_bindsock(int port, int local, int ipv6_enabled)
{
int sl = -1, optval;
struct addrinfo hints, *res = NULL, *ressave = NULL;
char portnumber[10], hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
snprintf(portnumber, sizeof(portnumber), "%u", port);
memset(&hints, 0, sizeof(struct addrinfo));
/* Use the AI_PASSIVE flag, which indicates we are using this address for a listen() */
hints.ai_flags = AI_PASSIVE;
#if defined(BSD)
hints.ai_family = AF_INET;
#else
if (!ipv6_enabled)
hints.ai_family = AF_INET;
else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_STREAM;
optval = getaddrinfo(local ? "localhost" : NULL, portnumber, &hints, &res);
if (optval != 0) {
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: getaddrinfo() for motion-stream socket failed: %s",
gai_strerror(optval));
if (res != NULL)
freeaddrinfo(res);
return -1;
}
ressave = res;
while (res) {
/* Create socket */
sl = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (sl >= 0) {
optval = 1;
/* Reuse Address */
setsockopt(sl, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: motion-stream testing : %s addr: %s port: %s",
res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf);
if (bind(sl, res->ai_addr, res->ai_addrlen) == 0) {
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: motion-stream Bound : %s addr: %s port: %s",
res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf);
break;
}
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream bind() failed, retrying");
close(sl);
sl = -1;
}
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream socket failed, retrying");
res = res->ai_next;
}
freeaddrinfo(ressave);
if (sl < 0) {
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream creating socket/bind ERROR");
return -1;
}
if (listen(sl, DEF_MAXWEBQUEUE) == -1) {
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream listen() ERROR");
close(sl);
sl = -1;
}
return sl;
}
/**
* http_acceptsock
*
*
* Returns: socket descriptor or -1 if any error happens.
*/
static int http_acceptsock(int sl)
{
int sc;
unsigned long i;
struct sockaddr_storage sin;
socklen_t addrlen = sizeof(sin);
if ((sc = accept(sl, (struct sockaddr *)&sin, &addrlen)) >= 0) {
i = 1;
ioctl(sc, FIONBIO, &i);
return sc;
}
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream accept()");
return -1;
}
/**
* stream_flush
* Sends any outstanding data to all connected clients.
* It continuously goes through the client list until no data is able
* to be sent (either because there isn't any, or because the clients
* are not able to accept it).
*/
static void stream_flush(struct stream *list, int *stream_count, int lim)
{
int written; /* The number of bytes actually written. */
struct stream *client; /* Pointer to the client being served. */
int workdone = 0; /* Flag set any time data is successfully
written. */
client = list->next;
while (client) {
/* If data waiting for client, try to send it. */
if (client->tmpbuffer) {
/*
* We expect that list->filepos < list->tmpbuffer->size
* should always be true. The check is more for safety,
* in case of trouble is some other part of the code.
* Note that if it is false, the following section will
* clean up.
*/
if (client->filepos < client->tmpbuffer->size) {
/*
* Here we are finally ready to write out the
* data. Remember that (because the socket
* has been set non-blocking) we may only
* write out part of the buffer. The var
* 'filepos' contains how much of the buffer
* has already been written.
*/
written = write(client->socket,
client->tmpbuffer->ptr + client->filepos,
client->tmpbuffer->size - client->filepos);
/*
* If any data has been written, update the
* data pointer and set the workdone flag.
*/
if (written > 0) {
client->filepos += written;
workdone = 1;
}
} else
written = 0;
/*
* If we have written the entire buffer to the socket,
* or if there was some error (other than EAGAIN, which
* means the system couldn't take it), this request is
* finished.
*/
if ((client->filepos >= client->tmpbuffer->size) ||
(written < 0 && errno != EAGAIN)) {
/* If no other clients need this buffer, free it. */
if (--client->tmpbuffer->ref <= 0) {
free(client->tmpbuffer->ptr);
free(client->tmpbuffer);
}
/* Mark this client's buffer as empty. */
client->tmpbuffer = NULL;
client->nr++;
}
/*
* If the client is no longer connected, or the total
* number of frames already sent to this client is
* greater than our configuration limit, disconnect
* the client and free the stream struct.
*/
if ((written < 0 && errno != EAGAIN) ||
(lim && !client->tmpbuffer && client->nr > lim)) {
void *tmp;
close(client->socket);
if (client->next)
client->next->prev = client->prev;
client->prev->next = client->next;
tmp = client;
client = client->prev;
free(tmp);
(*stream_count)--;
}
} /* End if (client->tmpbuffer) */
/*
* Step the the next client in the list. If we get to the
* end of the list, check if anything was written during
* that loop; (if so) reset the 'workdone' flag and go back
* to the beginning.
*/
client = client->next;
if (!client && workdone) {
client = list->next;
workdone = 0;
}
} /* End while (client) */
}
/**
* stream_tmpbuffer
* Routine to create a new "tmpbuffer", which is a common
* object used by all clients connected to a single camera.
*
* Returns: new allocated stream_buffer.
*/
static struct stream_buffer *stream_tmpbuffer(int size)
{
struct stream_buffer *tmpbuffer = mymalloc(sizeof(struct stream_buffer));
tmpbuffer->ref = 0;
tmpbuffer->ptr = mymalloc(size);
return tmpbuffer;
}
/**
* stream_add_client
*
*
*/
static void stream_add_client(struct stream *list, int sc)
{
struct stream *new = mymalloc(sizeof(struct stream));
static const char header[] = "HTTP/1.0 200 OK\r\n"
"Server: Motion/"VERSION"\r\n"
"Connection: close\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache, private\r\n"
"Pragma: no-cache\r\n"
"Content-Type: multipart/x-mixed-replace; "
"boundary=--BoundaryString\r\n\r\n";
memset(new, 0, sizeof(struct stream));
new->socket = sc;
if ((new->tmpbuffer = stream_tmpbuffer(sizeof(header))) == NULL) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error creating tmpbuffer in stream_add_client");
} else {
memcpy(new->tmpbuffer->ptr, header, sizeof(header)-1);
new->tmpbuffer->size = sizeof(header)-1;
}
new->prev = list;
new->next = list->next;
if (new->next)
new->next->prev = new;
list->next = new;
}
/**
* stream_add_write
*
*
*/
static void stream_add_write(struct stream *list, struct stream_buffer *tmpbuffer, unsigned int fps)
{
struct timeval curtimeval;
unsigned long int curtime;
gettimeofday(&curtimeval, NULL);
curtime = curtimeval.tv_usec + 1000000L * curtimeval.tv_sec;
while (list->next) {
list = list->next;
if (list->tmpbuffer == NULL && ((curtime - list->last) >= 1000000L / fps)) {
list->last = curtime;
list->tmpbuffer = tmpbuffer;
tmpbuffer->ref++;
list->filepos = 0;
}
}
if (tmpbuffer->ref <= 0) {
free(tmpbuffer->ptr);
free(tmpbuffer);
}
}
/**
* stream_check_write
* We walk through the chain of stream structs until we reach the end.
* Here we check if the tmpbuffer points to NULL.
* We return 1 if it finds a list->tmpbuffer which is a NULL pointer which would
* be the next client ready to be sent a new image. If not a 0 is returned.
*
* Returns:
*/
static int stream_check_write(struct stream *list)
{
while (list->next) {
list = list->next;
if (list->tmpbuffer == NULL)
return 1;
}
return 0;
}
/**
* stream_init
* This function is called from motion.c for each motion thread starting up.
* The function setup the incoming tcp socket that the clients connect to.
* The function returns an integer representing the socket.
*
* Returns: stream socket descriptor.
*/
int stream_init(struct context *cnt)
{
cnt->stream.socket = http_bindsock(cnt->conf.stream_port, cnt->conf.stream_localhost,
cnt->conf.ipv6_enabled);
cnt->stream.next = NULL;
cnt->stream.prev = NULL;
return cnt->stream.socket;
}
/**
* stream_stop
* This function is called from the motion_loop when it ends
* and motion is terminated or restarted.
*/
void stream_stop(struct context *cnt)
{
struct stream *list;
struct stream *next = cnt->stream.next;
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: Closing motion-stream listen socket"
" & active motion-stream sockets");
close(cnt->stream.socket);
cnt->stream.socket = -1;
while (next) {
list = next;
next = list->next;
if (list->tmpbuffer) {
free(list->tmpbuffer->ptr);
free(list->tmpbuffer);
}
close(list->socket);
free(list);
}
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: Closed motion-stream listen socket"
" & active motion-stream sockets");
}
/*
* stream_put
* Is the starting point of the stream loop. It is called from
* the motion_loop with the argument 'image' pointing to the latest frame.
* If config option 'stream_motion' is 'on' this function is called once
* per second (frame 0) and when Motion is detected excl pre_capture.
* If config option 'stream_motion' is 'off' this function is called once
* per captured picture frame.
* It is always run in setup mode for each picture frame captured and with
* the special setup image.
* The function does two things:
* It looks for possible waiting new clients and adds them.
* It sends latest picture frame to all connected clients.
* Note: Clients that have disconnected are handled in the stream_flush()
* function.
*/
void stream_put(struct context *cnt, unsigned char *image)
{
struct timeval timeout;
struct stream_buffer *tmpbuffer;
fd_set fdread;
int sl = cnt->stream.socket;
int sc;
/* Tthe following string has an extra 16 chars at end for length. */
const char jpeghead[] = "--BoundaryString\r\n"
"Content-type: image/jpeg\r\n"
"Content-Length: ";
int headlength = sizeof(jpeghead) - 1; /* Don't include terminator. */
char len[20]; /* Will be used for sprintf, must be >= 16 */
/*
* Timeout struct used to timeout the time we wait for a client
* and we do not wait at all.
*/
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&fdread);
FD_SET(cnt->stream.socket, &fdread);
/*
* If we have not reached the max number of allowed clients per
* thread we will check to see if new clients are waiting to connect.
* If this is the case we add the client as a new stream struct and
* add this to the end of the chain of stream structs that are linked
* to each other.
*/
if ((cnt->stream_count < DEF_MAXSTREAMS) &&
(select(sl + 1, &fdread, NULL, NULL, &timeout) > 0)) {
sc = http_acceptsock(sl);
if (cnt->conf.stream_auth_method == 0) {
stream_add_client(&cnt->stream, sc);
cnt->stream_count++;
} else {
do_client_auth(cnt, sc);
}
}
/* Lock the mutex */
if (cnt->conf.stream_auth_method != 0)
pthread_mutex_lock(&stream_auth_mutex);
/* Call flush to send any previous partial-sends which are waiting. */
stream_flush(&cnt->stream, &cnt->stream_count, cnt->conf.stream_limit);
/* Check if any clients have available buffers. */
if (stream_check_write(&cnt->stream)) {
/*
* Yes - create a new tmpbuffer for current image.
* Note that this should create a buffer which is *much* larger
* than necessary, but it is difficult to estimate the
* minimum size actually required.
*/
tmpbuffer = stream_tmpbuffer(cnt->imgs.size);
/* Check if allocation was ok. */
if (tmpbuffer) {
int imgsize;
/*
* We need a pointer that points to the picture buffer
* just after the mjpeg header. We create a working pointer wptr
* to be used in the call to put_picture_memory which we can change
* and leave tmpbuffer->ptr intact.
*/
unsigned char *wptr = tmpbuffer->ptr;
/*
* For web protocol, our image needs to be preceded
* with a little HTTP, so we put that into the buffer
* first.
*/
memcpy(wptr, jpeghead, headlength);
/* Update our working pointer to point past header. */
wptr += headlength;
/* Create a jpeg image and place into tmpbuffer. */
tmpbuffer->size = put_picture_memory(cnt, wptr, cnt->imgs.size, image,
cnt->conf.stream_quality);
/* Fill in the image length into the header. */
imgsize = sprintf(len, "%9ld\r\n\r\n", tmpbuffer->size);
memcpy(wptr - imgsize, len, imgsize);
/* Append a CRLF for good measure. */
memcpy(wptr + tmpbuffer->size, "\r\n", 2);
/*
* Now adjust tmpbuffer->size to reflect the
* header at the beginning and the extra CRLF
* at the end.
*/
tmpbuffer->size += headlength + 2;
/*
* And finally put this buffer to all clients with
* no outstanding data from previous frames.
*/
stream_add_write(&cnt->stream, tmpbuffer, cnt->conf.stream_maxrate);
} else {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: Error creating tmpbuffer");
}
}
/*
* Now we call flush again. This time (assuming some clients were
* ready for the new frame) the new data will be written out.
*/
stream_flush(&cnt->stream, &cnt->stream_count, cnt->conf.stream_limit);
/* Unlock the mutex */
if (cnt->conf.stream_auth_method != 0)
pthread_mutex_unlock(&stream_auth_mutex);
return;
}
|