diff options
Diffstat (limited to 'usbmuxd')
| -rw-r--r-- | usbmuxd/.gitignore | 3 | ||||
| -rw-r--r-- | usbmuxd/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | usbmuxd/COPYING | 674 | ||||
| -rw-r--r-- | usbmuxd/FindUSB.cmake | 68 | ||||
| -rw-r--r-- | usbmuxd/Modules/FindUSB.cmake | 31 | ||||
| -rw-r--r-- | usbmuxd/Modules/LibFindMacros.cmake | 99 | ||||
| -rw-r--r-- | usbmuxd/client.c | 430 | ||||
| -rw-r--r-- | usbmuxd/client.h | 92 | ||||
| -rw-r--r-- | usbmuxd/device.c | 731 | ||||
| -rw-r--r-- | usbmuxd/device.h | 52 | ||||
| -rw-r--r-- | usbmuxd/log.c | 58 | ||||
| -rw-r--r-- | usbmuxd/log.h | 40 | ||||
| -rw-r--r-- | usbmuxd/main.c | 201 | ||||
| -rw-r--r-- | usbmuxd/usb-linux.c | 501 | ||||
| -rw-r--r-- | usbmuxd/usb.h | 67 | ||||
| -rw-r--r-- | usbmuxd/utils.c | 110 | ||||
| -rw-r--r-- | usbmuxd/utils.h | 65 |
17 files changed, 3238 insertions, 0 deletions
diff --git a/usbmuxd/.gitignore b/usbmuxd/.gitignore new file mode 100644 index 0000000..0784410 --- /dev/null +++ b/usbmuxd/.gitignore | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | *~ | ||
| 2 | build | ||
| 3 | |||
diff --git a/usbmuxd/CMakeLists.txt b/usbmuxd/CMakeLists.txt new file mode 100644 index 0000000..8d22ef6 --- /dev/null +++ b/usbmuxd/CMakeLists.txt | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | PROJECT(usbmuxd) | ||
| 2 | |||
| 3 | cmake_minimum_required(VERSION 2.6) | ||
| 4 | |||
| 5 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Modules/") | ||
| 6 | |||
| 7 | find_package(USB REQUIRED) | ||
| 8 | include_directories(${USB_INCLUDE_DIRS}) | ||
| 9 | set(LIBS ${LIBS} ${USB_LIBRARIES}) | ||
| 10 | |||
| 11 | #set(CMAKE_VERBOSE_MAKEFILE ON) | ||
| 12 | |||
| 13 | add_definitions(-Wall -O2 -g) | ||
| 14 | add_executable(usbmuxd main.c usb-linux.c log.c utils.c device.c client.c) | ||
| 15 | target_link_libraries(usbmuxd ${LIBS}) | ||
| 16 | |||
diff --git a/usbmuxd/COPYING b/usbmuxd/COPYING new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/usbmuxd/COPYING | |||
| @@ -0,0 +1,674 @@ | |||
| 1 | GNU GENERAL PUBLIC LICENSE | ||
| 2 | Version 3, 29 June 2007 | ||
| 3 | |||
| 4 | Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> | ||
| 5 | Everyone is permitted to copy and distribute verbatim copies | ||
| 6 | of this license document, but changing it is not allowed. | ||
| 7 | |||
| 8 | Preamble | ||
| 9 | |||
| 10 | The GNU General Public License is a free, copyleft license for | ||
| 11 | software and other kinds of works. | ||
| 12 | |||
| 13 | The licenses for most software and other practical works are designed | ||
| 14 | to take away your freedom to share and change the works. By contrast, | ||
| 15 | the GNU General Public License is intended to guarantee your freedom to | ||
| 16 | share and change all versions of a program--to make sure it remains free | ||
| 17 | software for all its users. We, the Free Software Foundation, use the | ||
| 18 | GNU General Public License for most of our software; it applies also to | ||
| 19 | any other work released this way by its authors. You can apply it to | ||
| 20 | your programs, too. | ||
| 21 | |||
| 22 | When we speak of free software, we are referring to freedom, not | ||
| 23 | price. Our General Public Licenses are designed to make sure that you | ||
| 24 | have the freedom to distribute copies of free software (and charge for | ||
| 25 | them if you wish), that you receive source code or can get it if you | ||
| 26 | want it, that you can change the software or use pieces of it in new | ||
| 27 | free programs, and that you know you can do these things. | ||
| 28 | |||
| 29 | To protect your rights, we need to prevent others from denying you | ||
| 30 | these rights or asking you to surrender the rights. Therefore, you have | ||
| 31 | certain responsibilities if you distribute copies of the software, or if | ||
| 32 | you modify it: responsibilities to respect the freedom of others. | ||
| 33 | |||
| 34 | For example, if you distribute copies of such a program, whether | ||
| 35 | gratis or for a fee, you must pass on to the recipients the same | ||
| 36 | freedoms that you received. You must make sure that they, too, receive | ||
| 37 | or can get the source code. And you must show them these terms so they | ||
| 38 | know their rights. | ||
| 39 | |||
| 40 | Developers that use the GNU GPL protect your rights with two steps: | ||
| 41 | (1) assert copyright on the software, and (2) offer you this License | ||
| 42 | giving you legal permission to copy, distribute and/or modify it. | ||
| 43 | |||
| 44 | For the developers' and authors' protection, the GPL clearly explains | ||
| 45 | that there is no warranty for this free software. For both users' and | ||
| 46 | authors' sake, the GPL requires that modified versions be marked as | ||
| 47 | changed, so that their problems will not be attributed erroneously to | ||
| 48 | authors of previous versions. | ||
| 49 | |||
| 50 | Some devices are designed to deny users access to install or run | ||
| 51 | modified versions of the software inside them, although the manufacturer | ||
| 52 | can do so. This is fundamentally incompatible with the aim of | ||
| 53 | protecting users' freedom to change the software. The systematic | ||
| 54 | pattern of such abuse occurs in the area of products for individuals to | ||
| 55 | use, which is precisely where it is most unacceptable. Therefore, we | ||
| 56 | have designed this version of the GPL to prohibit the practice for those | ||
| 57 | products. If such problems arise substantially in other domains, we | ||
| 58 | stand ready to extend this provision to those domains in future versions | ||
| 59 | of the GPL, as needed to protect the freedom of users. | ||
| 60 | |||
| 61 | Finally, every program is threatened constantly by software patents. | ||
| 62 | States should not allow patents to restrict development and use of | ||
| 63 | software on general-purpose computers, but in those that do, we wish to | ||
| 64 | avoid the special danger that patents applied to a free program could | ||
| 65 | make it effectively proprietary. To prevent this, the GPL assures that | ||
| 66 | patents cannot be used to render the program non-free. | ||
| 67 | |||
| 68 | The precise terms and conditions for copying, distribution and | ||
| 69 | modification follow. | ||
| 70 | |||
| 71 | TERMS AND CONDITIONS | ||
| 72 | |||
| 73 | 0. Definitions. | ||
| 74 | |||
| 75 | "This License" refers to version 3 of the GNU General Public License. | ||
| 76 | |||
| 77 | "Copyright" also means copyright-like laws that apply to other kinds of | ||
| 78 | works, such as semiconductor masks. | ||
| 79 | |||
| 80 | "The Program" refers to any copyrightable work licensed under this | ||
| 81 | License. Each licensee is addressed as "you". "Licensees" and | ||
| 82 | "recipients" may be individuals or organizations. | ||
| 83 | |||
| 84 | To "modify" a work means to copy from or adapt all or part of the work | ||
| 85 | in a fashion requiring copyright permission, other than the making of an | ||
| 86 | exact copy. The resulting work is called a "modified version" of the | ||
| 87 | earlier work or a work "based on" the earlier work. | ||
| 88 | |||
| 89 | A "covered work" means either the unmodified Program or a work based | ||
| 90 | on the Program. | ||
| 91 | |||
| 92 | To "propagate" a work means to do anything with it that, without | ||
| 93 | permission, would make you directly or secondarily liable for | ||
| 94 | infringement under applicable copyright law, except executing it on a | ||
| 95 | computer or modifying a private copy. Propagation includes copying, | ||
| 96 | distribution (with or without modification), making available to the | ||
| 97 | public, and in some countries other activities as well. | ||
| 98 | |||
| 99 | To "convey" a work means any kind of propagation that enables other | ||
| 100 | parties to make or receive copies. Mere interaction with a user through | ||
| 101 | a computer network, with no transfer of a copy, is not conveying. | ||
| 102 | |||
| 103 | An interactive user interface displays "Appropriate Legal Notices" | ||
| 104 | to the extent that it includes a convenient and prominently visible | ||
| 105 | feature that (1) displays an appropriate copyright notice, and (2) | ||
| 106 | tells the user that there is no warranty for the work (except to the | ||
| 107 | extent that warranties are provided), that licensees may convey the | ||
| 108 | work under this License, and how to view a copy of this License. If | ||
| 109 | the interface presents a list of user commands or options, such as a | ||
| 110 | menu, a prominent item in the list meets this criterion. | ||
| 111 | |||
| 112 | 1. Source Code. | ||
| 113 | |||
| 114 | The "source code" for a work means the preferred form of the work | ||
| 115 | for making modifications to it. "Object code" means any non-source | ||
| 116 | form of a work. | ||
| 117 | |||
| 118 | A "Standard Interface" means an interface that either is an official | ||
| 119 | standard defined by a recognized standards body, or, in the case of | ||
| 120 | interfaces specified for a particular programming language, one that | ||
| 121 | is widely used among developers working in that language. | ||
| 122 | |||
| 123 | The "System Libraries" of an executable work include anything, other | ||
| 124 | than the work as a whole, that (a) is included in the normal form of | ||
| 125 | packaging a Major Component, but which is not part of that Major | ||
| 126 | Component, and (b) serves only to enable use of the work with that | ||
| 127 | Major Component, or to implement a Standard Interface for which an | ||
| 128 | implementation is available to the public in source code form. A | ||
| 129 | "Major Component", in this context, means a major essential component | ||
| 130 | (kernel, window system, and so on) of the specific operating system | ||
| 131 | (if any) on which the executable work runs, or a compiler used to | ||
| 132 | produce the work, or an object code interpreter used to run it. | ||
| 133 | |||
| 134 | The "Corresponding Source" for a work in object code form means all | ||
| 135 | the source code needed to generate, install, and (for an executable | ||
| 136 | work) run the object code and to modify the work, including scripts to | ||
| 137 | control those activities. However, it does not include the work's | ||
| 138 | System Libraries, or general-purpose tools or generally available free | ||
| 139 | programs which are used unmodified in performing those activities but | ||
| 140 | which are not part of the work. For example, Corresponding Source | ||
| 141 | includes interface definition files associated with source files for | ||
| 142 | the work, and the source code for shared libraries and dynamically | ||
| 143 | linked subprograms that the work is specifically designed to require, | ||
| 144 | such as by intimate data communication or control flow between those | ||
| 145 | subprograms and other parts of the work. | ||
| 146 | |||
| 147 | The Corresponding Source need not include anything that users | ||
| 148 | can regenerate automatically from other parts of the Corresponding | ||
| 149 | Source. | ||
| 150 | |||
| 151 | The Corresponding Source for a work in source code form is that | ||
| 152 | same work. | ||
| 153 | |||
| 154 | 2. Basic Permissions. | ||
| 155 | |||
| 156 | All rights granted under this License are granted for the term of | ||
| 157 | copyright on the Program, and are irrevocable provided the stated | ||
| 158 | conditions are met. This License explicitly affirms your unlimited | ||
| 159 | permission to run the unmodified Program. The output from running a | ||
| 160 | covered work is covered by this License only if the output, given its | ||
| 161 | content, constitutes a covered work. This License acknowledges your | ||
| 162 | rights of fair use or other equivalent, as provided by copyright law. | ||
| 163 | |||
| 164 | You may make, run and propagate covered works that you do not | ||
| 165 | convey, without conditions so long as your license otherwise remains | ||
| 166 | in force. You may convey covered works to others for the sole purpose | ||
| 167 | of having them make modifications exclusively for you, or provide you | ||
| 168 | with facilities for running those works, provided that you comply with | ||
| 169 | the terms of this License in conveying all material for which you do | ||
| 170 | not control copyright. Those thus making or running the covered works | ||
| 171 | for you must do so exclusively on your behalf, under your direction | ||
| 172 | and control, on terms that prohibit them from making any copies of | ||
| 173 | your copyrighted material outside their relationship with you. | ||
| 174 | |||
| 175 | Conveying under any other circumstances is permitted solely under | ||
| 176 | the conditions stated below. Sublicensing is not allowed; section 10 | ||
| 177 | makes it unnecessary. | ||
| 178 | |||
| 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. | ||
| 180 | |||
| 181 | No covered work shall be deemed part of an effective technological | ||
| 182 | measure under any applicable law fulfilling obligations under article | ||
| 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or | ||
| 184 | similar laws prohibiting or restricting circumvention of such | ||
| 185 | measures. | ||
| 186 | |||
| 187 | When you convey a covered work, you waive any legal power to forbid | ||
| 188 | circumvention of technological measures to the extent such circumvention | ||
| 189 | is effected by exercising rights under this License with respect to | ||
| 190 | the covered work, and you disclaim any intention to limit operation or | ||
| 191 | modification of the work as a means of enforcing, against the work's | ||
| 192 | users, your or third parties' legal rights to forbid circumvention of | ||
| 193 | technological measures. | ||
| 194 | |||
| 195 | 4. Conveying Verbatim Copies. | ||
| 196 | |||
| 197 | You may convey verbatim copies of the Program's source code as you | ||
| 198 | receive it, in any medium, provided that you conspicuously and | ||
| 199 | appropriately publish on each copy an appropriate copyright notice; | ||
| 200 | keep intact all notices stating that this License and any | ||
| 201 | non-permissive terms added in accord with section 7 apply to the code; | ||
| 202 | keep intact all notices of the absence of any warranty; and give all | ||
| 203 | recipients a copy of this License along with the Program. | ||
| 204 | |||
| 205 | You may charge any price or no price for each copy that you convey, | ||
| 206 | and you may offer support or warranty protection for a fee. | ||
| 207 | |||
| 208 | 5. Conveying Modified Source Versions. | ||
| 209 | |||
| 210 | You may convey a work based on the Program, or the modifications to | ||
| 211 | produce it from the Program, in the form of source code under the | ||
| 212 | terms of section 4, provided that you also meet all of these conditions: | ||
| 213 | |||
| 214 | a) The work must carry prominent notices stating that you modified | ||
| 215 | it, and giving a relevant date. | ||
| 216 | |||
| 217 | b) The work must carry prominent notices stating that it is | ||
| 218 | released under this License and any conditions added under section | ||
| 219 | 7. This requirement modifies the requirement in section 4 to | ||
| 220 | "keep intact all notices". | ||
| 221 | |||
| 222 | c) You must license the entire work, as a whole, under this | ||
| 223 | License to anyone who comes into possession of a copy. This | ||
| 224 | License will therefore apply, along with any applicable section 7 | ||
| 225 | additional terms, to the whole of the work, and all its parts, | ||
| 226 | regardless of how they are packaged. This License gives no | ||
| 227 | permission to license the work in any other way, but it does not | ||
| 228 | invalidate such permission if you have separately received it. | ||
| 229 | |||
| 230 | d) If the work has interactive user interfaces, each must display | ||
| 231 | Appropriate Legal Notices; however, if the Program has interactive | ||
| 232 | interfaces that do not display Appropriate Legal Notices, your | ||
| 233 | work need not make them do so. | ||
| 234 | |||
| 235 | A compilation of a covered work with other separate and independent | ||
| 236 | works, which are not by their nature extensions of the covered work, | ||
| 237 | and which are not combined with it such as to form a larger program, | ||
| 238 | in or on a volume of a storage or distribution medium, is called an | ||
| 239 | "aggregate" if the compilation and its resulting copyright are not | ||
| 240 | used to limit the access or legal rights of the compilation's users | ||
| 241 | beyond what the individual works permit. Inclusion of a covered work | ||
| 242 | in an aggregate does not cause this License to apply to the other | ||
| 243 | parts of the aggregate. | ||
| 244 | |||
| 245 | 6. Conveying Non-Source Forms. | ||
| 246 | |||
| 247 | You may convey a covered work in object code form under the terms | ||
| 248 | of sections 4 and 5, provided that you also convey the | ||
| 249 | machine-readable Corresponding Source under the terms of this License, | ||
| 250 | in one of these ways: | ||
| 251 | |||
| 252 | a) Convey the object code in, or embodied in, a physical product | ||
| 253 | (including a physical distribution medium), accompanied by the | ||
| 254 | Corresponding Source fixed on a durable physical medium | ||
| 255 | customarily used for software interchange. | ||
| 256 | |||
| 257 | b) Convey the object code in, or embodied in, a physical product | ||
| 258 | (including a physical distribution medium), accompanied by a | ||
| 259 | written offer, valid for at least three years and valid for as | ||
| 260 | long as you offer spare parts or customer support for that product | ||
| 261 | model, to give anyone who possesses the object code either (1) a | ||
| 262 | copy of the Corresponding Source for all the software in the | ||
| 263 | product that is covered by this License, on a durable physical | ||
| 264 | medium customarily used for software interchange, for a price no | ||
| 265 | more than your reasonable cost of physically performing this | ||
| 266 | conveying of source, or (2) access to copy the | ||
| 267 | Corresponding Source from a network server at no charge. | ||
| 268 | |||
| 269 | c) Convey individual copies of the object code with a copy of the | ||
| 270 | written offer to provide the Corresponding Source. This | ||
| 271 | alternative is allowed only occasionally and noncommercially, and | ||
| 272 | only if you received the object code with such an offer, in accord | ||
| 273 | with subsection 6b. | ||
| 274 | |||
| 275 | d) Convey the object code by offering access from a designated | ||
| 276 | place (gratis or for a charge), and offer equivalent access to the | ||
| 277 | Corresponding Source in the same way through the same place at no | ||
| 278 | further charge. You need not require recipients to copy the | ||
| 279 | Corresponding Source along with the object code. If the place to | ||
| 280 | copy the object code is a network server, the Corresponding Source | ||
| 281 | may be on a different server (operated by you or a third party) | ||
| 282 | that supports equivalent copying facilities, provided you maintain | ||
| 283 | clear directions next to the object code saying where to find the | ||
| 284 | Corresponding Source. Regardless of what server hosts the | ||
| 285 | Corresponding Source, you remain obligated to ensure that it is | ||
| 286 | available for as long as needed to satisfy these requirements. | ||
| 287 | |||
| 288 | e) Convey the object code using peer-to-peer transmission, provided | ||
| 289 | you inform other peers where the object code and Corresponding | ||
| 290 | Source of the work are being offered to the general public at no | ||
| 291 | charge under subsection 6d. | ||
| 292 | |||
| 293 | A separable portion of the object code, whose source code is excluded | ||
| 294 | from the Corresponding Source as a System Library, need not be | ||
| 295 | included in conveying the object code work. | ||
| 296 | |||
| 297 | A "User Product" is either (1) a "consumer product", which means any | ||
| 298 | tangible personal property which is normally used for personal, family, | ||
| 299 | or household purposes, or (2) anything designed or sold for incorporation | ||
| 300 | into a dwelling. In determining whether a product is a consumer product, | ||
| 301 | doubtful cases shall be resolved in favor of coverage. For a particular | ||
| 302 | product received by a particular user, "normally used" refers to a | ||
| 303 | typical or common use of that class of product, regardless of the status | ||
| 304 | of the particular user or of the way in which the particular user | ||
| 305 | actually uses, or expects or is expected to use, the product. A product | ||
| 306 | is a consumer product regardless of whether the product has substantial | ||
| 307 | commercial, industrial or non-consumer uses, unless such uses represent | ||
| 308 | the only significant mode of use of the product. | ||
| 309 | |||
| 310 | "Installation Information" for a User Product means any methods, | ||
| 311 | procedures, authorization keys, or other information required to install | ||
| 312 | and execute modified versions of a covered work in that User Product from | ||
| 313 | a modified version of its Corresponding Source. The information must | ||
| 314 | suffice to ensure that the continued functioning of the modified object | ||
| 315 | code is in no case prevented or interfered with solely because | ||
| 316 | modification has been made. | ||
| 317 | |||
| 318 | If you convey an object code work under this section in, or with, or | ||
| 319 | specifically for use in, a User Product, and the conveying occurs as | ||
| 320 | part of a transaction in which the right of possession and use of the | ||
| 321 | User Product is transferred to the recipient in perpetuity or for a | ||
| 322 | fixed term (regardless of how the transaction is characterized), the | ||
| 323 | Corresponding Source conveyed under this section must be accompanied | ||
| 324 | by the Installation Information. But this requirement does not apply | ||
| 325 | if neither you nor any third party retains the ability to install | ||
| 326 | modified object code on the User Product (for example, the work has | ||
| 327 | been installed in ROM). | ||
| 328 | |||
| 329 | The requirement to provide Installation Information does not include a | ||
| 330 | requirement to continue to provide support service, warranty, or updates | ||
| 331 | for a work that has been modified or installed by the recipient, or for | ||
| 332 | the User Product in which it has been modified or installed. Access to a | ||
| 333 | network may be denied when the modification itself materially and | ||
| 334 | adversely affects the operation of the network or violates the rules and | ||
| 335 | protocols for communication across the network. | ||
| 336 | |||
| 337 | Corresponding Source conveyed, and Installation Information provided, | ||
| 338 | in accord with this section must be in a format that is publicly | ||
| 339 | documented (and with an implementation available to the public in | ||
| 340 | source code form), and must require no special password or key for | ||
| 341 | unpacking, reading or copying. | ||
| 342 | |||
| 343 | 7. Additional Terms. | ||
| 344 | |||
| 345 | "Additional permissions" are terms that supplement the terms of this | ||
| 346 | License by making exceptions from one or more of its conditions. | ||
| 347 | Additional permissions that are applicable to the entire Program shall | ||
| 348 | be treated as though they were included in this License, to the extent | ||
| 349 | that they are valid under applicable law. If additional permissions | ||
| 350 | apply only to part of the Program, that part may be used separately | ||
| 351 | under those permissions, but the entire Program remains governed by | ||
| 352 | this License without regard to the additional permissions. | ||
| 353 | |||
| 354 | When you convey a copy of a covered work, you may at your option | ||
| 355 | remove any additional permissions from that copy, or from any part of | ||
| 356 | it. (Additional permissions may be written to require their own | ||
| 357 | removal in certain cases when you modify the work.) You may place | ||
| 358 | additional permissions on material, added by you to a covered work, | ||
| 359 | for which you have or can give appropriate copyright permission. | ||
| 360 | |||
| 361 | Notwithstanding any other provision of this License, for material you | ||
| 362 | add to a covered work, you may (if authorized by the copyright holders of | ||
| 363 | that material) supplement the terms of this License with terms: | ||
| 364 | |||
| 365 | a) Disclaiming warranty or limiting liability differently from the | ||
| 366 | terms of sections 15 and 16 of this License; or | ||
| 367 | |||
| 368 | b) Requiring preservation of specified reasonable legal notices or | ||
| 369 | author attributions in that material or in the Appropriate Legal | ||
| 370 | Notices displayed by works containing it; or | ||
| 371 | |||
| 372 | c) Prohibiting misrepresentation of the origin of that material, or | ||
| 373 | requiring that modified versions of such material be marked in | ||
| 374 | reasonable ways as different from the original version; or | ||
| 375 | |||
| 376 | d) Limiting the use for publicity purposes of names of licensors or | ||
| 377 | authors of the material; or | ||
| 378 | |||
| 379 | e) Declining to grant rights under trademark law for use of some | ||
| 380 | trade names, trademarks, or service marks; or | ||
| 381 | |||
| 382 | f) Requiring indemnification of licensors and authors of that | ||
| 383 | material by anyone who conveys the material (or modified versions of | ||
| 384 | it) with contractual assumptions of liability to the recipient, for | ||
| 385 | any liability that these contractual assumptions directly impose on | ||
| 386 | those licensors and authors. | ||
| 387 | |||
| 388 | All other non-permissive additional terms are considered "further | ||
| 389 | restrictions" within the meaning of section 10. If the Program as you | ||
| 390 | received it, or any part of it, contains a notice stating that it is | ||
| 391 | governed by this License along with a term that is a further | ||
| 392 | restriction, you may remove that term. If a license document contains | ||
| 393 | a further restriction but permits relicensing or conveying under this | ||
| 394 | License, you may add to a covered work material governed by the terms | ||
| 395 | of that license document, provided that the further restriction does | ||
| 396 | not survive such relicensing or conveying. | ||
| 397 | |||
| 398 | If you add terms to a covered work in accord with this section, you | ||
| 399 | must place, in the relevant source files, a statement of the | ||
| 400 | additional terms that apply to those files, or a notice indicating | ||
| 401 | where to find the applicable terms. | ||
| 402 | |||
| 403 | Additional terms, permissive or non-permissive, may be stated in the | ||
| 404 | form of a separately written license, or stated as exceptions; | ||
| 405 | the above requirements apply either way. | ||
| 406 | |||
| 407 | 8. Termination. | ||
| 408 | |||
| 409 | You may not propagate or modify a covered work except as expressly | ||
| 410 | provided under this License. Any attempt otherwise to propagate or | ||
| 411 | modify it is void, and will automatically terminate your rights under | ||
| 412 | this License (including any patent licenses granted under the third | ||
| 413 | paragraph of section 11). | ||
| 414 | |||
| 415 | However, if you cease all violation of this License, then your | ||
| 416 | license from a particular copyright holder is reinstated (a) | ||
| 417 | provisionally, unless and until the copyright holder explicitly and | ||
| 418 | finally terminates your license, and (b) permanently, if the copyright | ||
| 419 | holder fails to notify you of the violation by some reasonable means | ||
| 420 | prior to 60 days after the cessation. | ||
| 421 | |||
| 422 | Moreover, your license from a particular copyright holder is | ||
| 423 | reinstated permanently if the copyright holder notifies you of the | ||
| 424 | violation by some reasonable means, this is the first time you have | ||
| 425 | received notice of violation of this License (for any work) from that | ||
| 426 | copyright holder, and you cure the violation prior to 30 days after | ||
| 427 | your receipt of the notice. | ||
| 428 | |||
| 429 | Termination of your rights under this section does not terminate the | ||
| 430 | licenses of parties who have received copies or rights from you under | ||
| 431 | this License. If your rights have been terminated and not permanently | ||
| 432 | reinstated, you do not qualify to receive new licenses for the same | ||
| 433 | material under section 10. | ||
| 434 | |||
| 435 | 9. Acceptance Not Required for Having Copies. | ||
| 436 | |||
| 437 | You are not required to accept this License in order to receive or | ||
| 438 | run a copy of the Program. Ancillary propagation of a covered work | ||
| 439 | occurring solely as a consequence of using peer-to-peer transmission | ||
| 440 | to receive a copy likewise does not require acceptance. However, | ||
| 441 | nothing other than this License grants you permission to propagate or | ||
| 442 | modify any covered work. These actions infringe copyright if you do | ||
| 443 | not accept this License. Therefore, by modifying or propagating a | ||
| 444 | covered work, you indicate your acceptance of this License to do so. | ||
| 445 | |||
| 446 | 10. Automatic Licensing of Downstream Recipients. | ||
| 447 | |||
| 448 | Each time you convey a covered work, the recipient automatically | ||
| 449 | receives a license from the original licensors, to run, modify and | ||
| 450 | propagate that work, subject to this License. You are not responsible | ||
| 451 | for enforcing compliance by third parties with this License. | ||
| 452 | |||
| 453 | An "entity transaction" is a transaction transferring control of an | ||
| 454 | organization, or substantially all assets of one, or subdividing an | ||
| 455 | organization, or merging organizations. If propagation of a covered | ||
| 456 | work results from an entity transaction, each party to that | ||
| 457 | transaction who receives a copy of the work also receives whatever | ||
| 458 | licenses to the work the party's predecessor in interest had or could | ||
| 459 | give under the previous paragraph, plus a right to possession of the | ||
| 460 | Corresponding Source of the work from the predecessor in interest, if | ||
| 461 | the predecessor has it or can get it with reasonable efforts. | ||
| 462 | |||
| 463 | You may not impose any further restrictions on the exercise of the | ||
| 464 | rights granted or affirmed under this License. For example, you may | ||
| 465 | not impose a license fee, royalty, or other charge for exercise of | ||
| 466 | rights granted under this License, and you may not initiate litigation | ||
| 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that | ||
| 468 | any patent claim is infringed by making, using, selling, offering for | ||
| 469 | sale, or importing the Program or any portion of it. | ||
| 470 | |||
| 471 | 11. Patents. | ||
| 472 | |||
| 473 | A "contributor" is a copyright holder who authorizes use under this | ||
| 474 | License of the Program or a work on which the Program is based. The | ||
| 475 | work thus licensed is called the contributor's "contributor version". | ||
| 476 | |||
| 477 | A contributor's "essential patent claims" are all patent claims | ||
| 478 | owned or controlled by the contributor, whether already acquired or | ||
| 479 | hereafter acquired, that would be infringed by some manner, permitted | ||
| 480 | by this License, of making, using, or selling its contributor version, | ||
| 481 | but do not include claims that would be infringed only as a | ||
| 482 | consequence of further modification of the contributor version. For | ||
| 483 | purposes of this definition, "control" includes the right to grant | ||
| 484 | patent sublicenses in a manner consistent with the requirements of | ||
| 485 | this License. | ||
| 486 | |||
| 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free | ||
| 488 | patent license under the contributor's essential patent claims, to | ||
| 489 | make, use, sell, offer for sale, import and otherwise run, modify and | ||
| 490 | propagate the contents of its contributor version. | ||
| 491 | |||
| 492 | In the following three paragraphs, a "patent license" is any express | ||
| 493 | agreement or commitment, however denominated, not to enforce a patent | ||
| 494 | (such as an express permission to practice a patent or covenant not to | ||
| 495 | sue for patent infringement). To "grant" such a patent license to a | ||
| 496 | party means to make such an agreement or commitment not to enforce a | ||
| 497 | patent against the party. | ||
| 498 | |||
| 499 | If you convey a covered work, knowingly relying on a patent license, | ||
| 500 | and the Corresponding Source of the work is not available for anyone | ||
| 501 | to copy, free of charge and under the terms of this License, through a | ||
| 502 | publicly available network server or other readily accessible means, | ||
| 503 | then you must either (1) cause the Corresponding Source to be so | ||
| 504 | available, or (2) arrange to deprive yourself of the benefit of the | ||
| 505 | patent license for this particular work, or (3) arrange, in a manner | ||
| 506 | consistent with the requirements of this License, to extend the patent | ||
| 507 | license to downstream recipients. "Knowingly relying" means you have | ||
| 508 | actual knowledge that, but for the patent license, your conveying the | ||
| 509 | covered work in a country, or your recipient's use of the covered work | ||
| 510 | in a country, would infringe one or more identifiable patents in that | ||
| 511 | country that you have reason to believe are valid. | ||
| 512 | |||
| 513 | If, pursuant to or in connection with a single transaction or | ||
| 514 | arrangement, you convey, or propagate by procuring conveyance of, a | ||
| 515 | covered work, and grant a patent license to some of the parties | ||
| 516 | receiving the covered work authorizing them to use, propagate, modify | ||
| 517 | or convey a specific copy of the covered work, then the patent license | ||
| 518 | you grant is automatically extended to all recipients of the covered | ||
| 519 | work and works based on it. | ||
| 520 | |||
| 521 | A patent license is "discriminatory" if it does not include within | ||
| 522 | the scope of its coverage, prohibits the exercise of, or is | ||
| 523 | conditioned on the non-exercise of one or more of the rights that are | ||
| 524 | specifically granted under this License. You may not convey a covered | ||
| 525 | work if you are a party to an arrangement with a third party that is | ||
| 526 | in the business of distributing software, under which you make payment | ||
| 527 | to the third party based on the extent of your activity of conveying | ||
| 528 | the work, and under which the third party grants, to any of the | ||
| 529 | parties who would receive the covered work from you, a discriminatory | ||
| 530 | patent license (a) in connection with copies of the covered work | ||
| 531 | conveyed by you (or copies made from those copies), or (b) primarily | ||
| 532 | for and in connection with specific products or compilations that | ||
| 533 | contain the covered work, unless you entered into that arrangement, | ||
| 534 | or that patent license was granted, prior to 28 March 2007. | ||
| 535 | |||
| 536 | Nothing in this License shall be construed as excluding or limiting | ||
| 537 | any implied license or other defenses to infringement that may | ||
| 538 | otherwise be available to you under applicable patent law. | ||
| 539 | |||
| 540 | 12. No Surrender of Others' Freedom. | ||
| 541 | |||
| 542 | If conditions are imposed on you (whether by court order, agreement or | ||
| 543 | otherwise) that contradict the conditions of this License, they do not | ||
| 544 | excuse you from the conditions of this License. If you cannot convey a | ||
| 545 | covered work so as to satisfy simultaneously your obligations under this | ||
| 546 | License and any other pertinent obligations, then as a consequence you may | ||
| 547 | not convey it at all. For example, if you agree to terms that obligate you | ||
| 548 | to collect a royalty for further conveying from those to whom you convey | ||
| 549 | the Program, the only way you could satisfy both those terms and this | ||
| 550 | License would be to refrain entirely from conveying the Program. | ||
| 551 | |||
| 552 | 13. Use with the GNU Affero General Public License. | ||
| 553 | |||
| 554 | Notwithstanding any other provision of this License, you have | ||
| 555 | permission to link or combine any covered work with a work licensed | ||
| 556 | under version 3 of the GNU Affero General Public License into a single | ||
| 557 | combined work, and to convey the resulting work. The terms of this | ||
| 558 | License will continue to apply to the part which is the covered work, | ||
| 559 | but the special requirements of the GNU Affero General Public License, | ||
| 560 | section 13, concerning interaction through a network will apply to the | ||
| 561 | combination as such. | ||
| 562 | |||
| 563 | 14. Revised Versions of this License. | ||
| 564 | |||
| 565 | The Free Software Foundation may publish revised and/or new versions of | ||
| 566 | the GNU General Public License from time to time. Such new versions will | ||
| 567 | be similar in spirit to the present version, but may differ in detail to | ||
| 568 | address new problems or concerns. | ||
| 569 | |||
| 570 | Each version is given a distinguishing version number. If the | ||
| 571 | Program specifies that a certain numbered version of the GNU General | ||
| 572 | Public License "or any later version" applies to it, you have the | ||
| 573 | option of following the terms and conditions either of that numbered | ||
| 574 | version or of any later version published by the Free Software | ||
| 575 | Foundation. If the Program does not specify a version number of the | ||
| 576 | GNU General Public License, you may choose any version ever published | ||
| 577 | by the Free Software Foundation. | ||
| 578 | |||
| 579 | If the Program specifies that a proxy can decide which future | ||
| 580 | versions of the GNU General Public License can be used, that proxy's | ||
| 581 | public statement of acceptance of a version permanently authorizes you | ||
| 582 | to choose that version for the Program. | ||
| 583 | |||
| 584 | Later license versions may give you additional or different | ||
| 585 | permissions. However, no additional obligations are imposed on any | ||
| 586 | author or copyright holder as a result of your choosing to follow a | ||
| 587 | later version. | ||
| 588 | |||
| 589 | 15. Disclaimer of Warranty. | ||
| 590 | |||
| 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY | ||
| 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT | ||
| 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY | ||
| 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, | ||
| 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM | ||
| 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF | ||
| 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | ||
| 599 | |||
| 600 | 16. Limitation of Liability. | ||
| 601 | |||
| 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS | ||
| 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY | ||
| 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE | ||
| 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF | ||
| 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD | ||
| 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), | ||
| 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | ||
| 610 | SUCH DAMAGES. | ||
| 611 | |||
| 612 | 17. Interpretation of Sections 15 and 16. | ||
| 613 | |||
| 614 | If the disclaimer of warranty and limitation of liability provided | ||
| 615 | above cannot be given local legal effect according to their terms, | ||
| 616 | reviewing courts shall apply local law that most closely approximates | ||
| 617 | an absolute waiver of all civil liability in connection with the | ||
| 618 | Program, unless a warranty or assumption of liability accompanies a | ||
| 619 | copy of the Program in return for a fee. | ||
| 620 | |||
| 621 | END OF TERMS AND CONDITIONS | ||
| 622 | |||
| 623 | How to Apply These Terms to Your New Programs | ||
| 624 | |||
| 625 | If you develop a new program, and you want it to be of the greatest | ||
| 626 | possible use to the public, the best way to achieve this is to make it | ||
| 627 | free software which everyone can redistribute and change under these terms. | ||
| 628 | |||
| 629 | To do so, attach the following notices to the program. It is safest | ||
| 630 | to attach them to the start of each source file to most effectively | ||
| 631 | state the exclusion of warranty; and each file should have at least | ||
| 632 | the "copyright" line and a pointer to where the full notice is found. | ||
| 633 | |||
| 634 | <one line to give the program's name and a brief idea of what it does.> | ||
| 635 | Copyright (C) <year> <name of author> | ||
| 636 | |||
| 637 | This program is free software: you can redistribute it and/or modify | ||
| 638 | it under the terms of the GNU General Public License as published by | ||
| 639 | the Free Software Foundation, either version 3 of the License, or | ||
| 640 | (at your option) any later version. | ||
| 641 | |||
| 642 | This program is distributed in the hope that it will be useful, | ||
| 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 645 | GNU General Public License for more details. | ||
| 646 | |||
| 647 | You should have received a copy of the GNU General Public License | ||
| 648 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 649 | |||
| 650 | Also add information on how to contact you by electronic and paper mail. | ||
| 651 | |||
| 652 | If the program does terminal interaction, make it output a short | ||
| 653 | notice like this when it starts in an interactive mode: | ||
| 654 | |||
| 655 | <program> Copyright (C) <year> <name of author> | ||
| 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | ||
| 657 | This is free software, and you are welcome to redistribute it | ||
| 658 | under certain conditions; type `show c' for details. | ||
| 659 | |||
| 660 | The hypothetical commands `show w' and `show c' should show the appropriate | ||
| 661 | parts of the General Public License. Of course, your program's commands | ||
| 662 | might be different; for a GUI interface, you would use an "about box". | ||
| 663 | |||
| 664 | You should also get your employer (if you work as a programmer) or school, | ||
| 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. | ||
| 666 | For more information on this, and how to apply and follow the GNU GPL, see | ||
| 667 | <http://www.gnu.org/licenses/>. | ||
| 668 | |||
| 669 | The GNU General Public License does not permit incorporating your program | ||
| 670 | into proprietary programs. If your program is a subroutine library, you | ||
| 671 | may consider it more useful to permit linking proprietary applications with | ||
| 672 | the library. If this is what you want to do, use the GNU Lesser General | ||
| 673 | Public License instead of this License. But first, please read | ||
| 674 | <http://www.gnu.org/philosophy/why-not-lgpl.html>. | ||
diff --git a/usbmuxd/FindUSB.cmake b/usbmuxd/FindUSB.cmake new file mode 100644 index 0000000..e81d51e --- /dev/null +++ b/usbmuxd/FindUSB.cmake | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | # - Try to find USB | ||
| 2 | # Once done this will define | ||
| 3 | # | ||
| 4 | # USB_FOUND - system has USB | ||
| 5 | # USB_INCLUDE_DIRS - the USB include directory | ||
| 6 | # USB_LIBRARIES - Link these to use USB | ||
| 7 | # USB_DEFINITIONS - Compiler switches required for using USB | ||
| 8 | # | ||
| 9 | # Copyright (c) 2006 Andreas Schneider <mail@cynapses.org> | ||
| 10 | # Copyright (c) 2008 Andreas Schneider <mail@cynapses.org> | ||
| 11 | # | ||
| 12 | # Redistribution and use is allowed according to the terms of the New | ||
| 13 | # BSD license. | ||
| 14 | # For details see the accompanying COPYING-CMAKE-SCRIPTS file. | ||
| 15 | # | ||
| 16 | |||
| 17 | |||
| 18 | if (USB_LIBRARIES AND USB_INCLUDE_DIRS) | ||
| 19 | # in cache already | ||
| 20 | set(USB_FOUND TRUE) | ||
| 21 | else (USB_LIBRARIES AND USB_INCLUDE_DIRS) | ||
| 22 | find_path(USB_INCLUDE_DIR | ||
| 23 | NAMES | ||
| 24 | usb.h | ||
| 25 | PATHS | ||
| 26 | /usr/include | ||
| 27 | /usr/local/include | ||
| 28 | /opt/local/include | ||
| 29 | /sw/include | ||
| 30 | ) | ||
| 31 | |||
| 32 | find_library(USB_LIBRARY | ||
| 33 | NAMES | ||
| 34 | usb | ||
| 35 | PATHS | ||
| 36 | /usr/lib | ||
| 37 | /usr/local/lib | ||
| 38 | /opt/local/lib | ||
| 39 | /sw/lib | ||
| 40 | ) | ||
| 41 | |||
| 42 | set(USB_INCLUDE_DIRS | ||
| 43 | ${USB_INCLUDE_DIR} | ||
| 44 | ) | ||
| 45 | set(USB_LIBRARIES | ||
| 46 | ${USB_LIBRARY} | ||
| 47 | ) | ||
| 48 | |||
| 49 | if (USB_INCLUDE_DIRS AND USB_LIBRARIES) | ||
| 50 | set(USB_FOUND TRUE) | ||
| 51 | endif (USB_INCLUDE_DIRS AND USB_LIBRARIES) | ||
| 52 | |||
| 53 | if (USB_FOUND) | ||
| 54 | if (NOT USB_FIND_QUIETLY) | ||
| 55 | message(STATUS "Found USB: ${USB_LIBRARIES}") | ||
| 56 | endif (NOT USB_FIND_QUIETLY) | ||
| 57 | else (USB_FOUND) | ||
| 58 | if (USB_FIND_REQUIRED) | ||
| 59 | message(FATAL_ERROR "Could not find USB") | ||
| 60 | endif (USB_FIND_REQUIRED) | ||
| 61 | endif (USB_FOUND) | ||
| 62 | |||
| 63 | # show the USB_INCLUDE_DIRS and USB_LIBRARIES variables only in the advanced view | ||
| 64 | mark_as_advanced(USB_INCLUDE_DIRS USB_LIBRARIES) | ||
| 65 | |||
| 66 | endif (USB_LIBRARIES AND USB_INCLUDE_DIRS) | ||
| 67 | |||
| 68 | |||
diff --git a/usbmuxd/Modules/FindUSB.cmake b/usbmuxd/Modules/FindUSB.cmake new file mode 100644 index 0000000..d562b98 --- /dev/null +++ b/usbmuxd/Modules/FindUSB.cmake | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | # - Try to find libusb-1.0 | ||
| 2 | # Once done, this will define | ||
| 3 | # | ||
| 4 | # USB_FOUND - system has libusb-1.0 | ||
| 5 | # USB_INCLUDE_DIRS - the libusb-1.0 include directories | ||
| 6 | # USB_LIBRARIES - link these to use libusb-1.0 | ||
| 7 | |||
| 8 | include(LibFindMacros) | ||
| 9 | |||
| 10 | # Dependencies | ||
| 11 | |||
| 12 | # Use pkg-config to get hints about paths | ||
| 13 | libfind_pkg_check_modules(USB_PKGCONF libusb-1.0) | ||
| 14 | |||
| 15 | # Include dir | ||
| 16 | find_path(USB_INCLUDE_DIR | ||
| 17 | NAMES libusb.h | ||
| 18 | PATHS ${USB_PKGCONF_INCLUDE_DIRS} | ||
| 19 | ) | ||
| 20 | |||
| 21 | # Finally the library itself | ||
| 22 | find_library(USB_LIBRARY | ||
| 23 | NAMES usb-1.0 | ||
| 24 | PATHS ${USB_PKGCONF_LIBRARY_DIRS} | ||
| 25 | ) | ||
| 26 | |||
| 27 | # Set the include dir variables and the libraries and let libfind_process do the rest. | ||
| 28 | # NOTE: Singular variables for this library, plural for libraries this this lib depends on. | ||
| 29 | set(USB_PROCESS_INCLUDES USB_INCLUDE_DIR) | ||
| 30 | set(USB_PROCESS_LIBS USB_LIBRARY) | ||
| 31 | libfind_process(USB) | ||
diff --git a/usbmuxd/Modules/LibFindMacros.cmake b/usbmuxd/Modules/LibFindMacros.cmake new file mode 100644 index 0000000..795d6b7 --- /dev/null +++ b/usbmuxd/Modules/LibFindMacros.cmake | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | # Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments | ||
| 2 | # used for the current package. For this to work, the first parameter must be the | ||
| 3 | # prefix of the current package, then the prefix of the new package etc, which are | ||
| 4 | # passed to find_package. | ||
| 5 | macro (libfind_package PREFIX) | ||
| 6 | set (LIBFIND_PACKAGE_ARGS ${ARGN}) | ||
| 7 | if (${PREFIX}_FIND_QUIETLY) | ||
| 8 | set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET) | ||
| 9 | endif (${PREFIX}_FIND_QUIETLY) | ||
| 10 | if (${PREFIX}_FIND_REQUIRED) | ||
| 11 | set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED) | ||
| 12 | endif (${PREFIX}_FIND_REQUIRED) | ||
| 13 | find_package(${LIBFIND_PACKAGE_ARGS}) | ||
| 14 | endmacro (libfind_package) | ||
| 15 | |||
| 16 | # Damn CMake developers made the UsePkgConfig system deprecated in the same release (2.6) | ||
| 17 | # where they added pkg_check_modules. Consequently I need to support both in my scripts | ||
| 18 | # to avoid those deprecated warnings. Here's a helper that does just that. | ||
| 19 | # Works identically to pkg_check_modules, except that no checks are needed prior to use. | ||
| 20 | macro (libfind_pkg_check_modules PREFIX PKGNAME) | ||
| 21 | if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4) | ||
| 22 | include(UsePkgConfig) | ||
| 23 | pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS) | ||
| 24 | else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4) | ||
| 25 | find_package(PkgConfig) | ||
| 26 | if (PKG_CONFIG_FOUND) | ||
| 27 | pkg_check_modules(${PREFIX} ${PKGNAME}) | ||
| 28 | endif (PKG_CONFIG_FOUND) | ||
| 29 | endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4) | ||
| 30 | endmacro (libfind_pkg_check_modules) | ||
| 31 | |||
| 32 | # Do the final processing once the paths have been detected. | ||
| 33 | # If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain | ||
| 34 | # all the variables, each of which contain one include directory. | ||
| 35 | # Ditto for ${PREFIX}_PROCESS_LIBS and library files. | ||
| 36 | # Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES. | ||
| 37 | # Also handles errors in case library detection was required, etc. | ||
| 38 | macro (libfind_process PREFIX) | ||
| 39 | # Skip processing if already processed during this run | ||
| 40 | if (NOT ${PREFIX}_FOUND) | ||
| 41 | # Start with the assumption that the library was found | ||
| 42 | set (${PREFIX}_FOUND TRUE) | ||
| 43 | |||
| 44 | # Process all includes and set _FOUND to false if any are missing | ||
| 45 | foreach (i ${${PREFIX}_PROCESS_INCLUDES}) | ||
| 46 | if (${i}) | ||
| 47 | set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}}) | ||
| 48 | mark_as_advanced(${i}) | ||
| 49 | else (${i}) | ||
| 50 | set (${PREFIX}_FOUND FALSE) | ||
| 51 | endif (${i}) | ||
| 52 | endforeach (i) | ||
| 53 | |||
| 54 | # Process all libraries and set _FOUND to false if any are missing | ||
| 55 | foreach (i ${${PREFIX}_PROCESS_LIBS}) | ||
| 56 | if (${i}) | ||
| 57 | set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}}) | ||
| 58 | mark_as_advanced(${i}) | ||
| 59 | else (${i}) | ||
| 60 | set (${PREFIX}_FOUND FALSE) | ||
| 61 | endif (${i}) | ||
| 62 | endforeach (i) | ||
| 63 | |||
| 64 | # Print message and/or exit on fatal error | ||
| 65 | if (${PREFIX}_FOUND) | ||
| 66 | if (NOT ${PREFIX}_FIND_QUIETLY) | ||
| 67 | message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}") | ||
| 68 | endif (NOT ${PREFIX}_FIND_QUIETLY) | ||
| 69 | else (${PREFIX}_FOUND) | ||
| 70 | if (${PREFIX}_FIND_REQUIRED) | ||
| 71 | foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS}) | ||
| 72 | message("${i}=${${i}}") | ||
| 73 | endforeach (i) | ||
| 74 | message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.") | ||
| 75 | endif (${PREFIX}_FIND_REQUIRED) | ||
| 76 | endif (${PREFIX}_FOUND) | ||
| 77 | endif (NOT ${PREFIX}_FOUND) | ||
| 78 | endmacro (libfind_process) | ||
| 79 | |||
| 80 | macro(libfind_library PREFIX basename) | ||
| 81 | set(TMP "") | ||
| 82 | if(MSVC80) | ||
| 83 | set(TMP -vc80) | ||
| 84 | endif(MSVC80) | ||
| 85 | if(MSVC90) | ||
| 86 | set(TMP -vc90) | ||
| 87 | endif(MSVC90) | ||
| 88 | set(${PREFIX}_LIBNAMES ${basename}${TMP}) | ||
| 89 | if(${ARGC} GREATER 2) | ||
| 90 | set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2}) | ||
| 91 | string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES}) | ||
| 92 | set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP}) | ||
| 93 | endif(${ARGC} GREATER 2) | ||
| 94 | find_library(${PREFIX}_LIBRARY | ||
| 95 | NAMES ${${PREFIX}_LIBNAMES} | ||
| 96 | PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS} | ||
| 97 | ) | ||
| 98 | endmacro(libfind_library) | ||
| 99 | |||
diff --git a/usbmuxd/client.c b/usbmuxd/client.c new file mode 100644 index 0000000..b33d892 --- /dev/null +++ b/usbmuxd/client.c | |||
| @@ -0,0 +1,430 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdlib.h> | ||
| 26 | #include <errno.h> | ||
| 27 | #include <unistd.h> | ||
| 28 | #include <sys/types.h> | ||
| 29 | #include <sys/socket.h> | ||
| 30 | #include <sys/un.h> | ||
| 31 | #include <arpa/inet.h> | ||
| 32 | |||
| 33 | #include "log.h" | ||
| 34 | #include "usb.h" | ||
| 35 | #include "client.h" | ||
| 36 | #include "device.h" | ||
| 37 | |||
| 38 | #define CMD_BUF_SIZE 256 | ||
| 39 | #define REPLY_BUF_SIZE 1024 | ||
| 40 | |||
| 41 | enum client_state { | ||
| 42 | CLIENT_COMMAND, // waiting for command | ||
| 43 | CLIENT_LISTEN, // listening for devices | ||
| 44 | CLIENT_CONNECTING1, // issued connection request | ||
| 45 | CLIENT_CONNECTING2, // connection established, but waiting for response message to get sent | ||
| 46 | CLIENT_CONNECTED, // connected | ||
| 47 | CLIENT_DEAD | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct mux_client { | ||
| 51 | int fd; | ||
| 52 | unsigned char *ob_buf; | ||
| 53 | int ob_size; | ||
| 54 | int ob_capacity; | ||
| 55 | unsigned char *ib_buf; | ||
| 56 | int ib_size; | ||
| 57 | int ib_capacity; | ||
| 58 | short events, devents; | ||
| 59 | uint32_t connect_tag; | ||
| 60 | int connect_device; | ||
| 61 | enum client_state state; | ||
| 62 | }; | ||
| 63 | |||
| 64 | static struct collection client_list; | ||
| 65 | |||
| 66 | int client_read(struct mux_client *client, void *buffer, int len) | ||
| 67 | { | ||
| 68 | usbmuxd_log(LL_SPEW, "client_read fd %d buf %p len %d", client->fd, buffer, len); | ||
| 69 | if(client->state != CLIENT_CONNECTED) { | ||
| 70 | usbmuxd_log(LL_ERROR, "Attempted to read from client %d not in CONNECTED state", client->fd); | ||
| 71 | return -1; | ||
| 72 | } | ||
| 73 | return recv(client->fd, buffer, len, 0); | ||
| 74 | } | ||
| 75 | |||
| 76 | int client_write(struct mux_client *client, void *buffer, int len) | ||
| 77 | { | ||
| 78 | usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); | ||
| 79 | if(client->state != CLIENT_CONNECTED) { | ||
| 80 | usbmuxd_log(LL_ERROR, "Attempted to write to client %d not in CONNECTED state", client->fd); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | return send(client->fd, buffer, len, 0); | ||
| 84 | } | ||
| 85 | |||
| 86 | int client_set_events(struct mux_client *client, short events) | ||
| 87 | { | ||
| 88 | if((client->state != CLIENT_CONNECTED) && (client->state != CLIENT_CONNECTING2)) { | ||
| 89 | usbmuxd_log(LL_ERROR, "client_set_events to client %d not in CONNECTED state", client->fd); | ||
| 90 | return -1; | ||
| 91 | } | ||
| 92 | client->devents = events; | ||
| 93 | if(client->state == CLIENT_CONNECTED) | ||
| 94 | client->events = events; | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | int client_accept(int listenfd) | ||
| 99 | { | ||
| 100 | struct sockaddr_un addr; | ||
| 101 | int cfd; | ||
| 102 | socklen_t len = sizeof(struct sockaddr_un); | ||
| 103 | cfd = accept(listenfd, (struct sockaddr *)&addr, &len); | ||
| 104 | if (cfd < 0) { | ||
| 105 | usbmuxd_log(LL_ERROR, "accept() failed (%s)", strerror(errno)); | ||
| 106 | return cfd; | ||
| 107 | } | ||
| 108 | |||
| 109 | struct mux_client *client; | ||
| 110 | client = malloc(sizeof(struct mux_client)); | ||
| 111 | memset(client, 0, sizeof(struct mux_client)); | ||
| 112 | |||
| 113 | client->fd = cfd; | ||
| 114 | client->ob_buf = malloc(REPLY_BUF_SIZE); | ||
| 115 | client->ob_size = 0; | ||
| 116 | client->ob_capacity = REPLY_BUF_SIZE; | ||
| 117 | client->ib_buf = malloc(CMD_BUF_SIZE); | ||
| 118 | client->ib_size = 0; | ||
| 119 | client->ib_capacity = CMD_BUF_SIZE; | ||
| 120 | client->state = CLIENT_COMMAND; | ||
| 121 | client->events = POLLIN; | ||
| 122 | |||
| 123 | collection_add(&client_list, client); | ||
| 124 | |||
| 125 | usbmuxd_log(LL_INFO, "New client on fd %d", client->fd); | ||
| 126 | return client->fd; | ||
| 127 | } | ||
| 128 | |||
| 129 | void client_close(struct mux_client *client) | ||
| 130 | { | ||
| 131 | usbmuxd_log(LL_INFO, "Disconnecting client fd %d", client->fd); | ||
| 132 | if(client->state == CLIENT_CONNECTING1 || client->state == CLIENT_CONNECTING2) { | ||
| 133 | usbmuxd_log(LL_INFO, "Client died mid-connect, aborting device %d connection", client->connect_device); | ||
| 134 | client->state = CLIENT_DEAD; | ||
| 135 | device_abort_connect(client->connect_device, client); | ||
| 136 | } | ||
| 137 | close(client->fd); | ||
| 138 | if(client->ob_buf) | ||
| 139 | free(client->ob_buf); | ||
| 140 | if(client->ib_buf) | ||
| 141 | free(client->ib_buf); | ||
| 142 | collection_remove(&client_list, client); | ||
| 143 | free(client); | ||
| 144 | } | ||
| 145 | |||
| 146 | void client_get_fds(struct fdlist *list) | ||
| 147 | { | ||
| 148 | FOREACH(struct mux_client *client, &client_list) { | ||
| 149 | fdlist_add(list, FD_CLIENT, client->fd, client->events); | ||
| 150 | } ENDFOREACH | ||
| 151 | } | ||
| 152 | |||
| 153 | static int send_pkt(struct mux_client *client, uint32_t tag, enum client_msgtype msg, void *payload, int payload_length) | ||
| 154 | { | ||
| 155 | struct client_header hdr; | ||
| 156 | hdr.version = CLIENT_PROTOCOL_VERSION; | ||
| 157 | hdr.length = sizeof(hdr) + payload_length; | ||
| 158 | hdr.message = msg; | ||
| 159 | hdr.tag = tag; | ||
| 160 | usbmuxd_log(LL_DEBUG, "send_pkt fd %d tag %d msg %d payload_length %d", client->fd, tag, msg, payload_length); | ||
| 161 | if((client->ob_capacity - client->ob_size) < hdr.length) { | ||
| 162 | usbmuxd_log(LL_ERROR, "Client %d output buffer full (%d bytes) while sending message %d (%d bytes)", client->fd, client->ob_capacity, hdr.message, hdr.length); | ||
| 163 | client_close(client); | ||
| 164 | return -1; | ||
| 165 | } | ||
| 166 | memcpy(client->ob_buf + client->ob_size, &hdr, sizeof(hdr)); | ||
| 167 | if(payload && payload_length) | ||
| 168 | memcpy(client->ob_buf + client->ob_size + sizeof(hdr), payload, payload_length); | ||
| 169 | client->ob_size += hdr.length; | ||
| 170 | client->events |= POLLOUT; | ||
| 171 | return hdr.length; | ||
| 172 | } | ||
| 173 | |||
| 174 | static int send_result(struct mux_client *client, uint32_t tag, uint32_t result) | ||
| 175 | { | ||
| 176 | return send_pkt(client, tag, MESSAGE_RESULT, &result, sizeof(uint32_t)); | ||
| 177 | } | ||
| 178 | |||
| 179 | int client_notify_connect(struct mux_client *client, enum client_result result) | ||
| 180 | { | ||
| 181 | usbmuxd_log(LL_SPEW, "client_notify_connect fd %d result %d", client->fd, result); | ||
| 182 | if(client->state == CLIENT_DEAD) | ||
| 183 | return -1; | ||
| 184 | if(client->state != CLIENT_CONNECTING1) { | ||
| 185 | usbmuxd_log(LL_ERROR, "client_notify_connect when client %d is not in CONNECTING1 state", client->fd); | ||
| 186 | return -1; | ||
| 187 | } | ||
| 188 | if(send_result(client, client->connect_tag, result) < 0) | ||
| 189 | return -1; | ||
| 190 | if(result == RESULT_OK) { | ||
| 191 | client->state = CLIENT_CONNECTING2; | ||
| 192 | client->events = POLLOUT; // wait for the result packet to go through | ||
| 193 | // no longer need this | ||
| 194 | free(client->ib_buf); | ||
| 195 | client->ib_buf = NULL; | ||
| 196 | } else { | ||
| 197 | client->state = CLIENT_COMMAND; | ||
| 198 | } | ||
| 199 | return 0; | ||
| 200 | } | ||
| 201 | |||
| 202 | static int notify_device(struct mux_client *client, struct device_info *dev) | ||
| 203 | { | ||
| 204 | struct client_msg_dev dmsg; | ||
| 205 | memset(&dmsg, 0, sizeof(dmsg)); | ||
| 206 | dmsg.device_id = dev->id; | ||
| 207 | strncpy(dmsg.device_serial, dev->serial, 256); | ||
| 208 | dmsg.device_serial[255] = 0; | ||
| 209 | dmsg.location = dev->location; | ||
| 210 | dmsg.device_pid = dev->pid; | ||
| 211 | return send_pkt(client, 0, MESSAGE_DEVICE_ADD, &dmsg, sizeof(dmsg)); | ||
| 212 | } | ||
| 213 | |||
| 214 | static int start_listen(struct mux_client *client) | ||
| 215 | { | ||
| 216 | struct device_info *devs; | ||
| 217 | struct device_info *dev; | ||
| 218 | int count, i; | ||
| 219 | |||
| 220 | client->state = CLIENT_LISTEN; | ||
| 221 | count = device_get_count(); | ||
| 222 | if(!count) | ||
| 223 | return 0; | ||
| 224 | devs = malloc(sizeof(struct device_info) * count); | ||
| 225 | count = device_get_list(devs); | ||
| 226 | |||
| 227 | // going to need a larger buffer for many devices | ||
| 228 | int needed_buffer = count * (sizeof(struct client_msg_dev) + sizeof(struct client_header)) + REPLY_BUF_SIZE; | ||
| 229 | if(client->ob_capacity < needed_buffer) { | ||
| 230 | usbmuxd_log(LL_DEBUG, "Enlarging client %d reply buffer %d -> %d to make space for device notifications", client->fd, client->ob_capacity, needed_buffer); | ||
| 231 | client->ob_buf = realloc(client->ob_buf, needed_buffer); | ||
| 232 | client->ob_capacity = needed_buffer; | ||
| 233 | } | ||
| 234 | dev = devs; | ||
| 235 | for(i=0; i<count; i++) { | ||
| 236 | if(notify_device(client, dev++) < 0) { | ||
| 237 | free(devs); | ||
| 238 | return -1; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | free(devs); | ||
| 242 | return count; | ||
| 243 | } | ||
| 244 | |||
| 245 | static int client_command(struct mux_client *client, struct client_header *hdr, const char *payload) | ||
| 246 | { | ||
| 247 | int res; | ||
| 248 | usbmuxd_log(LL_DEBUG, "Client command in fd %d len %d ver %d msg %d tag %d", client->fd, hdr->length, hdr->version, hdr->message, hdr->tag); | ||
| 249 | |||
| 250 | if(client->state != CLIENT_COMMAND) { | ||
| 251 | usbmuxd_log(LL_ERROR, "Client %d command received in the wrong state", client->fd); | ||
| 252 | if(send_result(client, hdr->tag, RESULT_BADCOMMAND) < 0) | ||
| 253 | return -1; | ||
| 254 | client_close(client); | ||
| 255 | return -1; | ||
| 256 | } | ||
| 257 | |||
| 258 | struct client_msg_connect *ch; | ||
| 259 | switch(hdr->message) { | ||
| 260 | case MESSAGE_LISTEN: | ||
| 261 | if(send_result(client, hdr->tag, 0) < 0) | ||
| 262 | return -1; | ||
| 263 | usbmuxd_log(LL_DEBUG, "Client %d now LISTENING", client->fd); | ||
| 264 | return start_listen(client); | ||
| 265 | case MESSAGE_CONNECT: | ||
| 266 | ch = (void*)payload; | ||
| 267 | usbmuxd_log(LL_DEBUG, "Client %d connection request to device %d port %d", client->fd, ch->device_id, ntohs(ch->port)); | ||
| 268 | res = device_start_connect(ch->device_id, ntohs(ch->port), client); | ||
| 269 | if(res < 0) { | ||
| 270 | if(send_result(client, hdr->tag, -res) < 0) | ||
| 271 | return -1; | ||
| 272 | } else { | ||
| 273 | client->connect_tag = hdr->tag; | ||
| 274 | client->connect_device = ch->device_id; | ||
| 275 | client->state = CLIENT_CONNECTING1; | ||
| 276 | } | ||
| 277 | return 0; | ||
| 278 | default: | ||
| 279 | usbmuxd_log(LL_ERROR, "Client %d invalid command %d", client->fd, hdr->message); | ||
| 280 | if(send_result(client, hdr->tag, RESULT_BADCOMMAND) < 0) | ||
| 281 | return -1; | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | return -1; | ||
| 285 | } | ||
| 286 | |||
| 287 | static void process_send(struct mux_client *client) | ||
| 288 | { | ||
| 289 | int res; | ||
| 290 | if(!client->ob_size) { | ||
| 291 | usbmuxd_log(LL_WARNING, "Client %d OUT process but nothing to send?", client->fd); | ||
| 292 | client->events &= ~POLLOUT; | ||
| 293 | return; | ||
| 294 | } | ||
| 295 | res = send(client->fd, client->ob_buf, client->ob_size, 0); | ||
| 296 | if(res <= 0) { | ||
| 297 | usbmuxd_log(LL_ERROR, "Send to client fd %d failed: %d %s", client->fd, res, strerror(errno)); | ||
| 298 | client_close(client); | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | if(res == client->ob_size) { | ||
| 302 | client->ob_size = 0; | ||
| 303 | client->events &= ~POLLOUT; | ||
| 304 | if(client->state == CLIENT_CONNECTING2) { | ||
| 305 | usbmuxd_log(LL_DEBUG, "Client %d switching to CONNECTED state", client->fd); | ||
| 306 | client->state = CLIENT_CONNECTED; | ||
| 307 | client->events = client->devents; | ||
| 308 | // no longer need this | ||
| 309 | free(client->ob_buf); | ||
| 310 | client->ob_buf = NULL; | ||
| 311 | } | ||
| 312 | } else { | ||
| 313 | client->ob_size -= res; | ||
| 314 | memmove(client->ob_buf, client->ob_buf + res, client->ob_size); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | static void process_recv(struct mux_client *client) | ||
| 318 | { | ||
| 319 | int res; | ||
| 320 | int did_read = 0; | ||
| 321 | if(client->ib_size < sizeof(struct client_header)) { | ||
| 322 | res = recv(client->fd, client->ib_buf + client->ib_size, sizeof(struct client_header) - client->ib_size, 0); | ||
| 323 | if(res <= 0) { | ||
| 324 | if(res < 0) | ||
| 325 | usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno)); | ||
| 326 | else | ||
| 327 | usbmuxd_log(LL_INFO, "Client %d connection closed", client->fd); | ||
| 328 | client_close(client); | ||
| 329 | return; | ||
| 330 | } | ||
| 331 | client->ib_size += res; | ||
| 332 | if(client->ib_size < sizeof(struct client_header)) | ||
| 333 | return; | ||
| 334 | did_read = 1; | ||
| 335 | } | ||
| 336 | struct client_header *hdr = (void*)client->ib_buf; | ||
| 337 | if(hdr->version != CLIENT_PROTOCOL_VERSION) { | ||
| 338 | usbmuxd_log(LL_INFO, "Client %d version mismatch: expected %d, got %d", client->fd, CLIENT_PROTOCOL_VERSION, hdr->version); | ||
| 339 | client_close(client); | ||
| 340 | } | ||
| 341 | if(hdr->length > client->ib_capacity) { | ||
| 342 | usbmuxd_log(LL_INFO, "Client %d message is too long (%d bytes)", client->fd, hdr->length); | ||
| 343 | client_close(client); | ||
| 344 | } | ||
| 345 | if(hdr->length < sizeof(struct client_header)) { | ||
| 346 | usbmuxd_log(LL_ERROR, "Client %d message is too short (%d bytes)", client->fd, hdr->length); | ||
| 347 | client_close(client); | ||
| 348 | } | ||
| 349 | if(client->ib_size < hdr->length) { | ||
| 350 | if(did_read) | ||
| 351 | return; //maybe we would block, so defer to next loop | ||
| 352 | res = recv(client->fd, client->ib_buf + client->ib_size, hdr->length - client->ib_size, 0); | ||
| 353 | if(res < 0) { | ||
| 354 | usbmuxd_log(LL_ERROR, "Receive from client fd %d failed: %s", client->fd, strerror(errno)); | ||
| 355 | client_close(client); | ||
| 356 | return; | ||
| 357 | } else if(res == 0) { | ||
| 358 | usbmuxd_log(LL_INFO, "Client %d connection closed", client->fd); | ||
| 359 | client_close(client); | ||
| 360 | return; | ||
| 361 | } | ||
| 362 | client->ib_size += res; | ||
| 363 | if(client->ib_size < hdr->length) | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | client_command(client, hdr, (char *)(hdr+1)); | ||
| 367 | client->ib_size = 0; | ||
| 368 | } | ||
| 369 | |||
| 370 | void client_process(int fd, short events) | ||
| 371 | { | ||
| 372 | struct mux_client *client = NULL; | ||
| 373 | FOREACH(struct mux_client *lc, &client_list) { | ||
| 374 | if(lc->fd == fd) { | ||
| 375 | client = lc; | ||
| 376 | break; | ||
| 377 | } | ||
| 378 | } ENDFOREACH | ||
| 379 | |||
| 380 | if(!client) { | ||
| 381 | usbmuxd_log(LL_ERROR, "client_process: fd %d not found in client list", fd); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | if(client->state == CLIENT_CONNECTED) { | ||
| 386 | usbmuxd_log(LL_SPEW, "client_process in CONNECTED state"); | ||
| 387 | device_client_process(client->connect_device, client, events); | ||
| 388 | } else { | ||
| 389 | if(events & POLLIN) { | ||
| 390 | process_recv(client); | ||
| 391 | } else if(events & POLLOUT) { //not both in case client died as part of process_recv | ||
| 392 | process_send(client); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | void client_device_add(struct device_info *dev) | ||
| 399 | { | ||
| 400 | usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); | ||
| 401 | FOREACH(struct mux_client *client, &client_list) { | ||
| 402 | if(client->state == CLIENT_LISTEN) | ||
| 403 | notify_device(client, dev); | ||
| 404 | } ENDFOREACH | ||
| 405 | } | ||
| 406 | void client_device_remove(int device_id) | ||
| 407 | { | ||
| 408 | uint32_t id = device_id; | ||
| 409 | usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); | ||
| 410 | FOREACH(struct mux_client *client, &client_list) { | ||
| 411 | if(client->state == CLIENT_LISTEN) | ||
| 412 | send_pkt(client, 0, MESSAGE_DEVICE_REMOVE, &id, sizeof(uint32_t)); | ||
| 413 | } ENDFOREACH | ||
| 414 | } | ||
| 415 | |||
| 416 | |||
| 417 | void client_init(void) | ||
| 418 | { | ||
| 419 | usbmuxd_log(LL_DEBUG, "client_init"); | ||
| 420 | collection_init(&client_list); | ||
| 421 | } | ||
| 422 | |||
| 423 | void client_shutdown(void) | ||
| 424 | { | ||
| 425 | usbmuxd_log(LL_DEBUG, "client_shutdown"); | ||
| 426 | FOREACH(struct mux_client *client, &client_list) { | ||
| 427 | client_close(client); | ||
| 428 | } ENDFOREACH | ||
| 429 | collection_free(&client_list); | ||
| 430 | } | ||
diff --git a/usbmuxd/client.h b/usbmuxd/client.h new file mode 100644 index 0000000..0cda676 --- /dev/null +++ b/usbmuxd/client.h | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __CLIENT_H__ | ||
| 22 | #define __CLIENT_H__ | ||
| 23 | |||
| 24 | #include <stdint.h> | ||
| 25 | |||
| 26 | struct device_info; | ||
| 27 | struct mux_client; | ||
| 28 | |||
| 29 | enum client_result { | ||
| 30 | RESULT_OK = 0, | ||
| 31 | RESULT_BADCOMMAND = 1, | ||
| 32 | RESULT_BADDEV = 2, | ||
| 33 | RESULT_CONNREFUSED = 3, | ||
| 34 | // ??? | ||
| 35 | // ??? | ||
| 36 | RESULT_BADVERSION = 6, | ||
| 37 | }; | ||
| 38 | |||
| 39 | enum client_msgtype { | ||
| 40 | MESSAGE_RESULT = 1, | ||
| 41 | MESSAGE_CONNECT = 2, | ||
| 42 | MESSAGE_LISTEN = 3, | ||
| 43 | MESSAGE_DEVICE_ADD = 4, | ||
| 44 | MESSAGE_DEVICE_REMOVE = 5, | ||
| 45 | //??? | ||
| 46 | //??? | ||
| 47 | //MESSAGE_PLIST = 8, | ||
| 48 | }; | ||
| 49 | |||
| 50 | #define CLIENT_PROTOCOL_VERSION 0 | ||
| 51 | |||
| 52 | struct client_header { | ||
| 53 | uint32_t length; | ||
| 54 | uint32_t version; | ||
| 55 | uint32_t message; | ||
| 56 | uint32_t tag; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct client_msg_result { | ||
| 60 | uint32_t result; | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct client_msg_connect { | ||
| 64 | uint32_t device_id; | ||
| 65 | uint16_t port; | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct client_msg_dev { | ||
| 69 | uint32_t device_id; | ||
| 70 | uint16_t device_pid; | ||
| 71 | char device_serial[256]; | ||
| 72 | uint16_t padding; | ||
| 73 | uint32_t location; | ||
| 74 | }; | ||
| 75 | |||
| 76 | int client_read(struct mux_client *client, void *buffer, int len); | ||
| 77 | int client_write(struct mux_client *client, void *buffer, int len); | ||
| 78 | int client_set_events(struct mux_client *client, short events); | ||
| 79 | void client_close(struct mux_client *client); | ||
| 80 | int client_notify_connect(struct mux_client *client, enum client_result result); | ||
| 81 | |||
| 82 | void client_device_add(struct device_info *dev); | ||
| 83 | void client_device_remove(int device_id); | ||
| 84 | |||
| 85 | int client_accept(int fd); | ||
| 86 | void client_get_fds(struct fdlist *list); | ||
| 87 | void client_process(int fd, short events); | ||
| 88 | |||
| 89 | void client_init(void); | ||
| 90 | void client_shutdown(void); | ||
| 91 | |||
| 92 | #endif | ||
diff --git a/usbmuxd/device.c b/usbmuxd/device.c new file mode 100644 index 0000000..c6ef75b --- /dev/null +++ b/usbmuxd/device.c | |||
| @@ -0,0 +1,731 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #define _BSD_SOURCE | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <sys/time.h> | ||
| 28 | #include <netinet/in.h> | ||
| 29 | #include <netinet/tcp.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <string.h> | ||
| 32 | #include <stdint.h> | ||
| 33 | #include <inttypes.h> | ||
| 34 | #include "device.h" | ||
| 35 | #include "client.h" | ||
| 36 | #include "usb.h" | ||
| 37 | #include "log.h" | ||
| 38 | |||
| 39 | int next_device_id; | ||
| 40 | |||
| 41 | #define DEV_PKTBUF_SIZE 65536 | ||
| 42 | |||
| 43 | #define CONN_INBUF_SIZE 262144 | ||
| 44 | #define CONN_OUTBUF_SIZE 65536 | ||
| 45 | |||
| 46 | #define ACK_TIMEOUT 60 | ||
| 47 | |||
| 48 | enum mux_protocol { | ||
| 49 | MUX_PROTO_VERSION = 0, | ||
| 50 | MUX_PROTO_TCP = IPPROTO_TCP, | ||
| 51 | }; | ||
| 52 | |||
| 53 | enum mux_dev_state { | ||
| 54 | MUXDEV_INIT, // sent version packet | ||
| 55 | MUXDEV_ACTIVE, // received version packet, active | ||
| 56 | MUXDEV_DEAD // dead | ||
| 57 | }; | ||
| 58 | |||
| 59 | enum mux_conn_state { | ||
| 60 | CONN_CONNECTING, // SYN | ||
| 61 | CONN_CONNECTED, // SYN/SYNACK/ACK -> active | ||
| 62 | CONN_REFUSED, // RST received during SYN | ||
| 63 | CONN_DYING, // RST received | ||
| 64 | CONN_DEAD // being freed; used to prevent infinite recursion between client<->device freeing | ||
| 65 | }; | ||
| 66 | |||
| 67 | struct mux_header | ||
| 68 | { | ||
| 69 | uint32_t protocol; | ||
| 70 | uint32_t length; | ||
| 71 | }; | ||
| 72 | |||
| 73 | struct version_header | ||
| 74 | { | ||
| 75 | uint32_t major; | ||
| 76 | uint32_t minor; | ||
| 77 | uint32_t padding; | ||
| 78 | }; | ||
| 79 | |||
| 80 | struct mux_device; | ||
| 81 | |||
| 82 | #define CONN_ACK_PENDING 1 | ||
| 83 | |||
| 84 | struct mux_connection | ||
| 85 | { | ||
| 86 | struct mux_device *dev; | ||
| 87 | struct mux_client *client; | ||
| 88 | enum mux_conn_state state; | ||
| 89 | uint16_t sport, dport; | ||
| 90 | uint32_t tx_seq, tx_ack, tx_acked, tx_win; | ||
| 91 | uint32_t rx_seq, rx_recvd, rx_ack, rx_win; | ||
| 92 | int max_payload; | ||
| 93 | int sendable; | ||
| 94 | int flags; | ||
| 95 | unsigned char *ib_buf; | ||
| 96 | int ib_size; | ||
| 97 | int ib_capacity; | ||
| 98 | unsigned char *ob_buf; | ||
| 99 | int ob_capacity; | ||
| 100 | short events; | ||
| 101 | uint64_t last_ack_time; | ||
| 102 | }; | ||
| 103 | |||
| 104 | struct mux_device | ||
| 105 | { | ||
| 106 | struct usb_device *usbdev; | ||
| 107 | int id; | ||
| 108 | enum mux_dev_state state; | ||
| 109 | struct collection connections; | ||
| 110 | uint16_t next_sport; | ||
| 111 | unsigned char *pktbuf; | ||
| 112 | int pktlen; | ||
| 113 | }; | ||
| 114 | |||
| 115 | static struct collection device_list; | ||
| 116 | |||
| 117 | uint64_t mstime64(void) | ||
| 118 | { | ||
| 119 | struct timeval tv; | ||
| 120 | gettimeofday(&tv, NULL); | ||
| 121 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; | ||
| 122 | } | ||
| 123 | |||
| 124 | static int get_next_device_id(void) | ||
| 125 | { | ||
| 126 | while(1) { | ||
| 127 | int ok = 1; | ||
| 128 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 129 | if(dev->id == next_device_id) { | ||
| 130 | next_device_id++; | ||
| 131 | ok = 0; | ||
| 132 | break; | ||
| 133 | } | ||
| 134 | } ENDFOREACH | ||
| 135 | if(ok) | ||
| 136 | return next_device_id++; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | static int send_packet(struct mux_device *dev, enum mux_protocol proto, void *header, const void *data, int length) | ||
| 141 | { | ||
| 142 | unsigned char *buffer; | ||
| 143 | int hdrlen; | ||
| 144 | int res; | ||
| 145 | |||
| 146 | switch(proto) { | ||
| 147 | case MUX_PROTO_VERSION: | ||
| 148 | hdrlen = sizeof(struct version_header); | ||
| 149 | break; | ||
| 150 | case MUX_PROTO_TCP: | ||
| 151 | hdrlen = sizeof(struct tcphdr); | ||
| 152 | break; | ||
| 153 | default: | ||
| 154 | usbmuxd_log(LL_ERROR, "Invalid protocol %d for outgoing packet (dev %d hdr %p data %p len %d)", proto, dev->id, header, data, length); | ||
| 155 | return -1; | ||
| 156 | } | ||
| 157 | usbmuxd_log(LL_SPEW, "send_packet(%d, 0x%x, %p, %p, %d)", dev->id, proto, header, data, length); | ||
| 158 | |||
| 159 | int total = sizeof(struct mux_header) + hdrlen + length; | ||
| 160 | |||
| 161 | if(total > USB_MTU) { | ||
| 162 | usbmuxd_log(LL_ERROR, "Tried to send packet larger than USB MTU (hdr %d data %d total %d) to device %d", hdrlen, length, total, dev->id); | ||
| 163 | return -1; | ||
| 164 | } | ||
| 165 | |||
| 166 | buffer = malloc(total); | ||
| 167 | struct mux_header *mhdr = (struct mux_header *)buffer; | ||
| 168 | mhdr->protocol = htonl(proto); | ||
| 169 | mhdr->length = htonl(total);; | ||
| 170 | memcpy(buffer + sizeof(struct mux_header), header, hdrlen); | ||
| 171 | if(data && length) | ||
| 172 | memcpy(buffer + sizeof(struct mux_header) + hdrlen, data, length); | ||
| 173 | |||
| 174 | if((res = usb_send(dev->usbdev, buffer, total)) < 0) { | ||
| 175 | usbmuxd_log(LL_ERROR, "usb_send failed while sending packet (len %d) to device %d: %d", total, dev->id, res); | ||
| 176 | free(buffer); | ||
| 177 | return res; | ||
| 178 | } | ||
| 179 | return total; | ||
| 180 | } | ||
| 181 | |||
| 182 | static uint16_t find_sport(struct mux_device *dev) | ||
| 183 | { | ||
| 184 | if(collection_count(&dev->connections) >= 65535) | ||
| 185 | return 0; //insanity | ||
| 186 | |||
| 187 | while(1) { | ||
| 188 | int ok = 1; | ||
| 189 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 190 | if(dev->next_sport == conn->sport) { | ||
| 191 | dev->next_sport++; | ||
| 192 | ok = 0; | ||
| 193 | break; | ||
| 194 | } | ||
| 195 | } ENDFOREACH | ||
| 196 | if(ok) | ||
| 197 | return dev->next_sport++; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | static int send_tcp(struct mux_connection *conn, uint8_t flags, const unsigned char *data, int length) | ||
| 202 | { | ||
| 203 | struct tcphdr th; | ||
| 204 | memset(&th, 0, sizeof(th)); | ||
| 205 | th.th_sport = htons(conn->sport); | ||
| 206 | th.th_dport = htons(conn->dport); | ||
| 207 | th.th_seq = htonl(conn->tx_seq); | ||
| 208 | th.th_ack = htonl(conn->tx_ack); | ||
| 209 | th.th_flags = flags; | ||
| 210 | th.th_off = sizeof(th) / 4; | ||
| 211 | th.th_win = htons(conn->tx_win >> 8); | ||
| 212 | |||
| 213 | usbmuxd_log(LL_DEBUG, "[OUT] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", | ||
| 214 | conn->dev->id, conn->sport, conn->dport, conn->tx_seq, conn->tx_ack, flags, conn->tx_win, conn->tx_win >> 8, length); | ||
| 215 | |||
| 216 | int res = send_packet(conn->dev, MUX_PROTO_TCP, &th, data, length); | ||
| 217 | if(res >= 0) { | ||
| 218 | conn->tx_acked = conn->tx_ack; | ||
| 219 | conn->last_ack_time = mstime64(); | ||
| 220 | conn->flags &= ~CONN_ACK_PENDING; | ||
| 221 | } | ||
| 222 | return res; | ||
| 223 | } | ||
| 224 | |||
| 225 | static void connection_teardown(struct mux_connection *conn) | ||
| 226 | { | ||
| 227 | int res; | ||
| 228 | if(conn->state == CONN_DEAD) | ||
| 229 | return; | ||
| 230 | usbmuxd_log(LL_DEBUG, "connection_teardown dev %d sport %d dport %d", conn->dev->id, conn->sport, conn->dport); | ||
| 231 | if(conn->dev->state != MUXDEV_DEAD && conn->state != CONN_DYING && conn->state != CONN_REFUSED) { | ||
| 232 | res = send_tcp(conn, TH_RST, NULL, 0); | ||
| 233 | if(res < 0) | ||
| 234 | usbmuxd_log(LL_ERROR, "Error sending TCP RST to device %d (%d->%d)", conn->dev->id, conn->sport, conn->dport); | ||
| 235 | } | ||
| 236 | if(conn->client) { | ||
| 237 | if(conn->state == CONN_REFUSED || conn->state == CONN_CONNECTING) { | ||
| 238 | client_notify_connect(conn->client, RESULT_CONNREFUSED); | ||
| 239 | } else { | ||
| 240 | conn->state = CONN_DEAD; | ||
| 241 | client_close(conn->client); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | if(conn->ib_buf) | ||
| 245 | free(conn->ib_buf); | ||
| 246 | if(conn->ob_buf) | ||
| 247 | free(conn->ob_buf); | ||
| 248 | collection_remove(&conn->dev->connections, conn); | ||
| 249 | free(conn); | ||
| 250 | } | ||
| 251 | |||
| 252 | int device_start_connect(int device_id, uint16_t dport, struct mux_client *client) | ||
| 253 | { | ||
| 254 | struct mux_device *dev = NULL; | ||
| 255 | FOREACH(struct mux_device *cdev, &device_list) { | ||
| 256 | if(cdev->id == device_id) { | ||
| 257 | dev = cdev; | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | } ENDFOREACH | ||
| 261 | if(!dev) { | ||
| 262 | usbmuxd_log(LL_WARNING, "Attempted to connect to nonexistent device %d", device_id); | ||
| 263 | return -RESULT_BADDEV; | ||
| 264 | } | ||
| 265 | |||
| 266 | uint16_t sport = find_sport(dev); | ||
| 267 | if(!sport) { | ||
| 268 | usbmuxd_log(LL_WARNING, "Unable to allocate port for device %d", device_id); | ||
| 269 | return -RESULT_BADDEV; | ||
| 270 | } | ||
| 271 | |||
| 272 | struct mux_connection *conn; | ||
| 273 | conn = malloc(sizeof(struct mux_connection)); | ||
| 274 | memset(conn, 0, sizeof(struct mux_connection)); | ||
| 275 | |||
| 276 | conn->dev = dev; | ||
| 277 | conn->client = client; | ||
| 278 | conn->state = CONN_CONNECTING; | ||
| 279 | conn->sport = sport; | ||
| 280 | conn->dport = dport; | ||
| 281 | conn->tx_seq = 0; | ||
| 282 | conn->tx_ack = 0; | ||
| 283 | conn->tx_acked = 0; | ||
| 284 | conn->tx_win = 131072; | ||
| 285 | conn->rx_recvd = 0; | ||
| 286 | conn->flags = 0; | ||
| 287 | conn->max_payload = USB_MTU - sizeof(struct mux_header) - sizeof(struct tcphdr); | ||
| 288 | |||
| 289 | conn->ob_buf = malloc(CONN_OUTBUF_SIZE); | ||
| 290 | conn->ob_capacity = CONN_OUTBUF_SIZE; | ||
| 291 | conn->ib_buf = malloc(CONN_INBUF_SIZE); | ||
| 292 | conn->ib_capacity = CONN_INBUF_SIZE; | ||
| 293 | conn->ib_size = 0; | ||
| 294 | |||
| 295 | int res; | ||
| 296 | |||
| 297 | res = send_tcp(conn, TH_SYN, NULL, 0); | ||
| 298 | if(res < 0) { | ||
| 299 | usbmuxd_log(LL_ERROR, "Error sending TCP SYN to device %d (%d->%d)", dev->id, sport, dport); | ||
| 300 | free(conn); | ||
| 301 | return -RESULT_CONNREFUSED; //bleh | ||
| 302 | } | ||
| 303 | collection_add(&dev->connections, conn); | ||
| 304 | return 0; | ||
| 305 | } | ||
| 306 | |||
| 307 | static void update_connection(struct mux_connection *conn) | ||
| 308 | { | ||
| 309 | conn->sendable = conn->rx_win - (conn->tx_seq - conn->rx_ack); | ||
| 310 | |||
| 311 | if(conn->sendable > conn->ob_capacity) | ||
| 312 | conn->sendable = conn->ob_capacity; | ||
| 313 | if(conn->sendable > conn->max_payload) | ||
| 314 | conn->sendable = conn->max_payload; | ||
| 315 | |||
| 316 | if(conn->sendable > 0) | ||
| 317 | conn->events |= POLLIN; | ||
| 318 | else | ||
| 319 | conn->events &= ~POLLIN; | ||
| 320 | |||
| 321 | if(conn->ib_size) | ||
| 322 | conn->events |= POLLOUT; | ||
| 323 | else | ||
| 324 | conn->events &= ~POLLOUT; | ||
| 325 | |||
| 326 | if(conn->tx_acked != conn->tx_ack) | ||
| 327 | conn->flags |= CONN_ACK_PENDING; | ||
| 328 | else | ||
| 329 | conn->flags &= ~CONN_ACK_PENDING; | ||
| 330 | |||
| 331 | usbmuxd_log(LL_SPEW, "update_connection: sendable %d, events %d, flags %d", conn->sendable, conn->events, conn->flags); | ||
| 332 | client_set_events(conn->client, conn->events); | ||
| 333 | } | ||
| 334 | |||
| 335 | void device_client_process(int device_id, struct mux_client *client, short events) | ||
| 336 | { | ||
| 337 | struct mux_connection *conn = NULL; | ||
| 338 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 339 | if(dev->id == device_id) { | ||
| 340 | FOREACH(struct mux_connection *lconn, &dev->connections) { | ||
| 341 | if(lconn->client == client) { | ||
| 342 | conn = lconn; | ||
| 343 | break; | ||
| 344 | } | ||
| 345 | } ENDFOREACH | ||
| 346 | break; | ||
| 347 | } | ||
| 348 | } ENDFOREACH | ||
| 349 | |||
| 350 | if(!conn) { | ||
| 351 | usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client); | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | usbmuxd_log(LL_SPEW, "device_client_process (%d)", events); | ||
| 355 | |||
| 356 | int res; | ||
| 357 | int size; | ||
| 358 | if(events & POLLOUT) { | ||
| 359 | size = client_write(conn->client, conn->ib_buf, conn->ib_size); | ||
| 360 | if(size <= 0) { | ||
| 361 | usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size); | ||
| 362 | connection_teardown(conn); | ||
| 363 | return; | ||
| 364 | } | ||
| 365 | conn->tx_ack += size; | ||
| 366 | if(size == conn->ib_size) { | ||
| 367 | conn->ib_size = 0; | ||
| 368 | } else { | ||
| 369 | conn->ib_size -= size; | ||
| 370 | memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | if(events & POLLIN) { | ||
| 374 | size = client_read(conn->client, conn->ob_buf, conn->sendable); | ||
| 375 | if(size <= 0) { | ||
| 376 | usbmuxd_log(LL_DEBUG, "error reading from client (%d)", size); | ||
| 377 | connection_teardown(conn); | ||
| 378 | return; | ||
| 379 | } | ||
| 380 | res = send_tcp(conn, TH_ACK, conn->ob_buf, size); | ||
| 381 | if(res < 0) { | ||
| 382 | connection_teardown(conn); | ||
| 383 | return; | ||
| 384 | } | ||
| 385 | conn->tx_seq += size; | ||
| 386 | } | ||
| 387 | |||
| 388 | update_connection(conn); | ||
| 389 | } | ||
| 390 | |||
| 391 | static void connection_device_input(struct mux_connection *conn, unsigned char *payload, int payload_length) | ||
| 392 | { | ||
| 393 | if((conn->ib_size + payload_length) > conn->ib_capacity) { | ||
| 394 | usbmuxd_log(LL_ERROR, "Input buffer overflow on device %d connection %d->%d (space=%d, payload=%d)", conn->dev->id, conn->sport, conn->dport, conn->ib_capacity-conn->ib_size, payload_length); | ||
| 395 | connection_teardown(conn); | ||
| 396 | return; | ||
| 397 | } | ||
| 398 | memcpy(conn->ib_buf + conn->ib_size, payload, payload_length); | ||
| 399 | conn->ib_size += payload_length; | ||
| 400 | conn->rx_recvd += payload_length; | ||
| 401 | update_connection(conn); | ||
| 402 | } | ||
| 403 | |||
| 404 | void device_abort_connect(int device_id, struct mux_client *client) | ||
| 405 | { | ||
| 406 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 407 | if(dev->id == device_id) { | ||
| 408 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 409 | if(conn->client == client) { | ||
| 410 | connection_teardown(conn); | ||
| 411 | return; | ||
| 412 | } | ||
| 413 | } ENDFOREACH | ||
| 414 | usbmuxd_log(LL_WARNING, "Attempted to abort for nonexistent connection for device %d", device_id); | ||
| 415 | return; | ||
| 416 | } | ||
| 417 | } ENDFOREACH | ||
| 418 | usbmuxd_log(LL_WARNING, "Attempted to abort connection for nonexistent device %d", device_id); | ||
| 419 | } | ||
| 420 | |||
| 421 | static void device_version_input(struct mux_device *dev, struct version_header *vh) | ||
| 422 | { | ||
| 423 | if(dev->state != MUXDEV_INIT) { | ||
| 424 | usbmuxd_log(LL_WARNING, "Version packet from already initialized device %d", dev->id); | ||
| 425 | return; | ||
| 426 | } | ||
| 427 | vh->major = ntohl(vh->major); | ||
| 428 | vh->minor = ntohl(vh->minor); | ||
| 429 | if(vh->major != 1 || vh->minor != 0) { | ||
| 430 | usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d\n", dev->id, vh->major, vh->minor); | ||
| 431 | collection_remove(&device_list, dev); | ||
| 432 | free(dev); | ||
| 433 | return; | ||
| 434 | } | ||
| 435 | usbmuxd_log(LL_NOTICE, "Connected to v%d.%d device %d on location 0x%x with serial number %s", vh->major, vh->minor, dev->id, usb_get_location(dev->usbdev), usb_get_serial(dev->usbdev)); | ||
| 436 | dev->state = MUXDEV_ACTIVE; | ||
| 437 | collection_init(&dev->connections); | ||
| 438 | struct device_info info; | ||
| 439 | info.id = dev->id; | ||
| 440 | info.location = usb_get_location(dev->usbdev); | ||
| 441 | info.serial = usb_get_serial(dev->usbdev); | ||
| 442 | info.pid = usb_get_pid(dev->usbdev); | ||
| 443 | client_device_add(&info); | ||
| 444 | } | ||
| 445 | |||
| 446 | static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, int payload_length) | ||
| 447 | { | ||
| 448 | usbmuxd_log(LL_DEBUG, "[IN] dev=%d sport=%d dport=%d seq=%d ack=%d flags=0x%x window=%d[%d] len=%d", | ||
| 449 | dev->id, ntohs(th->th_sport), ntohs(th->th_dport), ntohl(th->th_seq), ntohl(th->th_ack), th->th_flags, ntohs(th->th_win) << 8, ntohs(th->th_win), payload_length); | ||
| 450 | |||
| 451 | uint16_t sport = ntohs(th->th_dport); | ||
| 452 | uint16_t dport = ntohs(th->th_sport); | ||
| 453 | struct mux_connection *conn = NULL; | ||
| 454 | FOREACH(struct mux_connection *lconn, &dev->connections) { | ||
| 455 | if(lconn->sport == sport && lconn->dport == dport) { | ||
| 456 | conn = lconn; | ||
| 457 | break; | ||
| 458 | } | ||
| 459 | } ENDFOREACH | ||
| 460 | |||
| 461 | if(!conn) { | ||
| 462 | usbmuxd_log(LL_WARNING, "No connection for device %d incoming packet %d->%d", dev->id, dport, sport); | ||
| 463 | return; | ||
| 464 | } | ||
| 465 | |||
| 466 | conn->rx_seq = ntohl(th->th_seq); | ||
| 467 | conn->rx_ack = ntohl(th->th_ack); | ||
| 468 | conn->rx_win = ntohs(th->th_win) << 8; | ||
| 469 | |||
| 470 | if(th->th_flags & TH_RST) { | ||
| 471 | char *buf = malloc(payload_length+1); | ||
| 472 | memcpy(buf, payload, payload_length); | ||
| 473 | if(payload_length && (buf[payload_length-1] == '\n')) | ||
| 474 | buf[payload_length-1] = 0; | ||
| 475 | buf[payload_length] = 0; | ||
| 476 | usbmuxd_log(LL_DEBUG, "RST reason: %s", buf); | ||
| 477 | free(buf); | ||
| 478 | } | ||
| 479 | |||
| 480 | if(conn->state == CONN_CONNECTING) { | ||
| 481 | if(th->th_flags != (TH_SYN|TH_ACK)) { | ||
| 482 | if(th->th_flags & TH_RST) | ||
| 483 | conn->state = CONN_REFUSED; | ||
| 484 | usbmuxd_log(LL_INFO, "Connection refused by device %d (%d->%d)", dev->id, sport, dport); | ||
| 485 | connection_teardown(conn); //this also sends the notification to the client | ||
| 486 | } else { | ||
| 487 | conn->tx_seq++; | ||
| 488 | conn->tx_ack++; | ||
| 489 | conn->rx_recvd = conn->rx_seq; | ||
| 490 | if(send_tcp(conn, TH_ACK, NULL, 0) < 0) { | ||
| 491 | usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, sport, dport); | ||
| 492 | connection_teardown(conn); | ||
| 493 | return; | ||
| 494 | } | ||
| 495 | conn->state = CONN_CONNECTED; | ||
| 496 | if(client_notify_connect(conn->client, RESULT_OK) < 0) { | ||
| 497 | conn->client = NULL; | ||
| 498 | connection_teardown(conn); | ||
| 499 | } | ||
| 500 | update_connection(conn); | ||
| 501 | } | ||
| 502 | } else if(conn->state == CONN_CONNECTED) { | ||
| 503 | if(th->th_flags != TH_ACK) { | ||
| 504 | usbmuxd_log(LL_INFO, "Connection reset by device %d (%d->%d)", dev->id, sport, dport); | ||
| 505 | if(th->th_flags & TH_RST) | ||
| 506 | conn->state = CONN_DYING; | ||
| 507 | connection_teardown(conn); | ||
| 508 | } else { | ||
| 509 | connection_device_input(conn, payload, payload_length); | ||
| 510 | } | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | void device_data_input(struct usb_device *usbdev, unsigned char *buffer, int length) | ||
| 515 | { | ||
| 516 | struct mux_device *dev = NULL; | ||
| 517 | FOREACH(struct mux_device *tdev, &device_list) { | ||
| 518 | if(tdev->usbdev == usbdev) { | ||
| 519 | dev = tdev; | ||
| 520 | break; | ||
| 521 | } | ||
| 522 | } ENDFOREACH | ||
| 523 | if(!dev) { | ||
| 524 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | ||
| 525 | return; | ||
| 526 | } | ||
| 527 | |||
| 528 | if(!length) | ||
| 529 | return; | ||
| 530 | |||
| 531 | usbmuxd_log(LL_SPEW, "Mux data input for device %p: %p len %d", dev, buffer, length); | ||
| 532 | |||
| 533 | // handle broken up transfers | ||
| 534 | if(dev->pktlen) { | ||
| 535 | memcpy(dev->pktbuf + dev->pktlen, buffer, length); | ||
| 536 | struct mux_header *mhdr = (struct mux_header *)dev->pktbuf; | ||
| 537 | if((length < USB_MRU) || (ntohl(mhdr->length) == length)) { | ||
| 538 | buffer = dev->pktbuf; | ||
| 539 | length += dev->pktlen; | ||
| 540 | dev->pktlen = 0; | ||
| 541 | usbmuxd_log(LL_SPEW, "Gathered mux data from buffer (total size: %d)", length); | ||
| 542 | } else { | ||
| 543 | dev->pktlen += length; | ||
| 544 | usbmuxd_log(LL_SPEW, "Appended mux data to buffer (total size: %d)", dev->pktlen); | ||
| 545 | return; | ||
| 546 | } | ||
| 547 | } else { | ||
| 548 | struct mux_header *mhdr = (struct mux_header *)buffer; | ||
| 549 | if((length == USB_MRU) && (length < ntohl(mhdr->length))) { | ||
| 550 | memcpy(dev->pktbuf, buffer, length); | ||
| 551 | dev->pktlen = length; | ||
| 552 | usbmuxd_log(LL_SPEW, "Copied mux data to buffer (size: %d)", dev->pktlen); | ||
| 553 | return; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | struct mux_header *mhdr = (struct mux_header *)buffer; | ||
| 558 | |||
| 559 | if(ntohl(mhdr->length) != length) { | ||
| 560 | usbmuxd_log(LL_ERROR, "Incoming packet size mismatch (dev %d, expected %d, got %d)", dev->id, ntohl(mhdr->length), length); | ||
| 561 | return; | ||
| 562 | } | ||
| 563 | |||
| 564 | struct tcphdr *th; | ||
| 565 | unsigned char *payload; | ||
| 566 | int payload_length; | ||
| 567 | |||
| 568 | switch(ntohl(mhdr->protocol)) { | ||
| 569 | case MUX_PROTO_VERSION: | ||
| 570 | device_version_input(dev, (struct version_header *)(mhdr+1)); | ||
| 571 | break; | ||
| 572 | case MUX_PROTO_TCP: | ||
| 573 | th = (struct tcphdr *)(mhdr+1); | ||
| 574 | payload = (unsigned char *)(th+1); | ||
| 575 | payload_length = length - sizeof(struct tcphdr) - sizeof(struct mux_header); | ||
| 576 | device_tcp_input(dev, (struct tcphdr *)(mhdr+1), payload, payload_length); | ||
| 577 | break; | ||
| 578 | default: | ||
| 579 | usbmuxd_log(LL_ERROR, "Incoming packet for device %d has unknown protocol 0x%x)", dev->id, ntohl(mhdr->protocol)); | ||
| 580 | break; | ||
| 581 | } | ||
| 582 | |||
| 583 | } | ||
| 584 | |||
| 585 | int device_add(struct usb_device *usbdev) | ||
| 586 | { | ||
| 587 | int res; | ||
| 588 | int id = get_next_device_id(); | ||
| 589 | struct mux_device *dev; | ||
| 590 | usbmuxd_log(LL_NOTICE, "Connecting to new device on location 0x%x as ID %d", usb_get_location(usbdev), id); | ||
| 591 | dev = malloc(sizeof(struct mux_device)); | ||
| 592 | dev->id = id; | ||
| 593 | dev->usbdev = usbdev; | ||
| 594 | dev->state = MUXDEV_INIT; | ||
| 595 | dev->next_sport = 1; | ||
| 596 | dev->pktbuf = malloc(DEV_PKTBUF_SIZE); | ||
| 597 | dev->pktlen = 0; | ||
| 598 | struct version_header vh; | ||
| 599 | vh.major = htonl(1); | ||
| 600 | vh.minor = htonl(0); | ||
| 601 | vh.padding = 0; | ||
| 602 | if((res = send_packet(dev, MUX_PROTO_VERSION, &vh, NULL, 0)) < 0) { | ||
| 603 | usbmuxd_log(LL_ERROR, "Error sending version request packet to device %d\n", id); | ||
| 604 | free(dev); | ||
| 605 | return res; | ||
| 606 | } | ||
| 607 | collection_add(&device_list, dev); | ||
| 608 | return 0; | ||
| 609 | } | ||
| 610 | |||
| 611 | void device_remove(struct usb_device *usbdev) | ||
| 612 | { | ||
| 613 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 614 | if(dev->usbdev == usbdev) { | ||
| 615 | usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev)); | ||
| 616 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 617 | dev->state = MUXDEV_DEAD; | ||
| 618 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 619 | connection_teardown(conn); | ||
| 620 | } ENDFOREACH | ||
| 621 | client_device_remove(dev->id); | ||
| 622 | collection_free(&dev->connections); | ||
| 623 | } | ||
| 624 | collection_remove(&device_list, dev); | ||
| 625 | free(dev->pktbuf); | ||
| 626 | free(dev); | ||
| 627 | return; | ||
| 628 | } | ||
| 629 | } ENDFOREACH | ||
| 630 | usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | ||
| 631 | } | ||
| 632 | |||
| 633 | int device_get_count(void) | ||
| 634 | { | ||
| 635 | int count = 0; | ||
| 636 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 637 | if(dev->state == MUXDEV_ACTIVE) | ||
| 638 | count++; | ||
| 639 | } ENDFOREACH | ||
| 640 | return count; | ||
| 641 | } | ||
| 642 | |||
| 643 | int device_get_list(struct device_info *p) | ||
| 644 | { | ||
| 645 | int count = 0; | ||
| 646 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 647 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 648 | p->id = dev->id; | ||
| 649 | p->serial = usb_get_serial(dev->usbdev); | ||
| 650 | p->location = usb_get_location(dev->usbdev); | ||
| 651 | p->pid = usb_get_pid(dev->usbdev); | ||
| 652 | count++; | ||
| 653 | p++; | ||
| 654 | } | ||
| 655 | } ENDFOREACH | ||
| 656 | return count; | ||
| 657 | } | ||
| 658 | |||
| 659 | int device_get_timeout(void) | ||
| 660 | { | ||
| 661 | uint64_t oldest = (uint64_t)-1; | ||
| 662 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 663 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 664 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 665 | if((conn->state == CONN_CONNECTED) && (conn->flags & CONN_ACK_PENDING) && conn->last_ack_time < oldest) | ||
| 666 | oldest = conn->last_ack_time; | ||
| 667 | } ENDFOREACH | ||
| 668 | } | ||
| 669 | } ENDFOREACH | ||
| 670 | uint64_t ct = mstime64(); | ||
| 671 | if(oldest == -1) | ||
| 672 | return 100000; //meh | ||
| 673 | if((ct - oldest) > ACK_TIMEOUT) | ||
| 674 | return 0; | ||
| 675 | return ACK_TIMEOUT - (ct - oldest); | ||
| 676 | } | ||
| 677 | |||
| 678 | void device_check_timeouts(void) | ||
| 679 | { | ||
| 680 | uint64_t ct = mstime64(); | ||
| 681 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 682 | if(dev->state == MUXDEV_ACTIVE) { | ||
| 683 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 684 | if((conn->state == CONN_CONNECTED) && | ||
| 685 | (conn->flags & CONN_ACK_PENDING) && | ||
| 686 | (ct - conn->last_ack_time) > ACK_TIMEOUT) { | ||
| 687 | usbmuxd_log(LL_DEBUG, "Sending ACK due to expired timeout (%" PRIu64 " -> %" PRIu64 ")", conn->last_ack_time, ct); | ||
| 688 | if(send_tcp(conn, TH_ACK, NULL, 0) < 0) { | ||
| 689 | usbmuxd_log(LL_ERROR, "Error sending TCP ACK to device %d (%d->%d)", dev->id, conn->sport, conn->dport); | ||
| 690 | connection_teardown(conn); | ||
| 691 | } | ||
| 692 | } | ||
| 693 | } ENDFOREACH | ||
| 694 | } | ||
| 695 | } ENDFOREACH | ||
| 696 | } | ||
| 697 | |||
| 698 | void device_init(void) | ||
| 699 | { | ||
| 700 | usbmuxd_log(LL_DEBUG, "device_init"); | ||
| 701 | collection_init(&device_list); | ||
| 702 | next_device_id = 1; | ||
| 703 | } | ||
| 704 | |||
| 705 | void device_kill_connections(void) | ||
| 706 | { | ||
| 707 | usbmuxd_log(LL_DEBUG, "device_kill_connections"); | ||
| 708 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 709 | if(dev->state != MUXDEV_INIT) { | ||
| 710 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 711 | connection_teardown(conn); | ||
| 712 | } ENDFOREACH | ||
| 713 | } | ||
| 714 | } ENDFOREACH | ||
| 715 | // give USB a while to send the final connection RSTs and the like | ||
| 716 | usb_process_timeout(100); | ||
| 717 | } | ||
| 718 | |||
| 719 | void device_shutdown(void) | ||
| 720 | { | ||
| 721 | usbmuxd_log(LL_DEBUG, "device_shutdown"); | ||
| 722 | FOREACH(struct mux_device *dev, &device_list) { | ||
| 723 | FOREACH(struct mux_connection *conn, &dev->connections) { | ||
| 724 | connection_teardown(conn); | ||
| 725 | } ENDFOREACH | ||
| 726 | collection_free(&dev->connections); | ||
| 727 | collection_remove(&device_list, dev); | ||
| 728 | free(dev); | ||
| 729 | } ENDFOREACH | ||
| 730 | collection_free(&device_list); | ||
| 731 | } | ||
diff --git a/usbmuxd/device.h b/usbmuxd/device.h new file mode 100644 index 0000000..ce6c50b --- /dev/null +++ b/usbmuxd/device.h | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __DEVICE_H__ | ||
| 22 | #define __DEVICE_H__ | ||
| 23 | |||
| 24 | #include "usb.h" | ||
| 25 | #include "client.h" | ||
| 26 | |||
| 27 | struct device_info { | ||
| 28 | int id; | ||
| 29 | const char *serial; | ||
| 30 | uint32_t location; | ||
| 31 | uint16_t pid; | ||
| 32 | }; | ||
| 33 | |||
| 34 | void device_data_input(struct usb_device *dev, unsigned char *buf, int length); | ||
| 35 | |||
| 36 | int device_add(struct usb_device *dev); | ||
| 37 | void device_remove(struct usb_device *dev); | ||
| 38 | |||
| 39 | int device_start_connect(int device_id, uint16_t port, struct mux_client *client); | ||
| 40 | void device_client_process(int device_id, struct mux_client *client, short events); | ||
| 41 | void device_abort_connect(int device_id, struct mux_client *client); | ||
| 42 | |||
| 43 | int device_get_count(void); | ||
| 44 | int device_get_list(struct device_info *p); | ||
| 45 | |||
| 46 | int device_get_timeout(void); | ||
| 47 | void device_check_timeouts(void); | ||
| 48 | |||
| 49 | void device_init(void); | ||
| 50 | void device_kill_connections(void); | ||
| 51 | void device_shutdown(void); | ||
| 52 | #endif | ||
diff --git a/usbmuxd/log.c b/usbmuxd/log.c new file mode 100644 index 0000000..a70120b --- /dev/null +++ b/usbmuxd/log.c | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <stdarg.h> | ||
| 29 | #include <time.h> | ||
| 30 | #include <sys/time.h> | ||
| 31 | |||
| 32 | #include "log.h" | ||
| 33 | |||
| 34 | int log_level = LL_INFO; | ||
| 35 | |||
| 36 | void usbmuxd_log(enum loglevel level, const char *fmt, ...) | ||
| 37 | { | ||
| 38 | va_list ap; | ||
| 39 | char *fs; | ||
| 40 | struct timeval ts; | ||
| 41 | struct tm *tp; | ||
| 42 | |||
| 43 | gettimeofday(&ts, NULL); | ||
| 44 | tp = localtime(&ts.tv_sec); | ||
| 45 | |||
| 46 | if(level > log_level) | ||
| 47 | return; | ||
| 48 | |||
| 49 | fs = malloc(20 + strlen(fmt)); | ||
| 50 | strftime(fs, 10, "[%H:%M:%S", tp); | ||
| 51 | sprintf(fs+9, ".%03d][%d] %s\n", (int)(ts.tv_usec / 1000), level, fmt); | ||
| 52 | |||
| 53 | va_start(ap, fmt); | ||
| 54 | vfprintf(stderr, fs, ap); | ||
| 55 | va_end(ap); | ||
| 56 | |||
| 57 | free(fs); | ||
| 58 | } | ||
diff --git a/usbmuxd/log.h b/usbmuxd/log.h new file mode 100644 index 0000000..f6eb5c1 --- /dev/null +++ b/usbmuxd/log.h | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __LOG_H__ | ||
| 22 | #define __LOG_H__ | ||
| 23 | |||
| 24 | enum loglevel { | ||
| 25 | LL_FATAL = 0, | ||
| 26 | LL_ERROR, | ||
| 27 | LL_WARNING, | ||
| 28 | LL_NOTICE, | ||
| 29 | LL_INFO, | ||
| 30 | LL_DEBUG, | ||
| 31 | LL_SPEW, | ||
| 32 | LL_FLOOD, | ||
| 33 | }; | ||
| 34 | |||
| 35 | extern int log_level; | ||
| 36 | |||
| 37 | void usbmuxd_log(enum loglevel level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); | ||
| 38 | |||
| 39 | |||
| 40 | #endif | ||
diff --git a/usbmuxd/main.c b/usbmuxd/main.c new file mode 100644 index 0000000..d39b416 --- /dev/null +++ b/usbmuxd/main.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #define _BSD_SOURCE | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdio.h> | ||
| 28 | #include <errno.h> | ||
| 29 | #include <string.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <signal.h> | ||
| 32 | #include <unistd.h> | ||
| 33 | #include <sys/socket.h> | ||
| 34 | #include <sys/un.h> | ||
| 35 | |||
| 36 | #include "log.h" | ||
| 37 | #include "usb.h" | ||
| 38 | #include "device.h" | ||
| 39 | #include "client.h" | ||
| 40 | |||
| 41 | static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME | ||
| 42 | int should_exit; | ||
| 43 | |||
| 44 | struct sigaction sa_old; | ||
| 45 | |||
| 46 | int create_socket(void) { | ||
| 47 | struct sockaddr_un bind_addr; | ||
| 48 | int listenfd; | ||
| 49 | |||
| 50 | if(unlink(socket_path) == -1 && errno != ENOENT) { | ||
| 51 | usbmuxd_log(LL_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno)); | ||
| 52 | return -1; | ||
| 53 | } | ||
| 54 | |||
| 55 | listenfd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
| 56 | if (listenfd == -1) { | ||
| 57 | usbmuxd_log(LL_FATAL, "socket() failed: %s", strerror(errno)); | ||
| 58 | return -1; | ||
| 59 | } | ||
| 60 | |||
| 61 | bzero(&bind_addr, sizeof(bind_addr)); | ||
| 62 | bind_addr.sun_family = AF_UNIX; | ||
| 63 | strcpy(bind_addr.sun_path, socket_path); | ||
| 64 | if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) { | ||
| 65 | usbmuxd_log(LL_FATAL, "bind() failed: %s", strerror(errno)); | ||
| 66 | return -1; | ||
| 67 | } | ||
| 68 | |||
| 69 | // Start listening | ||
| 70 | if (listen(listenfd, 5) != 0) { | ||
| 71 | usbmuxd_log(LL_FATAL, "listen() failed: %s", strerror(errno)); | ||
| 72 | return -1; | ||
| 73 | } | ||
| 74 | |||
| 75 | return listenfd; | ||
| 76 | } | ||
| 77 | |||
| 78 | void handle_signal(int sig) | ||
| 79 | { | ||
| 80 | if(sig == SIGINT) { | ||
| 81 | usbmuxd_log(LL_NOTICE,"Caught SIGINT"); | ||
| 82 | } else { | ||
| 83 | usbmuxd_log(LL_NOTICE,"Caught unknown signal %d", sig); | ||
| 84 | } | ||
| 85 | should_exit = 1; | ||
| 86 | sigaction(SIGINT, &sa_old, NULL); | ||
| 87 | } | ||
| 88 | |||
| 89 | void set_signal_handlers(void) | ||
| 90 | { | ||
| 91 | struct sigaction sa; | ||
| 92 | memset(&sa, 0, sizeof(struct sigaction)); | ||
| 93 | sa.sa_handler = handle_signal; | ||
| 94 | sigaction(SIGINT, &sa, &sa_old); | ||
| 95 | } | ||
| 96 | |||
| 97 | int main_loop(int listenfd) | ||
| 98 | { | ||
| 99 | int to, cnt, i, dto; | ||
| 100 | struct fdlist pollfds; | ||
| 101 | |||
| 102 | while(!should_exit) { | ||
| 103 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); | ||
| 104 | to = usb_get_timeout(); | ||
| 105 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); | ||
| 106 | dto = device_get_timeout(); | ||
| 107 | usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", to); | ||
| 108 | if(dto < to) | ||
| 109 | to = dto; | ||
| 110 | |||
| 111 | fdlist_create(&pollfds); | ||
| 112 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); | ||
| 113 | usb_get_fds(&pollfds); | ||
| 114 | client_get_fds(&pollfds); | ||
| 115 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); | ||
| 116 | |||
| 117 | cnt = poll(pollfds.fds, pollfds.count, to); | ||
| 118 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); | ||
| 119 | |||
| 120 | if(cnt == -1) { | ||
| 121 | if(errno == EINTR && should_exit) { | ||
| 122 | usbmuxd_log(LL_INFO, "event processing interrupted"); | ||
| 123 | fdlist_free(&pollfds); | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | } else if(cnt == 0) { | ||
| 127 | if(usb_process() < 0) { | ||
| 128 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | ||
| 129 | fdlist_free(&pollfds); | ||
| 130 | return -1; | ||
| 131 | } | ||
| 132 | device_check_timeouts(); | ||
| 133 | } else { | ||
| 134 | int done_usb = 0; | ||
| 135 | for(i=0; i<pollfds.count; i++) { | ||
| 136 | if(pollfds.fds[i].revents) { | ||
| 137 | if(!done_usb && pollfds.owners[i] == FD_USB) { | ||
| 138 | if(usb_process() < 0) { | ||
| 139 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | ||
| 140 | fdlist_free(&pollfds); | ||
| 141 | return -1; | ||
| 142 | } | ||
| 143 | done_usb = 1; | ||
| 144 | } | ||
| 145 | if(pollfds.owners[i] == FD_LISTEN) { | ||
| 146 | if(client_accept(listenfd) < 0) { | ||
| 147 | usbmuxd_log(LL_FATAL, "client_accept() failed"); | ||
| 148 | fdlist_free(&pollfds); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if(pollfds.owners[i] == FD_CLIENT) { | ||
| 153 | client_process(pollfds.fds[i].fd, pollfds.fds[i].revents); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | fdlist_free(&pollfds); | ||
| 159 | } | ||
| 160 | return 0; | ||
| 161 | } | ||
| 162 | |||
| 163 | int main(int argc, char *argv[]) | ||
| 164 | { | ||
| 165 | int listenfd; | ||
| 166 | int res; | ||
| 167 | |||
| 168 | usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up"); | ||
| 169 | should_exit = 0; | ||
| 170 | |||
| 171 | set_signal_handlers(); | ||
| 172 | |||
| 173 | usbmuxd_log(LL_INFO, "Creating socket"); | ||
| 174 | listenfd = create_socket(); | ||
| 175 | if(listenfd < 0) | ||
| 176 | return 1; | ||
| 177 | |||
| 178 | client_init(); | ||
| 179 | device_init(); | ||
| 180 | usbmuxd_log(LL_INFO, "Initializing USB"); | ||
| 181 | if((res = usb_init()) < 0) | ||
| 182 | return 2; | ||
| 183 | usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s"); | ||
| 184 | |||
| 185 | usbmuxd_log(LL_NOTICE, "Initialization complete"); | ||
| 186 | |||
| 187 | res = main_loop(listenfd); | ||
| 188 | if(res < 0) | ||
| 189 | usbmuxd_log(LL_FATAL, "main_loop failed"); | ||
| 190 | |||
| 191 | usbmuxd_log(LL_NOTICE, "usbmux shutting down"); | ||
| 192 | device_kill_connections(); | ||
| 193 | usb_shutdown(); | ||
| 194 | device_shutdown(); | ||
| 195 | client_shutdown(); | ||
| 196 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); | ||
| 197 | |||
| 198 | if(res < 0) | ||
| 199 | return -res; | ||
| 200 | return 0; | ||
| 201 | } | ||
diff --git a/usbmuxd/usb-linux.c b/usbmuxd/usb-linux.c new file mode 100644 index 0000000..b401cde --- /dev/null +++ b/usbmuxd/usb-linux.c | |||
| @@ -0,0 +1,501 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <stdint.h> | ||
| 28 | #include <string.h> | ||
| 29 | |||
| 30 | #include <libusb.h> | ||
| 31 | |||
| 32 | #include "usb.h" | ||
| 33 | #include "log.h" | ||
| 34 | #include "device.h" | ||
| 35 | |||
| 36 | // interval for device connection/disconnection polling, in milliseconds | ||
| 37 | // we need this because there is currently no asynchronous device discovery mechanism in libusb | ||
| 38 | #define DEVICE_POLL_TIME 1000 | ||
| 39 | |||
| 40 | struct usb_device { | ||
| 41 | libusb_device_handle *dev; | ||
| 42 | uint8_t bus, address; | ||
| 43 | uint16_t vid, pid; | ||
| 44 | char serial[256]; | ||
| 45 | int alive; | ||
| 46 | struct libusb_transfer *rx_xfer; | ||
| 47 | struct collection tx_xfers; | ||
| 48 | }; | ||
| 49 | |||
| 50 | static struct collection device_list; | ||
| 51 | |||
| 52 | static struct timeval next_dev_poll_time; | ||
| 53 | |||
| 54 | static int devlist_failures; | ||
| 55 | |||
| 56 | static void usb_disconnect(struct usb_device *dev) | ||
| 57 | { | ||
| 58 | if(!dev->dev) { | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | // kill the rx xfer and tx xfers and try to make sure the callbacks get called before we free the device | ||
| 63 | if(dev->rx_xfer) { | ||
| 64 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling RX xfer"); | ||
| 65 | libusb_cancel_transfer(dev->rx_xfer); | ||
| 66 | } | ||
| 67 | FOREACH(struct libusb_transfer *xfer, &dev->tx_xfers) { | ||
| 68 | usbmuxd_log(LL_DEBUG, "usb_disconnect: cancelling TX xfer %p", xfer); | ||
| 69 | libusb_cancel_transfer(xfer); | ||
| 70 | } ENDFOREACH | ||
| 71 | |||
| 72 | while(dev->rx_xfer || collection_count(&dev->tx_xfers)) { | ||
| 73 | struct timeval tv; | ||
| 74 | int res; | ||
| 75 | |||
| 76 | tv.tv_sec = 0; | ||
| 77 | tv.tv_usec = 1000; | ||
| 78 | if((res = libusb_handle_events_timeout(NULL, &tv)) < 0) { | ||
| 79 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout for usb_disconnect failed: %d", res); | ||
| 80 | break; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | collection_free(&dev->tx_xfers); | ||
| 84 | libusb_release_interface(dev->dev, USB_INTERFACE); | ||
| 85 | libusb_close(dev->dev); | ||
| 86 | dev->dev = NULL; | ||
| 87 | collection_remove(&device_list, dev); | ||
| 88 | free(dev); | ||
| 89 | } | ||
| 90 | |||
| 91 | static void tx_callback(struct libusb_transfer *xfer) | ||
| 92 | { | ||
| 93 | struct usb_device *dev = xfer->user_data; | ||
| 94 | usbmuxd_log(LL_SPEW, "TX callback dev %d-%d len %d -> %d status %d", dev->bus, dev->address, xfer->length, xfer->actual_length, xfer->status); | ||
| 95 | if(xfer->status != LIBUSB_TRANSFER_COMPLETED) { | ||
| 96 | switch(xfer->status) { | ||
| 97 | case LIBUSB_TRANSFER_COMPLETED: //shut up compiler | ||
| 98 | case LIBUSB_TRANSFER_ERROR: | ||
| 99 | // funny, this happens when we disconnect the device while waiting for a transfer, sometimes | ||
| 100 | usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to error or disconnect", dev->bus, dev->address); | ||
| 101 | break; | ||
| 102 | case LIBUSB_TRANSFER_TIMED_OUT: | ||
| 103 | usbmuxd_log(LL_ERROR, "TX transfer timed out for device %d-%d", dev->bus, dev->address); | ||
| 104 | break; | ||
| 105 | case LIBUSB_TRANSFER_CANCELLED: | ||
| 106 | usbmuxd_log(LL_ERROR, "TX transfer cancelled for device %d-%d", dev->bus, dev->address); | ||
| 107 | break; | ||
| 108 | case LIBUSB_TRANSFER_STALL: | ||
| 109 | usbmuxd_log(LL_ERROR, "TX transfer stalled for device %d-%d", dev->bus, dev->address); | ||
| 110 | break; | ||
| 111 | case LIBUSB_TRANSFER_NO_DEVICE: | ||
| 112 | // other times, this happens, and also even when we abort the transfer after device removal | ||
| 113 | usbmuxd_log(LL_INFO, "Device %d-%d TX aborted due to disconnect", dev->bus, dev->address); | ||
| 114 | break; | ||
| 115 | case LIBUSB_TRANSFER_OVERFLOW: | ||
| 116 | usbmuxd_log(LL_ERROR, "TX transfer overflow for device %d-%d", dev->bus, dev->address); | ||
| 117 | break; | ||
| 118 | // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) | ||
| 119 | } | ||
| 120 | // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events | ||
| 121 | // we'll do device_remove there too | ||
| 122 | dev->alive = 0; | ||
| 123 | } | ||
| 124 | if(xfer->buffer) | ||
| 125 | free(xfer->buffer); | ||
| 126 | collection_remove(&dev->tx_xfers, xfer); | ||
| 127 | libusb_free_transfer(xfer); | ||
| 128 | } | ||
| 129 | |||
| 130 | int usb_send(struct usb_device *dev, const unsigned char *buf, int length) | ||
| 131 | { | ||
| 132 | int res; | ||
| 133 | struct libusb_transfer *xfer = libusb_alloc_transfer(0); | ||
| 134 | libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, (void*)buf, length, tx_callback, dev, 0); | ||
| 135 | xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK; | ||
| 136 | if((res = libusb_submit_transfer(xfer)) < 0) { | ||
| 137 | usbmuxd_log(LL_ERROR, "Failed to submit TX transfer %p len %d to device %d-%d: %d", buf, length, dev->bus, dev->address, res); | ||
| 138 | libusb_free_transfer(xfer); | ||
| 139 | return res; | ||
| 140 | } | ||
| 141 | collection_add(&dev->tx_xfers, xfer);/* | ||
| 142 | if((length % 512) == 0) { | ||
| 143 | usbmuxd_log(LL_DEBUG, "Send ZLP"); | ||
| 144 | // Send Zero Length Packet | ||
| 145 | xfer = libusb_alloc_transfer(0); | ||
| 146 | void *buffer = malloc(1); | ||
| 147 | libusb_fill_bulk_transfer(xfer, dev->dev, BULK_OUT, buffer, 0, tx_callback, dev, 0); | ||
| 148 | xfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK; | ||
| 149 | if((res = libusb_submit_transfer(xfer)) < 0) { | ||
| 150 | usbmuxd_log(LL_ERROR, "Failed to submit TX ZLP transfer to device %d-%d: %d", dev->bus, dev->address, res); | ||
| 151 | libusb_free_transfer(xfer); | ||
| 152 | return res; | ||
| 153 | } | ||
| 154 | collection_add(&dev->tx_xfers, xfer); | ||
| 155 | }*/ | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | static void rx_callback(struct libusb_transfer *xfer) | ||
| 160 | { | ||
| 161 | struct usb_device *dev = xfer->user_data; | ||
| 162 | usbmuxd_log(LL_SPEW, "RX callback dev %d-%d len %d status %d", dev->bus, dev->address, xfer->actual_length, xfer->status); | ||
| 163 | if(xfer->status == LIBUSB_TRANSFER_COMPLETED) { | ||
| 164 | device_data_input(dev, xfer->buffer, xfer->actual_length); | ||
| 165 | libusb_submit_transfer(xfer); | ||
| 166 | } else { | ||
| 167 | switch(xfer->status) { | ||
| 168 | case LIBUSB_TRANSFER_COMPLETED: //shut up compiler | ||
| 169 | case LIBUSB_TRANSFER_ERROR: | ||
| 170 | // funny, this happens when we disconnect the device while waiting for a transfer, sometimes | ||
| 171 | usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to error or disconnect", dev->bus, dev->address); | ||
| 172 | break; | ||
| 173 | case LIBUSB_TRANSFER_TIMED_OUT: | ||
| 174 | usbmuxd_log(LL_ERROR, "RX transfer timed out for device %d-%d", dev->bus, dev->address); | ||
| 175 | break; | ||
| 176 | case LIBUSB_TRANSFER_CANCELLED: | ||
| 177 | usbmuxd_log(LL_ERROR, "RX transfer cancelled for device %d-%d", dev->bus, dev->address); | ||
| 178 | break; | ||
| 179 | case LIBUSB_TRANSFER_STALL: | ||
| 180 | usbmuxd_log(LL_ERROR, "RX transfer stalled for device %d-%d", dev->bus, dev->address); | ||
| 181 | break; | ||
| 182 | case LIBUSB_TRANSFER_NO_DEVICE: | ||
| 183 | // other times, this happens, and also even when we abort the transfer after device removal | ||
| 184 | usbmuxd_log(LL_INFO, "Device %d-%d RX aborted due to disconnect", dev->bus, dev->address); | ||
| 185 | break; | ||
| 186 | case LIBUSB_TRANSFER_OVERFLOW: | ||
| 187 | usbmuxd_log(LL_ERROR, "RX transfer overflow for device %d-%d", dev->bus, dev->address); | ||
| 188 | break; | ||
| 189 | // and nothing happens (this never gets called) if the device is freed after a disconnect! (bad) | ||
| 190 | } | ||
| 191 | free(xfer->buffer); | ||
| 192 | dev->rx_xfer = NULL; | ||
| 193 | libusb_free_transfer(xfer); | ||
| 194 | // we can't usb_disconnect here due to a deadlock, so instead mark it as dead and reap it after processing events | ||
| 195 | // we'll do device_remove there too | ||
| 196 | dev->alive = 0; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | static int start_rx(struct usb_device *dev) | ||
| 201 | { | ||
| 202 | int res; | ||
| 203 | void *buf; | ||
| 204 | dev->rx_xfer = libusb_alloc_transfer(0); | ||
| 205 | buf = malloc(USB_MRU); | ||
| 206 | libusb_fill_bulk_transfer(dev->rx_xfer, dev->dev, BULK_IN, buf, USB_MRU, rx_callback, dev, 0); | ||
| 207 | if((res = libusb_submit_transfer(dev->rx_xfer)) != 0) { | ||
| 208 | usbmuxd_log(LL_ERROR, "Failed to submit RX transfer to device %d-%d: %d", dev->bus, dev->address, res); | ||
| 209 | libusb_free_transfer(dev->rx_xfer); | ||
| 210 | dev->rx_xfer = NULL; | ||
| 211 | return res; | ||
| 212 | } | ||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | static int usb_discover(void) | ||
| 217 | { | ||
| 218 | int cnt, i, res; | ||
| 219 | int valid_count = 0; | ||
| 220 | libusb_device **devs; | ||
| 221 | |||
| 222 | cnt = libusb_get_device_list(NULL, &devs); | ||
| 223 | if(cnt < 0) { | ||
| 224 | usbmuxd_log(LL_WARNING, "Could not get device list: %d", cnt); | ||
| 225 | devlist_failures++; | ||
| 226 | // sometimes libusb fails getting the device list if you've just removed something | ||
| 227 | if(devlist_failures > 5) { | ||
| 228 | usbmuxd_log(LL_FATAL, "Too many errors getting device list\n"); | ||
| 229 | return cnt; | ||
| 230 | } else { | ||
| 231 | gettimeofday(&next_dev_poll_time, NULL); | ||
| 232 | next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; | ||
| 233 | next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; | ||
| 234 | next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; | ||
| 235 | return 0; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | devlist_failures = 0; | ||
| 239 | |||
| 240 | usbmuxd_log(LL_SPEW, "usb_discover: scanning %d devices", cnt); | ||
| 241 | |||
| 242 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 243 | usbdev->alive = 0; | ||
| 244 | } ENDFOREACH | ||
| 245 | |||
| 246 | for(i=0; i<cnt; i++) { | ||
| 247 | // the following are non-blocking operations on the device list | ||
| 248 | libusb_device *dev = devs[i]; | ||
| 249 | uint8_t bus = libusb_get_bus_number(dev); | ||
| 250 | uint8_t address = libusb_get_device_address(dev); | ||
| 251 | struct libusb_device_descriptor devdesc; | ||
| 252 | int found = 0; | ||
| 253 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 254 | if(usbdev->bus == bus && usbdev->address == address) { | ||
| 255 | valid_count++; | ||
| 256 | usbdev->alive = 1; | ||
| 257 | found = 1; | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | } ENDFOREACH | ||
| 261 | if(found) | ||
| 262 | continue; //device already found | ||
| 263 | if((res = libusb_get_device_descriptor(dev, &devdesc)) != 0) { | ||
| 264 | usbmuxd_log(LL_WARNING, "Could not get device descriptor for device %d-%d: %d", bus, address, res); | ||
| 265 | continue; | ||
| 266 | } | ||
| 267 | if(devdesc.idVendor != VID_APPLE) | ||
| 268 | continue; | ||
| 269 | if( (devdesc.idProduct != PID_IPHONE2G) && | ||
| 270 | (devdesc.idProduct != PID_ITOUCH1G) && | ||
| 271 | (devdesc.idProduct != PID_IPHONE3G)) | ||
| 272 | continue; | ||
| 273 | libusb_device_handle *handle; | ||
| 274 | usbmuxd_log(LL_INFO, "Found new device with v/p %04x:%04x at %d-%d", devdesc.idVendor, devdesc.idProduct, bus, address); | ||
| 275 | // potentially blocking operations follow; they will only run when new devices are detected, which is acceptable | ||
| 276 | if((res = libusb_open(dev, &handle)) != 0) { | ||
| 277 | usbmuxd_log(LL_WARNING, "Could not open device %d-%d: %d", bus, address, res); | ||
| 278 | continue; | ||
| 279 | } | ||
| 280 | if((res = libusb_set_configuration(handle, USB_CONFIGURATION)) != 0) { | ||
| 281 | usbmuxd_log(LL_WARNING, "Could not set configuration %d for device %d-%d: %d", USB_CONFIGURATION, bus, address, res); | ||
| 282 | libusb_close(handle); | ||
| 283 | continue; | ||
| 284 | } | ||
| 285 | if((res = libusb_claim_interface(handle, USB_INTERFACE)) != 0) { | ||
| 286 | usbmuxd_log(LL_WARNING, "Could not claim interface %d for device %d-%d: %d", USB_INTERFACE, bus, address, res); | ||
| 287 | libusb_close(handle); | ||
| 288 | continue; | ||
| 289 | } | ||
| 290 | struct usb_device *usbdev; | ||
| 291 | usbdev = malloc(sizeof(struct usb_device)); | ||
| 292 | |||
| 293 | if((res = libusb_get_string_descriptor_ascii(handle, devdesc.iSerialNumber, (uint8_t *)usbdev->serial, 256)) <= 0) { | ||
| 294 | usbmuxd_log(LL_WARNING, "Could not get serial number for device %d-%d: %d", bus, address, res); | ||
| 295 | libusb_release_interface(handle, USB_INTERFACE); | ||
| 296 | libusb_close(handle); | ||
| 297 | free(usbdev); | ||
| 298 | continue; | ||
| 299 | } | ||
| 300 | usbdev->serial[res] = 0; | ||
| 301 | usbdev->bus = bus; | ||
| 302 | usbdev->address = address; | ||
| 303 | usbdev->vid = devdesc.idVendor; | ||
| 304 | usbdev->pid = devdesc.idProduct; | ||
| 305 | usbdev->dev = handle; | ||
| 306 | usbdev->alive = 1; | ||
| 307 | collection_init(&usbdev->tx_xfers); | ||
| 308 | |||
| 309 | collection_add(&device_list, usbdev); | ||
| 310 | |||
| 311 | if(device_add(usbdev) < 0) { | ||
| 312 | usb_disconnect(usbdev); | ||
| 313 | continue; | ||
| 314 | } | ||
| 315 | if(start_rx(usbdev) < 0) { | ||
| 316 | device_remove(usbdev); | ||
| 317 | usb_disconnect(usbdev); | ||
| 318 | continue; | ||
| 319 | } | ||
| 320 | valid_count++; | ||
| 321 | } | ||
| 322 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 323 | if(!usbdev->alive) { | ||
| 324 | device_remove(usbdev); | ||
| 325 | usb_disconnect(usbdev); | ||
| 326 | } | ||
| 327 | } ENDFOREACH | ||
| 328 | |||
| 329 | libusb_free_device_list(devs, 1); | ||
| 330 | |||
| 331 | gettimeofday(&next_dev_poll_time, NULL); | ||
| 332 | next_dev_poll_time.tv_usec += DEVICE_POLL_TIME * 1000; | ||
| 333 | next_dev_poll_time.tv_sec += next_dev_poll_time.tv_usec / 1000000; | ||
| 334 | next_dev_poll_time.tv_usec = next_dev_poll_time.tv_usec % 1000000; | ||
| 335 | |||
| 336 | return valid_count; | ||
| 337 | } | ||
| 338 | |||
| 339 | const char *usb_get_serial(struct usb_device *dev) | ||
| 340 | { | ||
| 341 | if(!dev->dev) { | ||
| 342 | return NULL; | ||
| 343 | } | ||
| 344 | return dev->serial; | ||
| 345 | } | ||
| 346 | |||
| 347 | uint32_t usb_get_location(struct usb_device *dev) | ||
| 348 | { | ||
| 349 | if(!dev->dev) { | ||
| 350 | return 0; | ||
| 351 | } | ||
| 352 | return (dev->bus << 16) | dev->address; | ||
| 353 | } | ||
| 354 | |||
| 355 | uint16_t usb_get_pid(struct usb_device *dev) | ||
| 356 | { | ||
| 357 | if(!dev->dev) { | ||
| 358 | return 0; | ||
| 359 | } | ||
| 360 | return dev->pid; | ||
| 361 | } | ||
| 362 | |||
| 363 | void usb_get_fds(struct fdlist *list) | ||
| 364 | { | ||
| 365 | const struct libusb_pollfd **usbfds; | ||
| 366 | const struct libusb_pollfd **p; | ||
| 367 | usbfds = libusb_get_pollfds(NULL); | ||
| 368 | if(!usbfds) { | ||
| 369 | usbmuxd_log(LL_ERROR, "libusb_get_pollfds failed"); | ||
| 370 | return; | ||
| 371 | } | ||
| 372 | p = usbfds; | ||
| 373 | while(*p) { | ||
| 374 | fdlist_add(list, FD_USB, (*p)->fd, (*p)->events); | ||
| 375 | p++; | ||
| 376 | } | ||
| 377 | free(usbfds); | ||
| 378 | } | ||
| 379 | |||
| 380 | static int dev_poll_remain_ms(void) | ||
| 381 | { | ||
| 382 | int msecs; | ||
| 383 | struct timeval tv; | ||
| 384 | gettimeofday(&tv, NULL); | ||
| 385 | msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000; | ||
| 386 | msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000; | ||
| 387 | if(msecs < 0) | ||
| 388 | return 0; | ||
| 389 | return msecs; | ||
| 390 | } | ||
| 391 | |||
| 392 | int usb_get_timeout(void) | ||
| 393 | { | ||
| 394 | struct timeval tv; | ||
| 395 | int msec; | ||
| 396 | int res; | ||
| 397 | int pollrem; | ||
| 398 | pollrem = dev_poll_remain_ms(); | ||
| 399 | res = libusb_get_next_timeout(NULL, &tv); | ||
| 400 | if(res == 0) | ||
| 401 | return pollrem; | ||
| 402 | if(res < 0) { | ||
| 403 | usbmuxd_log(LL_ERROR, "libusb_get_next_timeout failed: %d", res); | ||
| 404 | return pollrem; | ||
| 405 | } | ||
| 406 | msec = tv.tv_sec * 1000; | ||
| 407 | msec += tv.tv_usec / 1000; | ||
| 408 | if(msec > pollrem) | ||
| 409 | return pollrem; | ||
| 410 | return msec; | ||
| 411 | } | ||
| 412 | |||
| 413 | int usb_process(void) | ||
| 414 | { | ||
| 415 | int res; | ||
| 416 | struct timeval tv; | ||
| 417 | tv.tv_sec = tv.tv_usec = 0; | ||
| 418 | res = libusb_handle_events_timeout(NULL, &tv); | ||
| 419 | if(res < 0) { | ||
| 420 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); | ||
| 421 | return res; | ||
| 422 | } | ||
| 423 | // reap devices marked dead due to an RX error | ||
| 424 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 425 | if(!usbdev->alive) { | ||
| 426 | device_remove(usbdev); | ||
| 427 | usb_disconnect(usbdev); | ||
| 428 | } | ||
| 429 | } ENDFOREACH | ||
| 430 | |||
| 431 | if(dev_poll_remain_ms() <= 0) { | ||
| 432 | res = usb_discover(); | ||
| 433 | if(res < 0) { | ||
| 434 | usbmuxd_log(LL_ERROR, "usb_discover failed: %d", res); | ||
| 435 | return res; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | return 0; | ||
| 439 | } | ||
| 440 | |||
| 441 | int usb_process_timeout(int msec) | ||
| 442 | { | ||
| 443 | int res; | ||
| 444 | struct timeval tleft, tcur, tfin; | ||
| 445 | gettimeofday(&tcur, NULL); | ||
| 446 | tfin.tv_sec = tcur.tv_sec + (msec / 1000); | ||
| 447 | tfin.tv_usec = tcur.tv_usec + (msec % 1000) * 1000; | ||
| 448 | tfin.tv_sec += tfin.tv_usec / 1000000; | ||
| 449 | tfin.tv_usec %= 1000000; | ||
| 450 | while((tfin.tv_sec > tcur.tv_sec) || ((tfin.tv_sec == tcur.tv_sec) && (tfin.tv_usec > tcur.tv_usec))) { | ||
| 451 | tleft.tv_sec = tfin.tv_sec - tcur.tv_sec; | ||
| 452 | tleft.tv_usec = tfin.tv_usec - tcur.tv_usec; | ||
| 453 | if(tleft.tv_usec < 0) { | ||
| 454 | tleft.tv_usec += 1000000; | ||
| 455 | tleft.tv_sec -= 1; | ||
| 456 | } | ||
| 457 | res = libusb_handle_events_timeout(NULL, &tleft); | ||
| 458 | if(res < 0) { | ||
| 459 | usbmuxd_log(LL_ERROR, "libusb_handle_events_timeout failed: %d", res); | ||
| 460 | return res; | ||
| 461 | } | ||
| 462 | // reap devices marked dead due to an RX error | ||
| 463 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 464 | if(!usbdev->alive) { | ||
| 465 | device_remove(usbdev); | ||
| 466 | usb_disconnect(usbdev); | ||
| 467 | } | ||
| 468 | } ENDFOREACH | ||
| 469 | gettimeofday(&tcur, NULL); | ||
| 470 | } | ||
| 471 | return 0; | ||
| 472 | } | ||
| 473 | |||
| 474 | int usb_init(void) | ||
| 475 | { | ||
| 476 | int res; | ||
| 477 | usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0"); | ||
| 478 | |||
| 479 | devlist_failures = 0; | ||
| 480 | res = libusb_init(NULL); | ||
| 481 | //libusb_set_debug(NULL, 3); | ||
| 482 | if(res != 0) { | ||
| 483 | usbmuxd_log(LL_FATAL, "libusb_init failed: %d", res); | ||
| 484 | return -1; | ||
| 485 | } | ||
| 486 | |||
| 487 | collection_init(&device_list); | ||
| 488 | |||
| 489 | return usb_discover(); | ||
| 490 | } | ||
| 491 | |||
| 492 | void usb_shutdown(void) | ||
| 493 | { | ||
| 494 | usbmuxd_log(LL_DEBUG, "usb_shutdown"); | ||
| 495 | FOREACH(struct usb_device *usbdev, &device_list) { | ||
| 496 | device_remove(usbdev); | ||
| 497 | usb_disconnect(usbdev); | ||
| 498 | } ENDFOREACH | ||
| 499 | collection_free(&device_list); | ||
| 500 | libusb_exit(NULL); | ||
| 501 | } | ||
diff --git a/usbmuxd/usb.h b/usbmuxd/usb.h new file mode 100644 index 0000000..8a48308 --- /dev/null +++ b/usbmuxd/usb.h | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __USB_H__ | ||
| 22 | #define __USB_H__ | ||
| 23 | |||
| 24 | #include <stdint.h> | ||
| 25 | #include "utils.h" | ||
| 26 | |||
| 27 | #define BULK_IN 0x85 | ||
| 28 | #define BULK_OUT 0x04 | ||
| 29 | |||
| 30 | // libusb fragments packets larger than this (usbfs limitation) | ||
| 31 | // on input, this creates race conditions and other issues | ||
| 32 | // I don't think the device uses larger packets | ||
| 33 | // if it does then we're going to have to implement proper framing... | ||
| 34 | #define USB_MRU 16384 | ||
| 35 | |||
| 36 | // max transmission packet size | ||
| 37 | // libusb fragments these too, but doesn't send ZLPs so we're safe | ||
| 38 | // but maybe we need to send a ZLP ourselves at the end (see usb-linux.h) | ||
| 39 | // we're using 3 * 16384 to optimize for the fragmentation | ||
| 40 | // this results in three URBs per full transfer, 32 USB packets each | ||
| 41 | // if there are ZLP issues this should make them show up too | ||
| 42 | #define USB_MTU (3 * 16384) | ||
| 43 | |||
| 44 | #define USB_PACKET_SIZE 512 | ||
| 45 | |||
| 46 | #define VID_APPLE 0x5ac | ||
| 47 | #define PID_IPHONE2G 0x1290 | ||
| 48 | #define PID_ITOUCH1G 0x1291 | ||
| 49 | #define PID_IPHONE3G 0x1292 | ||
| 50 | |||
| 51 | #define USB_CONFIGURATION 3 | ||
| 52 | #define USB_INTERFACE 1 | ||
| 53 | |||
| 54 | struct usb_device; | ||
| 55 | |||
| 56 | int usb_init(void); | ||
| 57 | void usb_shutdown(void); | ||
| 58 | const char *usb_get_serial(struct usb_device *dev); | ||
| 59 | uint32_t usb_get_location(struct usb_device *dev); | ||
| 60 | uint16_t usb_get_pid(struct usb_device *dev); | ||
| 61 | void usb_get_fds(struct fdlist *list); | ||
| 62 | int usb_get_timeout(void); | ||
| 63 | int usb_send(struct usb_device *dev, const unsigned char *buf, int length); | ||
| 64 | int usb_process(void); | ||
| 65 | int usb_process_timeout(int msec); | ||
| 66 | |||
| 67 | #endif | ||
diff --git a/usbmuxd/utils.c b/usbmuxd/utils.c new file mode 100644 index 0000000..1ffa04a --- /dev/null +++ b/usbmuxd/utils.c | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #include <stdlib.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include "utils.h" | ||
| 28 | #include "log.h" | ||
| 29 | |||
| 30 | void fdlist_create(struct fdlist *list) | ||
| 31 | { | ||
| 32 | list->count = 0; | ||
| 33 | list->capacity = 4; | ||
| 34 | list->owners = malloc(sizeof(*list->owners) * list->capacity); | ||
| 35 | list->fds = malloc(sizeof(*list->fds) * list->capacity); | ||
| 36 | } | ||
| 37 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events) | ||
| 38 | { | ||
| 39 | if(list->count == list->capacity) { | ||
| 40 | list->capacity *= 2; | ||
| 41 | list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity); | ||
| 42 | list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity); | ||
| 43 | } | ||
| 44 | list->owners[list->count] = owner; | ||
| 45 | list->fds[list->count].fd = fd; | ||
| 46 | list->fds[list->count].events = events; | ||
| 47 | list->fds[list->count].revents = 0; | ||
| 48 | list->count++; | ||
| 49 | } | ||
| 50 | |||
| 51 | void fdlist_free(struct fdlist *list) | ||
| 52 | { | ||
| 53 | list->count = 0; | ||
| 54 | list->capacity = 0; | ||
| 55 | free(list->owners); | ||
| 56 | list->owners = NULL; | ||
| 57 | free(list->fds); | ||
| 58 | list->fds = NULL; | ||
| 59 | } | ||
| 60 | |||
| 61 | void collection_init(struct collection *col) | ||
| 62 | { | ||
| 63 | col->list = malloc(sizeof(void *)); | ||
| 64 | memset(col->list, 0, sizeof(void *)); | ||
| 65 | col->capacity = 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | void collection_free(struct collection *col) | ||
| 69 | { | ||
| 70 | free(col->list); | ||
| 71 | col->list = NULL; | ||
| 72 | col->capacity = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | void collection_add(struct collection *col, void *element) | ||
| 76 | { | ||
| 77 | int i; | ||
| 78 | for(i=0; i<col->capacity; i++) { | ||
| 79 | if(!col->list[i]) { | ||
| 80 | col->list[i] = element; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | col->list = realloc(col->list, sizeof(void*) * col->capacity * 2); | ||
| 85 | memset(&col->list[col->capacity], 0, sizeof(void *) * col->capacity); | ||
| 86 | col->list[col->capacity] = element; | ||
| 87 | col->capacity *= 2; | ||
| 88 | } | ||
| 89 | |||
| 90 | void collection_remove(struct collection *col, void *element) | ||
| 91 | { | ||
| 92 | int i; | ||
| 93 | for(i=0; i<col->capacity; i++) { | ||
| 94 | if(col->list[i] == element) { | ||
| 95 | col->list[i] = NULL; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | usbmuxd_log(LL_ERROR, "collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); | ||
| 100 | } | ||
| 101 | |||
| 102 | int collection_count(struct collection *col) | ||
| 103 | { | ||
| 104 | int i, cnt = 0; | ||
| 105 | for(i=0; i<col->capacity; i++) { | ||
| 106 | if(col->list[i]) | ||
| 107 | cnt++; | ||
| 108 | } | ||
| 109 | return cnt; | ||
| 110 | } | ||
diff --git a/usbmuxd/utils.h b/usbmuxd/utils.h new file mode 100644 index 0000000..ad4ac9d --- /dev/null +++ b/usbmuxd/utils.h | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef __UTILS_H__ | ||
| 22 | #define __UTILS_H__ | ||
| 23 | |||
| 24 | #include <poll.h> | ||
| 25 | |||
| 26 | enum fdowner { | ||
| 27 | FD_LISTEN, | ||
| 28 | FD_CLIENT, | ||
| 29 | FD_USB | ||
| 30 | }; | ||
| 31 | |||
| 32 | struct fdlist { | ||
| 33 | int count; | ||
| 34 | int capacity; | ||
| 35 | enum fdowner *owners; | ||
| 36 | struct pollfd *fds; | ||
| 37 | }; | ||
| 38 | |||
| 39 | void fdlist_create(struct fdlist *list); | ||
| 40 | void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); | ||
| 41 | void fdlist_free(struct fdlist *list); | ||
| 42 | |||
| 43 | struct collection { | ||
| 44 | void **list; | ||
| 45 | int capacity; | ||
| 46 | }; | ||
| 47 | |||
| 48 | void collection_init(struct collection *col); | ||
| 49 | void collection_add(struct collection *col, void *element); | ||
| 50 | void collection_remove(struct collection *col, void *element); | ||
| 51 | int collection_count(struct collection *col); | ||
| 52 | void collection_free(struct collection *col); | ||
| 53 | |||
| 54 | #define FOREACH(var, col) \ | ||
| 55 | do { \ | ||
| 56 | int _iter; \ | ||
| 57 | for(_iter=0; _iter<(col)->capacity; _iter++) { \ | ||
| 58 | if(!(col)->list[_iter]) continue; \ | ||
| 59 | var = (col)->list[_iter]; | ||
| 60 | |||
| 61 | #define ENDFOREACH \ | ||
| 62 | } \ | ||
| 63 | } while(0); | ||
| 64 | |||
| 65 | #endif | ||
