OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. */ | 3 * found in the LICENSE file. */ |
4 | 4 |
5 #include "handlers.h" | 5 #include "handlers.h" |
6 | 6 |
7 #include <arpa/inet.h> | 7 #include <arpa/inet.h> |
8 #include <assert.h> | 8 #include <assert.h> |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <limits.h> | 10 #include <limits.h> |
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 char* result = getcwd(cwd, PATH_MAX); | 777 char* result = getcwd(cwd, PATH_MAX); |
778 if (result == NULL) { | 778 if (result == NULL) { |
779 *output = PrintfToNewString("getcwd returned error: %d", errno); | 779 *output = PrintfToNewString("getcwd returned error: %d", errno); |
780 return 1; | 780 return 1; |
781 } | 781 } |
782 | 782 |
783 *output = PrintfToNewString("getcwd\1%s", cwd); | 783 *output = PrintfToNewString("getcwd\1%s", cwd); |
784 return 0; | 784 return 0; |
785 } | 785 } |
786 | 786 |
| 787 int HandleGetaddrinfo(int num_params, char** params, char** output) { |
| 788 int output_len; |
| 789 int current_pos; |
| 790 |
| 791 if (num_params != 2) { |
| 792 *output = PrintfToNewString("getaddrinfo takes 2 parameters."); |
| 793 return 1; |
| 794 } |
| 795 |
| 796 const char* name = params[0]; |
| 797 const char* family = params[1]; |
| 798 |
| 799 struct addrinfo *ai; |
| 800 struct addrinfo hints; |
| 801 memset(&hints, 0, sizeof(hints)); |
| 802 hints.ai_flags = AI_CANONNAME; |
| 803 if (!strcmp(family, "AF_INET")) |
| 804 hints.ai_family = AF_INET; |
| 805 else if (!strcmp(family, "AF_INET6")) |
| 806 hints.ai_family = AF_INET6; |
| 807 else if (!strcmp(family, "AF_UNSPEC")) |
| 808 hints.ai_family = AF_UNSPEC; |
| 809 else { |
| 810 *output = PrintfToNewString("getaddrinfo uknown family: %s", family); |
| 811 return 1; |
| 812 } |
| 813 |
| 814 int rtn = getaddrinfo(name, NULL, &hints, &ai); |
| 815 if (rtn != 0) { |
| 816 *output = PrintfToNewString("getaddrinfo failed, error is \"%s\"", |
| 817 gai_strerror(rtn)); |
| 818 return 2; |
| 819 } |
| 820 |
| 821 |
| 822 output_len = strlen("getaddrinfo") + strlen(ai->ai_canonname) + 3; |
| 823 |
| 824 struct addrinfo *current = ai; |
| 825 while (current) { |
| 826 output_len += 2 + INET6_ADDRSTRLEN + strlen("AF_INET6"); |
| 827 current = current->ai_next; |
| 828 } |
| 829 |
| 830 char* out = (char*)calloc(output_len, 1); |
| 831 if (!out) { |
| 832 *output = PrintfToNewString("out of memory."); |
| 833 return 3; |
| 834 } |
| 835 |
| 836 snprintf(out, output_len, "getaddrinfo\1%s", ai->ai_canonname); |
| 837 |
| 838 current_pos = strlen(out); |
| 839 current = ai; |
| 840 while (current) { |
| 841 out[current_pos] = '\1'; |
| 842 current_pos++; |
| 843 const char* tmp = NULL; |
| 844 if (ai->ai_family == AF_INET6) { |
| 845 struct sockaddr_in6* in6 = (struct sockaddr_in6*)current->ai_addr; |
| 846 tmp = inet_ntop(ai->ai_family, &in6->sin6_addr.s6_addr, |
| 847 out+current_pos, output_len-current_pos); |
| 848 } else if (ai->ai_family == AF_INET) { |
| 849 struct sockaddr_in* in = (struct sockaddr_in*)current->ai_addr; |
| 850 tmp = inet_ntop(ai->ai_family, &in->sin_addr, |
| 851 out+current_pos, output_len-current_pos); |
| 852 } |
| 853 current_pos += strlen(tmp); |
| 854 |
| 855 const char* addr_type = ai->ai_family == AF_INET ? "AF_INET" : "AF_INET6"; |
| 856 current_pos += sprintf(out + current_pos, "\1%s", addr_type); |
| 857 current = current->ai_next; |
| 858 } |
| 859 |
| 860 *output = out; |
| 861 freeaddrinfo(ai); |
| 862 return 0; |
| 863 } |
| 864 |
787 /** | 865 /** |
788 * Handle a call to gethostbyname() made by JavaScript. | 866 * Handle a call to gethostbyname() made by JavaScript. |
789 * | 867 * |
790 * gethostbyname expects 1 parameter: | 868 * gethostbyname expects 1 parameter: |
791 * 0: The name of the host to look up. | 869 * 0: The name of the host to look up. |
792 * on success, gethostbyname returns a result in |output| separated by \1: | 870 * on success, gethostbyname returns a result in |output| separated by \1: |
793 * 0: "gethostbyname" | 871 * 0: "gethostbyname" |
794 * 1: Host name | 872 * 1: Host name |
795 * 2: Address type (either "AF_INET" or "AF_INET6") | 873 * 2: Address type (either "AF_INET" or "AF_INET6") |
796 * 3. The first address. | 874 * 3. The first address. |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 int sock = strtol(params[0], NULL, 10); | 1080 int sock = strtol(params[0], NULL, 10); |
1003 int result = close(sock); | 1081 int result = close(sock); |
1004 if (result != 0) { | 1082 if (result != 0) { |
1005 *output = PrintfToNewString("close returned error: %d", errno); | 1083 *output = PrintfToNewString("close returned error: %d", errno); |
1006 return 2; | 1084 return 2; |
1007 } | 1085 } |
1008 | 1086 |
1009 *output = PrintfToNewString("close\1%d", sock); | 1087 *output = PrintfToNewString("close\1%d", sock); |
1010 return 0; | 1088 return 0; |
1011 } | 1089 } |
OLD | NEW |